SlideShare a Scribd company logo
1 of 19
Java fundamentals Todor Kolev
Java architecture Java's architecture arises out of four distinct but interrelated technologies: -the Java programming language -the Java class files -the Java Application Programming Interface -the Java virtual machine
Java architecture When you write and run a Java program, you are tapping the power of these four technologies. You express the program in source files written in the Java programming language, compile the source to Java class files, and run the class files on a Java virtual machine.  Java API is actually a huge collection of library routines that performs basic programming tasks. The combination of the Java virtual machine and Java API is called the  Java Platform   See the figure below...
Java architecture The Java programming environment.
Java architecture Java Virtual Machine(JVM)
Java architecture Java Virtual Machine(JVM) The Method area The method area is where the bytecodes reside. The program counter always points to (contains the address of) some byte in the method area. The program counter is used to keep track of the thread of execution. After a bytecode instruction has been executed, the program counter will contain the address of the next instruction to execute. After execution of an instruction, the JVM sets the program counter to the address of the instruction that immediately follows the previous one, unless the previous one specifically demanded a jump.
Java architecture Java Virtual Machine(JVM) The Java stack The Java stack is used to store the results of the bytecode instructions,  to pass parameters to and return values from methods as well as to keep the state of each method invocation.  The state of each method is called its Stack frame. For example, if we have method that calls other m If our program is calling  the A method, the A method will automatically call B, C and D, because of its state. Stack frame of A
- The JVM's heap stores all objects instantiated by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time.  Garbage collection   What is the Java heap memory? - Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.  Java architecture Java Virtual Machine(JVM)
Java references and parameters passing Java, unlike C++, stores all objects on the heap, and requires the new operator to create the object. Variables are still stored on the stack, but they hold a pointer to the object, not the object itself (and of course, to confuse C++ programmers more, these pointers are called "references").
Java references and parameters passing In C++, objects may be created on the stack using "automatic" allocation.  The following is legal C++: Integer foo = Integer(1);  it creates a new Integer object on the stack. A Java compiler, however, will reject it as a syntax error, because we haven’t allocated heap memory by ‘new’ operator.
Java references and parameters passing Truth #1: The values of variables in Java are always primitives or references, never objects.  public static void foo(String bar) {  Integer baz = new Integer(bar);  }
Java references and parameters passing Truth #2: Everything in Java is passed by value. Objects, however, are never passed at all.  1.   public void foo(Dog d) { 2.     d.name == "Max"; // true 3.    d = new Dog("Fifi"); 4.    d.name == "Fifi"; // true 5.   } 6. 7.   public static void main(String [] args){ 9.   Dog aDog = new Dog("Max"); 10.  foo(aDog); 11.  aDog.name == "Max"; // true 12.  }
Java references and parameters passing Java references vs. C++ pointers Pointers in C++ must be referenced and dereferenced using the *, whereas the referencing and dereferencing of objects is handled for you automatically by Java.  C++ int *num = new int(1); std::cout << num; //result 00345948 Java Integer num = new Integer(1); System. out .print(num); //result 1
Java references and parameters passing Java references vs. C++ pointers Java does not allow you to do pointer arithmetic .  Java public class Number { public int num; public Number(int num) { this.num = num; } } public class Main { public static void main(String[] args) { Number ref = new Number(1); System. out .print(++(ref.num)); } } //Result is 2 C++ class Number   { public: int num; Number(int n um )   { this-> num = n um ; } }; void main(){ Number * ptr  = new Number(1); // ptr==00345948 std::cout << (*(++ ptr )).num; // ptr==0034594C delete  ptr ;  //explicitly heap cleaning } //The value of ‘ (*(++ ptr )).num ’ is address with //an uninitialized value(-33686019).We got a Bug.
Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and  all methods in a final class are implicitly final.  Many of the Java standard library classes are final ,  for example  java.lang.System   and   java.lang.String .   A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.  A  final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.
Some Java Basics The three forms of Polymorphism Instance methods and overriding An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass  overrides  the superclass's method.  Overloading Overloaded methods are methods with the same  name signature  but either a different number of parameters or different types in the parameter list.    Dynamic (or late) method binding   Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at  runtime . 
Some Java Basics The three forms of Polymorphism Dynamic (or late) method binding - Example public abstract class Employee { abstract void printPosition(); } public class Slave extends Employee { public void printPosition() { System. out .println(&quot;I am the Slave!&quot;); } } public class Manager extends Employee { public void printPosition() { System. out .println(&quot;I am the Boss!&quot;); } } public class Main { public static void main(String[] args) { Employee ref; Slave aSlave = new Slave(); ref = aSlave; ref.printPosition();//I am the Slave! Manager aManager = new Manager(); ref = aManager; ref.printPosition();//I am the Boss! } }
Some Java Basics Variables Instance Variables (Non-Static Fields)  Non-static fields are also known as  instance variables  because their values are unique to each  instance  of a class (to each object, in other words).  Class Variables (Static Fields) A  class variable  is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.  Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in  local variables . These are only visible to the methods in which they are declared. Parameters  are variables that are passed to the methods.
Some Java Basics Constants The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.  String Class The String class represents character strings. All string literals in Java programs, such as &quot;abc&quot;, are implemented as instances of this class.  Since strings are instances of the String class, max size of a String class object could be the  available heap memory.

More Related Content

What's hot

Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features OverviewSergii Stets
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzJAX London
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhHarmeet Singh(Taara)
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummaryAmal Khailtash
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2Todor Kolev
 
Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and BytecodeYoav Avrahami
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8Garth Gilmour
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New FeaturesNaveen Hegde
 

What's hot (18)

Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java 8 - Features Overview
Java 8 - Features OverviewJava 8 - Features Overview
Java 8 - Features Overview
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Java 8 ​and ​Best Practices
Java 8 ​and ​Best PracticesJava 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
 
Playing with Java Classes and Bytecode
Playing with Java Classes and BytecodePlaying with Java Classes and Bytecode
Playing with Java Classes and Bytecode
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 

Similar to Java findamentals1

Similar to Java findamentals1 (20)

Java findamentals2
Java findamentals2Java findamentals2
Java findamentals2
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUALCS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 

Java findamentals1

  • 2. Java architecture Java's architecture arises out of four distinct but interrelated technologies: -the Java programming language -the Java class files -the Java Application Programming Interface -the Java virtual machine
  • 3. Java architecture When you write and run a Java program, you are tapping the power of these four technologies. You express the program in source files written in the Java programming language, compile the source to Java class files, and run the class files on a Java virtual machine. Java API is actually a huge collection of library routines that performs basic programming tasks. The combination of the Java virtual machine and Java API is called the  Java Platform See the figure below...
  • 4. Java architecture The Java programming environment.
  • 5. Java architecture Java Virtual Machine(JVM)
  • 6. Java architecture Java Virtual Machine(JVM) The Method area The method area is where the bytecodes reside. The program counter always points to (contains the address of) some byte in the method area. The program counter is used to keep track of the thread of execution. After a bytecode instruction has been executed, the program counter will contain the address of the next instruction to execute. After execution of an instruction, the JVM sets the program counter to the address of the instruction that immediately follows the previous one, unless the previous one specifically demanded a jump.
  • 7. Java architecture Java Virtual Machine(JVM) The Java stack The Java stack is used to store the results of the bytecode instructions, to pass parameters to and return values from methods as well as to keep the state of each method invocation. The state of each method is called its Stack frame. For example, if we have method that calls other m If our program is calling the A method, the A method will automatically call B, C and D, because of its state. Stack frame of A
  • 8. - The JVM's heap stores all objects instantiated by an executing Java program. Objects are created by Java's &quot;new&quot; operator, and memory for new objects is allocated on the heap at run time. Garbage collection What is the Java heap memory? - Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches. Java architecture Java Virtual Machine(JVM)
  • 9. Java references and parameters passing Java, unlike C++, stores all objects on the heap, and requires the new operator to create the object. Variables are still stored on the stack, but they hold a pointer to the object, not the object itself (and of course, to confuse C++ programmers more, these pointers are called &quot;references&quot;).
  • 10. Java references and parameters passing In C++, objects may be created on the stack using &quot;automatic&quot; allocation. The following is legal C++: Integer foo = Integer(1); it creates a new Integer object on the stack. A Java compiler, however, will reject it as a syntax error, because we haven’t allocated heap memory by ‘new’ operator.
  • 11. Java references and parameters passing Truth #1: The values of variables in Java are always primitives or references, never objects. public static void foo(String bar) { Integer baz = new Integer(bar); }
  • 12. Java references and parameters passing Truth #2: Everything in Java is passed by value. Objects, however, are never passed at all. 1. public void foo(Dog d) { 2.   d.name == &quot;Max&quot;; // true 3.   d = new Dog(&quot;Fifi&quot;); 4.   d.name == &quot;Fifi&quot;; // true 5. } 6. 7. public static void main(String [] args){ 9. Dog aDog = new Dog(&quot;Max&quot;); 10. foo(aDog); 11. aDog.name == &quot;Max&quot;; // true 12. }
  • 13. Java references and parameters passing Java references vs. C++ pointers Pointers in C++ must be referenced and dereferenced using the *, whereas the referencing and dereferencing of objects is handled for you automatically by Java. C++ int *num = new int(1); std::cout << num; //result 00345948 Java Integer num = new Integer(1); System. out .print(num); //result 1
  • 14. Java references and parameters passing Java references vs. C++ pointers Java does not allow you to do pointer arithmetic . Java public class Number { public int num; public Number(int num) { this.num = num; } } public class Main { public static void main(String[] args) { Number ref = new Number(1); System. out .print(++(ref.num)); } } //Result is 2 C++ class Number { public: int num; Number(int n um ) { this-> num = n um ; } }; void main(){ Number * ptr = new Number(1); // ptr==00345948 std::cout << (*(++ ptr )).num; // ptr==0034594C delete ptr ; //explicitly heap cleaning } //The value of ‘ (*(++ ptr )).num ’ is address with //an uninitialized value(-33686019).We got a Bug.
  • 15. Some Java Basics ‘ final’ keyword A  final class  cannot be extended. This is done for reasons of security and efficiency and all methods in a final class are implicitly final. Many of the Java standard library classes are final , for example java.lang.System and java.lang.String . A  final method  cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class. A final variable  can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of definition: this is called a 'blank final' variable.
  • 16. Some Java Basics The three forms of Polymorphism Instance methods and overriding An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass  overrides  the superclass's method. Overloading Overloaded methods are methods with the same  name signature  but either a different number of parameters or different types in the parameter list.  Dynamic (or late) method binding Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at  runtime . 
  • 17. Some Java Basics The three forms of Polymorphism Dynamic (or late) method binding - Example public abstract class Employee { abstract void printPosition(); } public class Slave extends Employee { public void printPosition() { System. out .println(&quot;I am the Slave!&quot;); } } public class Manager extends Employee { public void printPosition() { System. out .println(&quot;I am the Boss!&quot;); } } public class Main { public static void main(String[] args) { Employee ref; Slave aSlave = new Slave(); ref = aSlave; ref.printPosition();//I am the Slave! Manager aManager = new Manager(); ref = aManager; ref.printPosition();//I am the Boss! } }
  • 18. Some Java Basics Variables Instance Variables (Non-Static Fields) Non-static fields are also known as  instance variables  because their values are unique to each  instance  of a class (to each object, in other words). Class Variables (Static Fields) A  class variable  is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in  local variables . These are only visible to the methods in which they are declared. Parameters are variables that are passed to the methods.
  • 19. Some Java Basics Constants The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change. String Class The String class represents character strings. All string literals in Java programs, such as &quot;abc&quot;, are implemented as instances of this class. Since strings are instances of the String class, max size of a String class object could be the available heap memory.