SlideShare a Scribd company logo
Java course - IAG0040




              Java Basics,
             Program Flow


Anton Keks                           2011
Java type system
 ●
     static type checking         ●
                                      provides (benefits)
     –   by compiler, type            –   safety
         declarations required             ●
                                               many bugs are type
 ●
     strongly typed                            errors
     –   runtime checking             –   documentation
     –   automatic conversions        –   abstraction
         allowed only when not             ●   high-level thinking
         loosing information
                                      –   optimization
 ●   memory-safe                          (performance)
     –   array bounds checking,
         no pointer arithmetic
Java course – IAG0040                                          Lecture 2
Anton Keks                                                       Slide 2
Objects and Classes
 ●
     Classes introduce new types into a program
 ●
     First appeared in Simula-67 to define 'classes of
     objects'
 ●
     In OOP, Objects are instances of Classes
 ●
     All objects of the same class share
           –   same properties:
                        ●   fields hold internal state
           –   same behavior, dependent on the state:
                        ●
                            methods used for communication with objects

Java course – IAG0040                                             Lecture 2
Anton Keks                                                          Slide 3
References
 ●   You manipulate objects with references
     –   Like remote control of a TV is a reference to the TV
 ●
     Objects are stored on the heap, independently
     –   String s;      // is a reference to nothing
     –   s = “hello”; // now s points to a newly created
                      // String object on the heap
 ●
     You must create all the objects before using them
     –   s = new Date();
 ●   There are many other built-in types (classes), like
     Integer, Date, Currency, BigDecimal, etc, and making
     your own types is the point of OOP
Java course – IAG0040                                     Lecture 2
Anton Keks                                                  Slide 4
Variables and Fields
 ●
     Local variables
     –   inside of methods
     –   final variables can't change their values
 ●
     Class members (fields)
     –   inside of classes, outside of methods
 ●
     Global variables
     –   don't exist in Java
     –   can be simulated with static class members
 ●
     Constants are static final fields
Java course – IAG0040                                 Lecture 2
Anton Keks                                              Slide 5
Primitive types
 ●
     Only primitives are not objects in Java
     –   But, there are wrapper classes. Unlike in C, formats and
         sizes are standardized and are always the same.
     Primitive Size          Min       Max        Wrapper Default
     boolean 1 bit? false          true          Boolean false
     char        16 bit Unicode 0 216-1 (UTF-16) Character 'u0000'
     byte        8 bit   -128      127           Byte      (byte)0
     short       16 bit -215       215-1         Short     (short)0
     int         32 bit -231       231-1         Integer   0
     long        64 bit -263       263-1         Long      0L
     float       32 bit IEEE 754 IEEE 754 Float            0.0f
     double      64 bit IEEE 754 IEEE 754 Double           0.0d
     void        -       -         -             Void      -
Java course – IAG0040                                          Lecture 2
Anton Keks                                                       Slide 6
High-precision numbers
 ●
     BigInteger and BigDecimal
     –   these are classes, not primitives
     –   immutable
     –   cannot be used with operators, operations are
         methods
     –   slower, but long and precise
 ●   BigInteger – arbitrary long integers
 ●   BigDecimal – arbitrary precision fixed point
     numbers
Java course – IAG0040                                Lecture 2
Anton Keks                                             Slide 7
Fields (Members)
 ●
     A field (or member) is a class variable, holds its state
 ●
     Field names are in “lowerCamelCase”
 ●
     Constant names are in UPPER_CASE
 ●
     Initialized either automatically or manually
 ●   class MyClass {
        boolean isEmpty;                            // a field
        int count;                                  // a second field
         static String foo = “bar”;                 // a static field
         static final double E = Math.E;            // a constant
         static {
            foo = “fooBar”;                         // static block
         }
     }
Java course – IAG0040                                           Lecture 2
Anton Keks                                                        Slide 8
Methods
 ●
     A method is a class function, used to execute
     an operation on a class (aka send a message)
 ●   class MyClass {
        int count;                          // a field
        void doStuff(int count, short b) { // a method
           this.count = count * b;          // method body
        }
        int getCount() {              // second method,
           return count;              // returning a value
        }
     }
 ●   Methods can be overloaded
 ●   Parameters are passed as references
 ●   Local variables are enforced to be manually initialized
Java course – IAG0040                                          Lecture 2
Anton Keks                                                       Slide 9
Accessing fields and methods
 ●
     Java uses a dot '.' as an accessor operator
     –   MyClass myObject = new MyClass();
         myObject.isEmpty = true;            // field access
     –   myObject.setCount(5);               // method call
     –   int j = new MyClass().getCount();   // also allowed

 ●   Static fields and methods are accessed
     through the class itself, not instance variables
     –   int i = Integer.MAX_VALUE;

 ●
     “Deep” access is possible
     –   System.out.println(“Hello!”);


