Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 outline of a java source file l What happens when one method calls another?

2 2 Memory Model of A Program l What happens when a program is loaded? l A closer look at methods? l What happens when one method calls another? l When a program is loaded the operating system allocates memory for that program and then copies the executable code from disk to that memory area. l The compiler divides allocated memory into s »Code area: whose is size known and fixed before program execution. Considered read-only and only pointers to functions can access this area. »Data area: whose size is also fixed statically. Holds static global variables. »Heap: memory allocated during execution. Its sizes grows and shrinks during execution. »Stack: it is not fixed; changes during program execution in a controlled fashion, I.e., from one end only. Crucial for implementing functions.

3 3 Structure of a Java Source File General Comment for the file Documentation Comment for Package package PackageName import Packages Documentation Comment for ClassName [public] class ClassName extends BaseClass implements Interface1, Interface2 { Documentation Comment for member1 member1; Documentation Comment for member2 member2; * Documentation Comment for memberN memberN; } Other non-public Classes, if any following the above structure for a class

4 4 Main Method Definition public static void main(String[] args) { Statement1; Statement2; * StatementN; } ------------------------------------------------------------------------------- public static void main(String[] args) throws SomeException1, SomeException2 { Statement1; Statement2; * StatementN; }

5 5 Method Definition AccessModifiers ReturnType MethodName(Parameters) { Statement1; Statement2; * StatementN; } -------------------------------------------------------------------------------------- Access Modifiers: [public | private | protected | ] [ static | ] [ final | ] -------------------------------------------------------------------------------------- Parameters: Comma Separated list of: TypeOfParameter NameOfParameter --------------------------------------------------------------------------------------

6 6 Methods: Basic Issues l The returnType of a method indicates the type of value that the method sends back to the calling location. A method that does not return a value (such as main ) has a void return type. l The return statement specifies the value that will be returned. l Every method must be identified by a name. l A parameter list of a method can contain zero or more entities called parameters/arguments. l A method with zero parameters is said to be parameterless (i.e., without any parameters).

7 7 Methods: Basic Issues (cont.) l Each parameter in the parameter list specifies its type and name explicitly and position implicitly. l The parameters in the method definition are called formal parameters. l The values passed to a method when it is invoked are called actual parameters/arguments. l When a parameter is passed, a copy of the value of evaluation in the calling context is made and is then assigned to the formal parameter. l Both primitive types and object references can be passed as parameters. l A method may contain local declarations in addition to executable statements. l Variables declared locally can only be used locally.

8 8 Methods Definition: Simple Examples l A method definition is a description of the actions of the method abstraction. The method showName() is parameterless void showName () { System.out.println(“Name is Ali, age is ”+33); } The method average() below has 3 parameters double average (int m1, int m2, int m3) { int sum; double result; sum = m1+m2+m3; result = sum/3.0; return result; }

9 9 Methods Invocation l Methods are abstractions or named actions that are meant to be invoked whenever the actions that they specify are desired. l Methods invoke other methods, sometime mutually. A method can also invoke itself recursively. Such a method is called recursive. l A method call is an explicit request that the code for the method be executed. l When a method is invoked, relevant information about the function, called its activation record, is copied onto the stack. l The activation record, which is copied only temporarily on to the runtime stack, consists of the method’s »Parameters, Return Address, Local variables, and Return Value. l When execution of the method’s code completes the activation record of the method is removed from the stack; control reverts back to the calling context.

10 10 Methods Invocation An Example: import java.io.*; class MethodInvocation { static TextIO inputStream = new TextIO(System.in); static double doubleParameter( double num) { double d; d = 2*num; return d; } static double squareParameter ( double num){ double sq; sq = num*num; return sq; }

11 11 Methods Invocation Example continued public static void main (String[] args) throws IOException { double n, d,sq, answer; System.out.println("Enter a number:"); n = inputStream.readDouble(); d = doubleParameter(n); sq = squareParameter(d); System.out.println("Your input number is: "+ n); System.out.println("Double of " + n +" is "+ d); System.out.println("Square of " + d + " double is "+ sq); }


Download ppt "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."

Similar presentations


Ads by Google