METHODS

Definition: A Java Method is a collection of statements that are grouped together to perform an operation and executed whenever called or invoked.

(1)Why methods

(2) Re-Usability

Access-level: public, protected, default, private.

Modifier: static or non-static.

Return-type: Primitive data-type or Derived data-type.

Method-name: Is an identifier can be any valid identifier.

arguments: inputs for a method.

Note: Return type and return value should match.

Part-1:

The following process takes place when we execute a program:

1) Program execution happens in RAM.

2) RAM memory is divided into 2 types,

a) Stack: For execution purpose.

b) Heap: For Storage purpose.

3) JVM will be loaded onto stack automatically.

4) JVM will call class loader (Built-in Library).

5) Class Loader will create a static pool in the heap memory and all the static members of the class will be loaded on to it.

Static pool name will be similar to class name.

Part-2:

1) JVM will check the availability of main method in the static pool, if its available, JVM will load the main method for execution purpose.

2) JVM will pass the control to main method so that main method can execute its statement.

Part-3:

1) From main method, some method is called by using static pool name i.e., Classname.someMethod(1);

2) someMethod() will be loaded onto the stack.

3) Control will pass to the someMethod(), so it can executes its statement.

4) someMethod() after its execution will return the control back to main method.

5) someMethod() will be erased from the stack.

6) Main method resume its execution and finally it will return the control back to JVM.

7) Main method will be erased from stack.

8) JVM will call Garbage Collector(Daemon Thread).

9) Garbage Collector will clear the contents from the HEAP memory.

10) JVM will exit from the stack.

Output: 4

Output: Result is: 27

Note:

1) void means return no value but return control.

2) If the method return-type is void, then that method cannot return any value after processing.

3) If any methods return-type is void, then developing return statement is not mandatory.

4) If any methods return-type is void and if the programmer has not developed to return statement then compiler will develop return statement automatically at compile time.

5) Developing methods with arguments is not mandatory i.e., we can develop methods without arguments.

6) The methods without arguments cannot receive any input at the time of method invocation.

Output:

CTE Return type required

Note: Can we develop a method without return type? No, we cannot develop any method without return type, return type is mandatory should be either a primitive data type or Derived Data-type or void, but arguments for a method is not mandatory

Output:

Result is :81

Output:

Result is :90

Output:

90.5

Note: In System.out.println we cannot call the method of type void, which cannot return value. We can call only non-void method.

Output:

Erroneous tree type:

Note: If the methods Return type is void, we cannot call it in “System.out.println” or having the “System.out.println” we can only call those methods which will return some value (non-void method). When we opt for void method? Whenever we are not expecting any return value from the method after processing then we should develop those methods with return type. When we opt for non-void method? Whenever we are expecting the return value from the method and based on that return www.testingbar.com Sujoy@testingbar.com value if we want to do further processing then we should choose non-void method.

Output:

(1)Result is true

(2)Result is false

Output:

Result is :30

Result is :30

Result is :40

Output:

Result is:true

Result is:true

Result is:false

Result is:false

Last updated