Your SlideShare is downloading. ×
0
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Java findamentals1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java findamentals1

319

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
319
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java fundamentals Todor Kolev
  • 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 "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)
  • 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 "references").
  • 10. 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.
  • 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 == "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. }
  • 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.

×