Download presentation
Presentation is loading. Please wait.
1
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
2
2 Closer Look at Methods l Methods are the fundamental building blocks of a Java program. l They can be instant methods, usually written to implement a particular behavior of an object – e.g. withdraw method l They can also be class method, usually written to compute some value or perform some action – e.g main method or sqrt method of the Math class. l The general format of a method is as follows: Modifiers ReturnType methodName(Parameters) [Exceptions] { Statement1; Statement2; * StatementN; }
3
3 Closer Look at Methods (cont’d) l A method definition could begin with one or more of the following modifiers: [public | private | protected | ] [ static | ] [ final | ] l The first group are called access modifies as they indicate the type of permission or restriction associated with the method. l The following table indicate the meaning of each. l A method can also be static indicating that it is a class method or non-static, indicating that it is an instance method. l A method may also be final, indicating that it can not be overridden by subclasses. classpackagesub-classworld public protected default private
4
4 Closer Look at Methods (cont’d) Return Type: l The return type indicates the type of value the method returns – int, double, …,or object reference l If no value or reference is returned by a method, the return type is specified as void. l In Java, a method cannot return more than one value. Parameters: l A method may have zero or more parameters. l Parameters are used to receive input from the method caller. l Each parameter is specified by its type and name. l The parameters in a method definition are called formal parameters. l The values used to call the method are called actual parameters. return statement: l If the return type of a method is not void, then there must be at least one return statement that returns the result of the method.
5
5 Methods Invocation An Example: import java.io.*; class MethodInvocation { static double doubleParameter(double num) { double d; d = 2*num; return d; } static double squareParameter (double num){ double sq; sq = num*num; return sq; }
6
6 Methods Invocation public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); double n, d,sq, answer; System.out.println("Enter a number:"); String input = stdin.readLine(); n=Double.parseDouble(input); d = doubleParameter(n); sq = squareParameter(d); System.out.println("Your input is: "+ n); System.out.println("Double of "+n+"is"+d); System.out.println("Square of "+d+"double is "+ sq); }
7
7 Memory Model of JVM l So far we have learnt that to execute a java program, it is first compiled into bytecode, then invoke the JVM which loads and execute it. l However, to have a clearer understanding of how Java programs execute, we need to take a closer look at the memory model of JVM. l The JVM divides the memory into three main areas, as shown by the digram below: Method Area Stack Heap
8
8 Memory Model of JVM (cont’d) l When a class file is loaded, the JVM extracts the following information and store them in the method area: »The bytecode for each method in the class »The class variables (static variables) »Class information – modifies, etc »Methods’ information – modifies, return type, etc The Heap l This is where objects are created. Memory is allocated for each of the instance variables of the object. l Also a reference to the method area containing the code for the methods of the object is stored with the data for the object. `
9
9 Memory Model of JVM (cont’d) Stack l At any particular point in the life-time of an application, only one method can execute. Such method is called current method. l A method may call another method, which may in turn call another, etc. l To keep track of the order from one method call to another, the stack memory area is used. l This is achieved by creating a stack frame for the current method in the stack. l The stack frame (also called activation record) contain the following information: l Local variables and parameters of the method l References to objects created by the method l Return address and return value of the method l If the current method calls another method, another frame is created for that method and push onto the stack – that method then becomes the current. l On exit from the current method, its frame is popped off the stack and discarded l Thus the value of local variables exists in memory only as long as the method executes.
10
10 Memory Model of JVM (cont’d) ‘
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.