Java findamentals1

19
Java fundamentals Todor Kolev

Transcript of Java findamentals1

Page 1: Java findamentals1

Java fundamentalsTodor Kolev

Page 2: Java findamentals1

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

Page 3: Java findamentals1

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...

Page 4: Java findamentals1

Java architecture

The Java programming environment.

Page 5: Java findamentals1

Java architectureJava Virtual Machine(JVM)

Page 6: Java findamentals1

Java architectureJava 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.

Page 7: Java findamentals1

Java architectureJava 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

Page 8: Java findamentals1

- 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 architectureJava Virtual Machine(JVM)

Page 9: Java findamentals1

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").

Page 10: Java findamentals1

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.

Page 11: Java findamentals1

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); }

Page 12: Java findamentals1

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"; // true3.  d = new Dog("Fifi");4.  d.name == "Fifi"; // true5. }6.

7. public static void main(String [] args){9. Dog aDog = new Dog("Max");10. foo(aDog);11. aDog.name == "Max"; // true12. }

Page 13: Java findamentals1

Java references and parameters passingJava 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

JavaInteger num = new Integer(1);System.out.print(num);

//result 1

Page 14: Java findamentals1

Java references and parameters passingJava references vs. C++ pointers

Java does not allow you to do pointer arithmetic.

Javapublic 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 num) {

this->num = num;

}

};

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.

Page 15: Java findamentals1

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.

Page 16: Java findamentals1

Some Java BasicsThe three forms of Polymorphism

Instance methods and overridingAn 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.

OverloadingOverloaded 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. 

Page 17: Java findamentals1

Some Java BasicsThe 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("I am the Slave!");}

}

public class Manager extends Employee {public void printPosition() {System.out.println("I am the Boss!");}

}

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! }

}

Page 18: Java findamentals1

Some Java BasicsVariables

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.

Page 19: Java findamentals1

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 "abc", 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.