Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Oop JVM in Context. oop What’s a JVM uPlatform-independent execution environment lA different JVM is required for each operating system lAny JVM can run.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 The Java Virtual Machine Yearly Programming Project.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Reflection. Compile-Time vs Run-Time Some data you know at compile time: int area = radius*radius*3.14; The “3.14” is set – known at compile time.
Object Oriented Programming in Java Lecture 13. Java Reflection Reflection is a feature unique to Java that allows an executing program to examine or.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Lecture 10 : Introduction to Java Virtual Machine
1 Introduction to JVM Based on material produced by Bill Venners.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Constructors & Garbage Collection Ch. 9 – Head First Java.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Languages and Paradigms Activation Records in Java.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
RealTimeSystems Lab Jong-Koo, Lim
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Design issues for Object-Oriented Languages
CS 432: Compiler Construction Lecture 15
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Java Programming Language
CSE 413, Autumn 2002 Programming Languages
Topic: JAVA .Class File Structure
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Presentation transcript:

Java Virtual Machine (JVM)

Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem Learn about the linking process: oVerification oPreparation oResolution Understand the class initialization process Learn about the steps involved in the class instantiation

Introduction to the JVM JVM is a component of the Java system that interprets and executes the instructions in our class files. The following figure shows a block diagram of the JVM that includes its major subsystems and memory areas. Figure 1: Memory configuration by the JVM.

Each instance of the JVM has one method area, one heap, and one or more stacks - one for each thread When JVM loads a class file, it puts its information in the method area As the program runs, all objects instantiated are stored in the heap The stack area is used to store activation records as a program runs Introduction to the JVM (Cont’d)

Figure 2: Content of Memory Blocks at runtime.

The Class Loader Subsystem The class loader performs three main functions of JVM, namely: loading, linking and initialization The linking process consists of three sub-tasks, namely, verification, preparation, and resolution Figure 3: Class loading process.

Class Loading Process Loading means reading the class file for a type, parsing it to get its information, and storing the information in the method area. For each type it loads, the JVM must store the following information in the method area:  The fully qualified name of the type  The fully qualified name of the type's direct superclass or if the type is an interface, a list of its direct super interfaces.  Whether the type is a class or an interface  The type's modifiers ( public, abstract, final, etc)  Constant pool for the type: constants and symbolic references.  Field info : name, type and modifiers of variables (not constants)  Method info: name, return type, number & types of parameters, modifiers, bytecodes, size of stack frame and exception table.

The end of the loading process is the creation of an instance of java.lang.Class for the loaded type. The purpose is to give access to some of the information captured in the method area for the type, to the programmer. Some of the methods of the class java.lang.Class are: Note that for any loaded type T, only one instance of java.lang.Class is created even if T is used several times in an application. To use the above methods, we need to first call the getClass() method on any instance of T to get the reference to the Class instance for T. Class Loading Process (Cont’d) public String getName() public Class getSupClass() public boolean isInterface() public Class[] getInterfaces() public Method[] getMethods() public Fields[] getFields() public Constructor[] getConstructors()

Class Loading Process (Cont’d) Figure 4: Instances of Class objects created in the heap at runtime.

Class Loading Process (Cont’d) import java.lang.reflect.Method; // Required! //you must import your Circle class public class TestClassClass{ public static void main(String[] args) { String name = new String(“Ahmed”); Class nameClassInfo = name.getClass(); System.out.println("Class name is : “ + nameClassInfo.getName()); System.out.println("Parent is : “ + nameClassInfo.getSuperclass()); Method[] methods = nameClassInfo.getMethods(); System.out.println("\nMethods are: "); for(int i = 0; i < methods.length; i++) System.out.println(methods[i]); }

Class Loading Process (Cont’d) public methods are displayed ONLY!