Java course – IAG0040                                   Lecture 2
Anton Keks                                               Slide 10
Access Modifiers and Visibility
 ●
     Several access modifiers are in use (keywords):
     –   private – accessible only in the same class
     –   default, no keyword (package local) – accessible only
         in the same package
     –   protected – accessible in the same package and
         subclasses
     –   public – accessible from anywhere
 ●   public class HelloWorld {         //   public class
        int a;                         //   package local field
        private int b;                 //   private field
        protected void doStuff() {}    //   protected method
        private HelloWorld() {}        //   private constructor
     }
Java course – IAG0040                                      Lecture 2
Anton Keks                                                  Slide 11
Encapsulation
 ●
     Use the most restrictive access modifiers as
     possible
 ●   Classes 'hide' implementations and internal
     details from their users
           –   This allows changing of internals any times
                without breaking any usages
           –   The less internals exposed, the easier is to
                change them
 ●
     Public/accessible interface must be obvious to
     use; don't do anything unexpected!
Java course – IAG0040                                     Lecture 2
Anton Keks                                                 Slide 12
Operators
 ●   Arithmetical: + - * / %
 ●   Bitwise: ~ & | ^ << >> >>>
 ●   Boolean: ! & | ^
 ●   Relational: < > <= >= == !=
 ●   Conditional: && || ^^ ternary: ? :
 ●   Assignment: = += -= *= /= %= <<= >>= &= |= ^=
 ●   String concatenation: +
 ●   Unary: + - ++ -- ! ~ (cast)
 ●   Type comparision: instanceof
Java course – IAG0040                        Lecture 2
Anton Keks                                    Slide 13
Naming convention
 ●
     Packages: lower case, reversed domain name as prefix
      –   net.azib.hello, java.util, com.sun.internal
 ●   Classes: camel case, should contain a noun
      –   String, ArrayList, System, VeryAngryDogWithTeeth
 ●   Interfaces: camel case, often adjectives
      –   Collection, List, Comparable, Iterable
 ●   Variables and fields: lower camel case, often include the class name
      –   isEmpty, count, i, veryLongArrayList
 ●   Methods: lower camel case, contain verbs
      –   doStuff, processRequest, length, getLength
 ●   Constants: underscore ('_') separated upper case
      –   Math.PI, MAX_VALUE, NAME_PREFIX
 ●
     Names should be obvious and descriptive!!!
      –   System.currentTimeMillis(), s.substring(0, 3), s.toCharArray()
Java course – IAG0040                                                       Lecture 2
Anton Keks                                                                   Slide 14

More Related Content

PDF
Java Course 3: OOP
PDF
Java Course 1: Introduction
PDF
Java Course 15: Ant, Scripting, Spring, Hibernate
PDF
Java Course 7: Text processing, Charsets & Encodings
PDF
Java Course 4: Exceptions & Collections
PDF
Java Course 5: Enums, Generics, Assertions
PDF
Java Course 6: Introduction to Agile
PDF
Java Course 12: XML & XSL, Web & Servlets
Java Course 3: OOP
Java Course 1: Introduction
Java Course 15: Ant, Scripting, Spring, Hibernate
Java Course 7: Text processing, Charsets & Encodings
Java Course 4: Exceptions & Collections
Java Course 5: Enums, Generics, Assertions
Java Course 6: Introduction to Agile
Java Course 12: XML & XSL, Web & Servlets

What's hot (20)

PDF
Java Course 13: JDBC & Logging
PDF
Core Java Tutorial
PPTX
Core java complete ppt(note)
PDF
Core Java Certification
PPTX
Core Java introduction | Basics | free course
PPT
Java basic tutorial by sanjeevini india
PPTX
Core java
PPT
Core java
PPT
Java Tutorial
PPT
Java Basics
PDF
Java Presentation For Syntax
PDF
New Features Of JDK 7
PDF
An Introduction to Java Compiler and Runtime
PPT
Java tutorial PPT
PPT
Presentation to java
PPTX
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
PPSX
Introduction to java
PPTX
Java features
PPTX
Java training in delhi
PPT
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Java Course 13: JDBC & Logging
Core Java Tutorial
Core java complete ppt(note)
Core Java Certification
Core Java introduction | Basics | free course
Java basic tutorial by sanjeevini india
Core java
Core java
Java Tutorial
Java Basics
Java Presentation For Syntax
New Features Of JDK 7
An Introduction to Java Compiler and Runtime
Java tutorial PPT
Presentation to java
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Introduction to java
Java features
Java training in delhi
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Ad

