This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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 thenewoperator 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
newIntegerobject on the stack. A Java compiler, however, will
reject it as a syntax error, because we havent 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 passedat
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 = n um ; } }; void main(){ Number * ptr = new
Number(1); // ptr==00345948 std::cout