What if we do not have an object of a class T and would like to know whether the class of type T is loaded in the method are? Then, the class Class provides us the following method for such purpose: Class Loading Process (Cont’d) public static Class forName(String className) throws ClassNotFoundException import java.lang.reflect.Method; public class TestClassClass{ public static void main(String[] args) throws Exception { Class test = Class.forName("TestClassClass"); System.out.println("\nClass name is: "+test.getName()); System.out.println("Superclass is: "+test.getSuperclass()); System.out.println("\nMethods are: "); Method[] methods = test.getMethods(); for(int i = 0; i<methods.length; i++) System.out.println(methods[i]); }

Class Loading Process (Cont’d)

Verification During Linking Process The next process handled by the class loader is Linking. This involves three sub-processes: Verification, Preparation and Resolution Verification is the process of ensuring that binary representation of a class is structurally correct The JVM has to make sure that a file it is asked to load was generated by a valid compiler and it is well formed Class B may be a valid sub-class of A at the time A and B were compiled, but class A may have been changed and re-compiled Example of some of the things that are checked at verification are:  Every method is provided with a structurally correct signature  Every instruction obeys the type discipline of the Java language  Every branch instruction branches to the start not middle of another instruction

Preparation In this phase, the JVM allocates memory for the class (i.e static) variables and sets them to default initial values. Note that class variables are not initialized to their proper initial values until the initialization phase - no java code is executed until initialization. The default values for the various types are shown below:

Resolution Resolution is the process of replacing symbolic names for types, fields and methods used by a loaded type with their actual references. Symbolic references are resolved into a direct references by searching through the method area to locate the referenced entity. For the class below, at the loading phase, the class loader would have loaded the classes: TestClassClass, String, System and Object. The names of these classes would have been stored in the constant pool for TestClassClass. In this phase, the names are replaced with their actual references. public class TestClassClass{ public static void main(String[] args){ String name = new String(“Ahmed”); Class nameClassInfo = name.getClass(); System.out.println("Parent is: “ + nameClassInfo.getSuperclass()); }

Class Initialization This is the process of setting class variables to their proper initial values - initial values desired by the programmer. Initialization of a class consists of two steps:  Initializing its direct superclass (if any and if not already initialized)  Executing its own initialization statements The above imply that, the first class that gets initialized is Object. Note that static final variables are not treated as class variables but as constants and are assigned their values at compilation. class Example1 { static double rate = 3.5; static int size = 3*(int)(Math.random()*5);... } class Example2 { static final int angle = 35; static final int length = angle * 2;... }

After a class is loaded, linked, and initialized, it is ready for use. Its static fields and static methods can be used and it can be instantiated. When a new class instance is created, memory is allocated for all its instance variables in the heap. Memory is also allocated recursively for all the instance variables declared in its super class and all classes up is inheritance hierarchy. All instance variables in the new object and those of its superclasses are then initialized to their default values. The constructor invoked in the instantiation is then processed according to the rules shown on the next page. Finally, the reference to the newly created object is returned as the result. Class Initialization (Cont’d)

Class Instantiation Rules for processing a constructor: 1.Assign the arguments for the constructor to its parameter variables. 2.If this constructor begins with an explicit invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively. 3.If this constructor is for a class other than Object, then it will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively. 4.Initialize the instance variables for this class with their proper values. 5.Execute the rest of the body of this constructor.

class GrandFather { int grandy = 70; public GrandFather(int grandy) { this.grandy = grandy; System.out.println("Grandy: "+grandy); } class Father extends GrandFather { int father = 40; public Father(int g, int f) { super(g); father = f; System.out.println("Grandy: "+grandy+" Father: "+father); } class Son extends Father { int son = 10; public Son(int grandy, int father, int son) { super(grandy, father); this.son = son; System.out.println("Grandy: "+grandy+" Father: "+father+" Son: "+son); } public class Instantiation1 { public static void main(String[] args) { Son s = new Son(65, 35, 5); } Class Instantiation: Example 1

Class Instantiation: Example 2 class GrandFather { int grandy = 70; public GrandFather(int grandy) { System.out.println("Grandy: "+this.grandy+ " Father: "+getFather() + " Son:"+getSon()); this.grandy = grandy; System.out.println("Grandy: "+grandy);} public int getFather() { return -1; } public int getSon() { return -2;}} class Son extends Father { int son = 10; public int getFather() { return father;} public int getSon() { return son;} public Son(int grandy, int father, int son) { super(grandy, father); System.out.println("Grandy: "+this.grandy+ " Father: "+getFather() + " Son:"+getSon()); this.son = son; System.out.println("Grandy: "+this.grandy+ " Father: "+this.father+" Son: "+this.son);}} public class Instantiation2 { public static void main(String[] args) { Son s = new Son(65, 35, 5);}} class Father extends GrandFather { int father = 40; public int getFather() { return father;} public int getSon() { return -3;} public Father(int grandy, int father) { super(grandy); System.out.println("Grandy: "+this.grandy+ " Father: "+getFather() + " Son:"+getSon()); this.father = father; System.out.println("Grandy: "+this.grandy+ " Father: "+this.father);}}

Class Instantiation: Example 3 class Super { Super() { printThree(); } void printThree() { System.out.println("three"); } class Test extends Super { int three = (int)Math.PI; // That is, 3 public static void main(String[] args) { Test t = new Test(); t.printThree(); } void printThree() { System.out.println(three); }

1 //Assuming you put your answer at this line 2 class Super { 3 int x=10; 4 Super() { 5 this(9); 6 printThree(); 7 x=20; 8 } 9 10 Super (int x) { 11 printThree(); 12 this.x= x; 13 } 14 void printThree() { 15 System.out.println( "three"); 16 } 17 } 18 class Test extends Super { 19 int three = (int)Math.PI; 20 public static void main(String[] args) { 21 Test t = new Test(); 22 t.printThree(); 23 } 24 void printThree() { 25 System.out.println( "three = "+three+ " x= "+x); 26 } 27 }

Drill Exercises 1.Write a program to show that each loaded type has only one instance of java.lang.Class associated with it, irrespective of how many times it is used in a class. 2.Write a program to show that Loading may be caused either by creating an instance of a class or by accessing a static member. 3.Write a class to show that class variables are initialized with their default values when loaded. Also show that instance variables are initialized with their default values when an object is created. 4.Demonstrate that Verification actually takes place in the Loading process. To do this, write a class, Base, and a class, SubClass, that extends Base. Compile both classes. Then modify Base by changing the signature of one of its methods and compile it alone. Now write a test program that uses Subclass and try to compile and run it.