Viewers also liked (20)

PPT
PALASH SL GUPTA
PDF
Java basics notes
PPTX
Basics of java 2
PPT
Programming with Java: the Basics
PPT
Java Basics
PDF
Introduction to basics of java
PPT
Java basics
PPT
Java Programming for Designers
PPTX
Basics of file handling
PPT
2. Basics of Java
PPTX
Java basics
PPT
Core java Basics
PPT
Core Java Basics
PPTX
OOPs in Java
PPT
Java Basics
PPTX
Java basics part 1
PPTX
Java Basics
PPTX
Ppt on java basics
PPT
Basics of java programming language
PPT
02 java basics
PALASH SL GUPTA
Java basics notes
Basics of java 2
Programming with Java: the Basics
Java Basics
Introduction to basics of java
Java basics
Java Programming for Designers
Basics of file handling
2. Basics of Java
Java basics
Core java Basics
Core Java Basics
OOPs in Java
Java Basics
Java basics part 1
Java Basics
Ppt on java basics
Basics of java programming language
02 java basics
Ad

Similar to Java Course 2: Basics (20)

PPT
core java
PPTX
Android webinar class_java_review
PPS
Dacj 1-2 a
PPS
Dacj 1-2 b
PDF
Core java 5 days workshop stuff
PPT
PPT
JavaYDL10
PDF
Java 7
PPT
Core java
PPS
Java session02
PDF
Java concepts and questions
PPTX
Learning core java
PDF
Shiksharth com java_topics
PPTX
Classes, objects in JAVA
PDF
Java Course 11: Design Patterns
PDF
Java Course 9: Networking and Reflection
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
PPTX
java Basic Programming Needs
PDF
core java
PPT
Intro to Java for C++ Developers
core java
Android webinar class_java_review
Dacj 1-2 a
Dacj 1-2 b
Core java 5 days workshop stuff
JavaYDL10
Java 7
Core java
Java session02
Java concepts and questions
Learning core java
Shiksharth com java_topics
Classes, objects in JAVA
Java Course 11: Design Patterns
Java Course 9: Networking and Reflection
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
java Basic Programming Needs
core java
Intro to Java for C++ Developers

More from Anton Keks (9)

PDF
Being a professional software tester
PDF
Java Course 14: Beans, Applets, GUI
PDF
Java Course 10: Threads and Concurrency
PDF
Java Course 8: I/O, Files and Streams
PDF
Choose a pattern for a problem
PDF
Simple Pure Java
PDF
Database Refactoring
PDF
Scrum is not enough - being a successful agile engineer
PDF
Being a Professional Software Developer
Being a professional software tester
Java Course 14: Beans, Applets, GUI
Java Course 10: Threads and Concurrency
Java Course 8: I/O, Files and Streams
Choose a pattern for a problem
Simple Pure Java
Database Refactoring
Scrum is not enough - being a successful agile engineer
Being a Professional Software Developer

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Spectroscopy.pptx food analysis technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Spectroscopy.pptx food analysis technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology

Java Course 2: Basics

  • 1. Java course - IAG0040 Java Basics, Program Flow Anton Keks 2011
  • 2. Java type system ● static type checking ● provides (benefits) – by compiler, type – safety declarations required ● many bugs are type ● strongly typed errors – runtime checking – documentation – automatic conversions – abstraction allowed only when not ● high-level thinking loosing information – optimization ● memory-safe (performance) – array bounds checking, no pointer arithmetic Java course – IAG0040 Lecture 2 Anton Keks Slide 2
  • 3. Objects and Classes ● Classes introduce new types into a program ● First appeared in Simula-67 to define 'classes of objects' ● In OOP, Objects are instances of Classes ● All objects of the same class share – same properties: ● fields hold internal state – same behavior, dependent on the state: ● methods used for communication with objects Java course – IAG0040 Lecture 2 Anton Keks Slide 3
  • 4. References ● You manipulate objects with references – Like remote control of a TV is a reference to the TV ● Objects are stored on the heap, independently – String s; // is a reference to nothing – s = “hello”; // now s points to a newly created // String object on the heap ● You must create all the objects before using them – s = new Date(); ● There are many other built-in types (classes), like Integer, Date, Currency, BigDecimal, etc, and making your own types is the point of OOP Java course – IAG0040 Lecture 2 Anton Keks Slide 4
  • 5. Variables and Fields ● Local variables – inside of methods – final variables can't change their values ● Class members (fields) – inside of classes, outside of methods ● Global variables – don't exist in Java – can be simulated with static class members ● Constants are static final fields Java course – IAG0040 Lecture 2 Anton Keks Slide 5
  • 6. Primitive types ● Only primitives are not objects in Java – But, there are wrapper classes. Unlike in C, formats and sizes are standardized and are always the same. Primitive Size Min Max Wrapper Default boolean 1 bit? false true Boolean false char 16 bit Unicode 0 216-1 (UTF-16) Character 'u0000' byte 8 bit -128 127 Byte (byte)0 short 16 bit -215 215-1 Short (short)0 int 32 bit -231 231-1 Integer 0 long 64 bit -263 263-1 Long 0L float 32 bit IEEE 754 IEEE 754 Float 0.0f double 64 bit IEEE 754 IEEE 754 Double 0.0d void - - - Void - Java course – IAG0040 Lecture 2 Anton Keks Slide 6
  • 7. High-precision numbers ● BigInteger and BigDecimal – these are classes, not primitives – immutable – cannot be used with operators, operations are methods – slower, but long and precise ● BigInteger – arbitrary long integers ● BigDecimal – arbitrary precision fixed point numbers Java course – IAG0040 Lecture 2 Anton Keks Slide 7
  • 8. Fields (Members) ● A field (or member) is a class variable, holds its state ● Field names are in “lowerCamelCase” ● Constant names are in UPPER_CASE ● Initialized either automatically or manually ● class MyClass { boolean isEmpty; // a field int count; // a second field static String foo = “bar”; // a static field static final double E = Math.E; // a constant static { foo = “fooBar”; // static block } } Java course – IAG0040 Lecture 2 Anton Keks Slide 8
  • 9. Methods ● A method is a class function, used to execute an operation on a class (aka send a message) ● class MyClass { int count; // a field void doStuff(int count, short b) { // a method this.count = count * b; // method body } int getCount() { // second method, return count; // returning a value } } ● Methods can be overloaded ● Parameters are passed as references ● Local variables are enforced to be manually initialized Java course – IAG0040 Lecture 2 Anton Keks Slide 9
  • 10. Accessing fields and methods ● Java uses a dot '.' as an accessor operator – MyClass myObject = new MyClass(); myObject.isEmpty = true; // field access – myObject.setCount(5); // method call – int j = new MyClass().getCount(); // also allowed ● Static fields and methods are accessed through the class itself, not instance variables – int i = Integer.MAX_VALUE; ● “Deep” access is possible – System.out.println(“Hello!”); Java course – IAG0040 Lecture 2 Anton Keks Slide 10
  • 11. Access Modifiers and Visibility ● Several access modifiers are in use (keywords): – private – accessible only in the same class – default, no keyword (package local) – accessible only in the same package – protected – accessible in the same package and subclasses – public – accessible from anywhere ● public class HelloWorld { // public class int a; // package local field private int b; // private field protected void doStuff() {} // protected method private HelloWorld() {} // private constructor } Java course – IAG0040 Lecture 2 Anton Keks Slide 11
  • 12. Encapsulation ● Use the most restrictive access modifiers as possible ● Classes 'hide' implementations and internal details from their users – This allows changing of internals any times without breaking any usages – The less internals exposed, the easier is to change them ● Public/accessible interface must be obvious to use; don't do anything unexpected! Java course – IAG0040 Lecture 2 Anton Keks Slide 12
  • 13. Operators ● Arithmetical: + - * / % ● Bitwise: ~ & | ^ << >> >>> ● Boolean: ! & | ^ ● Relational: < > <= >= == != ● Conditional: && || ^^ ternary: ? : ● Assignment: = += -= *= /= %= <<= >>= &= |= ^= ● String concatenation: + ● Unary: + - ++ -- ! ~ (cast) ● Type comparision: instanceof Java course – IAG0040 Lecture 2 Anton Keks Slide 13
  • 14. Naming convention ● Packages: lower case, reversed domain name as prefix – net.azib.hello, java.util, com.sun.internal ● Classes: camel case, should contain a noun – String, ArrayList, System, VeryAngryDogWithTeeth ● Interfaces: camel case, often adjectives – Collection, List, Comparable, Iterable ● Variables and fields: lower camel case, often include the class name – isEmpty, count, i, veryLongArrayList ● Methods: lower camel case, contain verbs – doStuff, processRequest, length, getLength ● Constants: underscore ('_') separated upper case – Math.PI, MAX_VALUE, NAME_PREFIX ● Names should be obvious and descriptive!!! – System.currentTimeMillis(), s.substring(0, 3), s.toCharArray() Java course – IAG0040 Lecture 2 Anton Keks Slide 14