Methods
β Definitionβ
- A method provides the functionality / operation / behavior of an object
Syntax of a Methodβ
[modifiers]<return Type><methodName>(params) {
// Method implementation
}
β Notes:β
modifiersβ optional (public, private, static, etc.)returnTypeβ mandatorymethodNameβ mandatoryparamsβ optional
Types of Methods
- Instance Methods
- Static Methods
1) Instance Methodsβ
β Methods defined inside a class without using static modifier
class Hello {
void m1() {
// instance method
}
}
2) Static Methodsβ
β Methods defined inside a class using static modifier
class Hello {
static void m2() {
// static method
}
}
Example (Instance vs Static Method)
class Hello {
void m1() {}
static void m2() {}
}
Method Callsβ
β Using Object Referenceβ
Hello h=new Hello();
h.m1();// VALID (instance method)
h.m2();// VALID (static method)
Using Class Nameβ
Hello.m1();// INVALID (instance method)
Hello.m2();// VALID (static method)
β Using Null Referenceβ
Hello h=null;
h.m1();// INVALID (instance method β runtime error)
h.m2();// VALID (static method)
Key Rules
β Instance Methodsβ
- Must be called using object reference
- Cannot be accessed using class name
β Static Methodsβ
Can be called in 3 ways:
-
Using class name
Hello.m2(); -
Using reference variable (even if null)
Hello h=null;h.m2(); -
Using object reference
Hello h=new Hello();h.m2();
π§ Important Concept
π Static methods belong to class, not object
π Instance methods belong to object
β‘ Interview Quick Points
β Can we call static method using null reference?
π Yes
β Can we call instance method using class name?
π No
β Can static method be called using object?
π Yes (but not recommended)
Method Invocation Examples
Lab347.javaβ
class Lab347 {
public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}
class Hello {
void show() {
System.out.println("show()");
}
}
Output:β
show()
β Instance method called using object β Valid
Lab348.javaβ
class Lab348 {
public static void main(String[] args) {
Hello.show();
}
}
class Hello {
void show() {
System.out.println("show()");
}
}
Result:β
- Compilation Error
β Instance method cannot be called using class name
Lab349.javaβ
class Lab349 {
public static void main(String[] args) {
Hello h = null;
h.show();
}
}
class Hello {
void show() {
System.out.println("show()");
}
}
Result:β
- Runtime Exception (NullPointerException)
β Instance method needs object β null reference fails
Lab350.javaβ
class Lab350 {
public static void main(String[] args) {
Hello.show();
}
}
class Hello {
static void show() {
System.out.println("show()");
}
}
Output:β
show()
β Static method called using class name β Valid
Lab351.javaβ
class Lab351 {
public static void main(String[] args) {
Hello h = null;
h.show();
}
}
class Hello {
static void show() {
System.out.println("show()");
}
}
Output:β
show()
β Static method works even with null reference
Lab352.javaβ
class Lab352 {
public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}
class Hello {
static void show() {
System.out.println("show()");
}
}
Output:β
show()
β Static method can be called using object (but not recommended)
Summary of Concepts
| Scenario | Result |
|---|---|
| Instance method via object | Valid |
| Instance method via class name | Compile error |
| Instance method via null | Runtime error |
| Static method via class name | Valid |
| Static method via null reference | Valid |
| Static method via object | Valid |
π§ Important Understanding
π Instance method β requires actual object
π Static method β belongs to class, not object
β‘ Interview Traps
β h.show() where h = null
- Works only if method is static
β ClassName.method()
- Works only if method is static
Method Examples (Continuation)
Lab353.javaβ
class Lab353 {
public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}
class Hello {
int a;
static int b;
void show() {
System.out.println(a);
System.out.println(b);
}
}
Output:β
0
0
β Instance method can access:
- Instance variables (
a) - Static variables (
b)
Lab354.javaβ
class Lab354 {
public static void main(String[] args) {
Hello.show();
}
}
class Hello {
int a;
static int b;
static void show() {
System.out.println(a);
System.out.println(b);
}
}
Result:β
- Compilation Error
β Static method cannot access instance variable directly
Lab355.javaβ
class Lab355 {
public static void main(String[] args) {
Hello.show();
}
}
class Hello {
static int b;
static void show() {
System.out.println(b);
}
}
Output:β
0
β Static method can access static variables
3.4.5.1 Methods Return Type
β Key Conceptsβ
1) Method Execution Resultβ
- When a method performs an operation:
- It may or may not produce a result
2) Ways to Use Method Resultβ
β Two ways:
- Use the result inside the method
- Return the result to the caller
3) Returning a Valueβ
π If you want to return a value:
- You must specify a return type
Examples:
int
boolean
String
4) No Return Valueβ
π If you donβt want to return anything:
- Use:
void
Key Rules Summary
β Instance method β can access both instance & static variables
β Static method β can access only static variables directly
β Return type:
- Required when returning value
voidwhen nothing is returned
π§ Interview Quick Points
π Can static method access instance variable?
No (without object)
π Can instance method access static variable?
β Yes
π What is default value of int?
β 0
π What if no return value?
β Use void
Method Return Type β Examples
Lab356.javaβ
class Lab356 {
public static void main(String[] args) {
Student st = new Student();
st.show();
}
}
class Student {
show() {
// INVALID
System.out.println("show()");
}
}
Result:β
- Compilation Error
β Method must have a return type
Lab357.javaβ
class Lab357 {
public static void main(String[] args) {
Student st = new Student();
st.setAge(-12);
System.out.println(st.age);
}
}
class Student {
int age;
void setAge(int age) {
this.age = age;
}
}
Output:β
-12
β void method β does not return value
β It updates instance variable
Lab358.javaβ
class Lab358 {
public static void main(String[] args) {
Student st = new Student();
st.setAge(-12);
System.out.println(st.age);
}
}
class Student {
int age = 18;
void setAge(int age) {
if (age < 18) return;
this.age = age;
}
}
Output:β
18
β return; β exits method early
β Age remains unchanged
Lab359.javaβ
class Lab359 {
public static void main(String[] args) {
Student st = new Student();
st.setAge(-12);
System.out.println(st.age);
}
}
class Student {
int age = 18;
void setAge(int age) {
if (age < 18) return 0; // INVALID
this.age = age;
}
}
Result:β
- Compilation Error
β Cannot return value in void method
Lab360.javaβ
class Lab360 {
public static void main(String[] args) {
Hello h = new Hello();
int a = h.show(10);
System.out.println(a);
}
}
class Hello {
int show(int x) {
return x + 1;
}
}
Output:β
11
β Method returns value β assigned to variable
Lab361.javaβ
class Lab361 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
int show(int x) {
return x + 1;
}
}
Output:β
11
β Returned value used directly
Key Concepts
β Return Type Rulesβ
| Case | Result |
|---|---|
| No return type | Compile error |
void method with return; | Valid |
void method with return value; | Error |
| Non-void method without return | Error |
β return Statementβ
return;β exits method (void)return value;β returns value (non-void)
π§ Interview Quick Points
π Is return type mandatory?
β Yes
π Can void method return value?
No
π Can void method use return;?
β Yes
π What happens if return missing in non-void method?
Compile error
Lab362.javaβ
class Lab362 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
System.out.println("Hello Guys");
}
}
class Hello {
int show(int x) {
return x + 1;
}
}
Output:β
Hello Guys
β Return value is ignored
β Method executes, but result not used
Lab363.javaβ
class Lab363 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
void show(int x) {
System.out.println("show()");
}
}
Result:β
- Compilation Error
β Cannot use void method inside println()
Lab364.javaβ
class Lab364 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
System.out.println("Hello Guys");
}
}
class Hello {
int show(int x) {
System.out.println("show()");
}
}
Result:β
- Compilation Error
β Missing return statement in non-void method
Lab365.javaβ
class Lab365 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
System.out.println("Hello Guys");
}
}
class Hello {
int show(int x) {
System.out.println("show()");
return;
}
}
Result:β
- Compilation Error
β Cannot use empty return in non-void method
Lab366.javaβ
class Lab366 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
System.out.println("Hello Guys");
}
}
class Hello {
int show(int x) {
System.out.println("show()");
return 12L;
}
}
Result:β
- Compilation Error
β Cannot return long (12L) for int method
Lab367.javaβ
class Lab367 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
long show(int x) {
System.out.println("show()");
return 'A';
}
}
Output:β
show()
65
β 'A' β converted to ASCII value β 65
β char can be promoted to long
Key Takeaways
β Using Return Valuesβ
- You can ignore return value β valid
- You can use return value β valid
β Compile-Time Errorsβ
| Case | Reason |
|---|---|
| Using void in println | No return value |
| Missing return in non-void | Mandatory |
return; in non-void | Invalid |
| Wrong return type | Type mismatch |
β Type Conversion Rulesβ
charβ can convert toint,longlongβ cannot convert toint(without cast)
π§ Interview Quick Points
π Can you ignore a return value?
β Yes
π Can void method be used in expression?
No
π Is return mandatory in non-void method?
β Yes
π Can return type be different?
Must match (or be compatible)
Lab368.javaβ
class Lab368 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
long show(int x) {
System.out.println("show()");
return x + 1;
}
}
Output:β
show()
11
β int β automatically promoted to long
Lab369.javaβ
class Lab369 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
boolean show(int x) {
System.out.println("show()");
return true;
}
}
Output:β
show()
true
β Boolean return works correctly
Lab370.javaβ
class Lab370 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
boolean show(int x) {
System.out.println("show()");
return 0;
}
}
Result:β
- Compilation Error
β Cannot return int for boolean
Lab371.javaβ
class Lab371 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10));
}
}
class Hello {
boolean show(int x) {
System.out.println("show()");
return false;
return false;
}
}
Result:β
- Compilation Error
β Unreachable code (second return never executes)
Lab372.javaβ
class Lab372 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.isDigit('A'));
System.out.println(h.isDigit('8'));
}
}
class Hello {
boolean isDigit(char ch) {
System.out.println("isDigit(): " + ch);
boolean b = ch >= 48 && ch <= 57;
return b;
}
}
Output:β
isDigit(): A
false
isDigit(): 8
true
β ASCII range check for digits (0β9 β 48 to 57)
Lab373.javaβ
class Lab373 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.isDigit('A'));
System.out.println(h.isDigit('8'));
}
}
class Hello {
boolean isDigit(char ch) {
System.out.println("isDigit(): " + ch);
if (ch >= 48 && ch <= 57) return true;
else return false;
}
}
Output:β
isDigit(): A
false
isDigit(): 8
true
β Multiple return statements are allowed β But only one executes per path
Key Takeaways
β Return Type Rulesβ
- Must match method return type
- Implicit widening allowed (
intβlong) - Not allowed:
intβboolean
β Boolean Returnβ
- Only
trueorfalse - Cannot use numeric values
β Return Statementsβ
- Multiple returns allowed β
- But unreachable code not allowed
β Condition-Based Returnβ
Two styles:
return condition;
OR
if(condition) return true;
else return false;
π§ Interview Quick Points
π Can int be returned in boolean method?
No
π Can we have multiple return statements?
β Yes
π What is unreachable code?
Code after return
π Can int return into long?
β Yes (widening)
3.4.5.2 Method Parameters
There are two types of arguments:β
- Formal Arguments
- Actual Arguments
Formal Arguments (Parameters)β
- Arguments specified in the method definition/declaration
- Also called Parameters
Example:β
void show(int a,int b) {
// a, b β Formal Arguments
}
Actual Argumentsβ
- Arguments specified in the method call
Example:β
Hello h=new Hello();
h.show(10,20);// 10, 20 β Actual Arguments
Lab374.javaβ
class Lab374 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show());
}
}
class Hello {
int show(int x) {
System.out.println("show()");
return x + 1;
}
}
Result:β
- Compilation Error
β Method requires 1 parameter, but none provided
Lab375.javaβ
class Lab375 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.show(10, 20));
}
}
class Hello {
int show(int x) {
System.out.println("show()");
return x + 1;
}
}
Result:β
- Compilation Error
β Method expects 1 argument, but 2 provided
Lab376.javaβ
class Lab376 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(12);
}
}
class Hello {
void show(byte x) {
System.out.println("show(byte)");
}
}
Result:β
- Compilation Error
β int (12) cannot be passed to byte parameter directly
β Requires explicit casting
Lab377.javaβ
class Lab377 {
public static void main(String[] args) {
Hello h = new Hello();
byte b = 12;
h.show(b);
}
}
class Hello {
void show(byte x) {
System.out.println("show(byte)");
}
}
Output:β
show(byte)
β Correct type match (byte β byte)
Key Takeaways
β Argument Matching Rulesβ
- Number of arguments must match
- Type of arguments must match
β Type Conversionβ
| Case | Result |
|---|---|
| byte β int | β Allowed |
| int β byte | Not allowed (needs cast) |
β Errors You Must Rememberβ
- Missing arguments
- Extra arguments
- Wrong data type
π§ Interview Quick Points
π What are formal arguments?
β Defined in method
π What are actual arguments?
β Passed during method call
π Can int go into byte parameter?
No (needs casting)
π Must argument count match?
β Yes
Lab378.javaβ
class Lab378 {
public static void main(String[] args) {
Hello h = new Hello();
h.show((byte) 12);
}
}
class Hello {
void show(byte x) {
System.out.println("show(byte)");
}
}
Output:β
show(byte)
β Explicit type casting (int β byte) works
Lab379.javaβ
class Lab379 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(65);
}
}
class Hello {
void show(char x) {
System.out.println("show(char)");
}
}
Result:β
- Compilation Error
β int cannot be automatically converted to char
Lab380.javaβ
class Lab380 {
public static void main(String[] args) {
Hello h = new Hello();
h.show('A');
}
}
class Hello {
void show(char x) {
System.out.println("show(char)");
}
}
Output:β
show(char)
β Direct char value works
Lab381.javaβ
class Lab381 {
public static void main(String[] args) {
Hello h = new Hello();
h.show((char) 65);
}
}
class Hello {
void show(char x) {
System.out.println("show(char)");
}
}
Output:β
show(char)
β Explicit casting (int β char) works
3.4.5.3 Method Overloading
Definitionβ
You can define multiple methods in the same class with the same name but different parameters.
π This is called METHOD OVERLOADING
Rules of Method Overloadingβ
β Must:β
- Method name should be same
β Must change parameters:β
- Number of parameters
- Type of parameters
- Order of parameters
β Important:β
- Return type alone is NOT enough to overload a method
Lab382.javaβ
class Lab382 {
public static void main(String[] args) {
Hello h = new Hello();
int a = h.add(10, 20);
System.out.println(a);
h.add(10, 20);
}
}
class Hello {
int add(int a, int b) {
System.out.println("add(int,int)");
return a + b;
}
void add(int a, int b) {
System.out.println("add(int,int)");
}
}
Result:β
- Compilation Error
β Duplicate method
β Only return type is different β NOT valid overloading
Key Takeaways
β Type Castingβ
| Case | Result |
|---|---|
| int β byte | (needs cast) |
| int β char | (needs cast) |
| (byte) / (char) casting | β allowed |
β Method Overloading Rulesβ
- Same method name β
- Different parameters β
- Only return type change
π§ Interview Quick Points
π Can we overload methods with only return type?
No
π What defines method signature?
β Method name + parameters
π Is casting required for narrowing?
β Yes
π Is char compatible with int?
β Only with explicit cast
Lab383.javaβ
class Lab383 {
public static void main(String[] args) {
Hello h = new Hello();
int a = h.add(10, 20);
System.out.println(a);
int b = h.add(10, 20, 30);
System.out.println(b);
}
}
class Hello {
int add(int a, int b) {
System.out.println("add(int,int)");
return a + b;
}
int add(int a, int b, int c) {
System.out.println("add(int,int,int)");
return a + b + c;
}
}
Output:β
add(int,int)
30
add(int,int,int)
60
β Overloading by number of parameters
Lab384.javaβ
class Lab384 {
public static void main(String[] args) {
Hello h = new Hello();
int a = h.add(10, 20);
System.out.println(a);
String b = h.add("JavaWorld", 99);
System.out.println(b);
}
}
class Hello {
int add(int a, int b) {
System.out.println("add(int,int)");
return a + b;
}
String add(String a, int b) {
System.out.println("add(String,int)");
return a + b;
}
}
Output:β
add(int,int)
30
add(String,int)
JavaWorld99
β Overloading by type of parameters
Lab385.javaβ
class Lab385 {
public static void main(String[] args) {
Hello h = new Hello();
String a = h.add(99, "JavaWorld");
System.out.println(a);
String b = h.add("JavaWorld", 99);
System.out.println(b);
}
}
class Hello {
String add(int b, String a) {
System.out.println("add(int,String)");
return a + b;
}
String add(String a, int b) {
System.out.println("add(String,int)");
return a + b;
}
}
Output:β
add(int,String)
JavaWorld99
add(String,int)
JavaWorld99
β Overloading by order of parameters
Lab386.javaβ
class Lab386 {
public static void main(String[] args) {
Hello h = new Hello();
byte b = 20;
h.add(10, b);
h.add(b, b);
}
}
class Hello {
void add(int a, byte b) {
System.out.println("add(int,byte)");
}
}
Output:β
add(int,byte)
add(int,byte)
β byte β int (widening) works
β Both calls resolve to same method
Lab387.javaβ
class Lab387 {
public static void main(String[] args) {
Hello h = new Hello();
byte b = 20;
h.add(b, 10);
h.add(b, b);
}
}
class Hello {
void add(byte a, int b) {
System.out.println("add(byte,int)");
}
}
Result:β
- Compilation Error
β h.add(b, b) fails
β Because second byte cannot automatically match int in this context (no exact match)
Key Takeaways
β Method Overloading Variationsβ
| Variation | Example |
|---|---|
| Number of params | add(a,b) vs add(a,b,c) |
| Type of params | add(int,int) vs add(String,int) |
| Order of params | add(int,String) vs add(String,int) |
β Type Conversion Priorityβ
- Exact match
- Widening (byte β int)
- Otherwise β Error
β Important Rulesβ
- Compiler chooses best match
- Automatic widening works β
- Narrowing needs casting
π§ Interview Quick Points
π How does Java choose overloaded method?
β Best match (exact > widening)
π Can order change method signature?
β Yes
π Is byte β int allowed?
β Yes
π Is int β byte allowed automatically?
No
Lab388.javaβ
class Lab388 {
public static void main(String[] args) {
Hello h = new Hello();
byte b = 20;
h.add(b, 10);
h.add(10, b);
}
}
class Hello {
void add(byte a, int b) {
System.out.println("add(byte,int)");
}
void add(int a, byte b) {
System.out.println("add(int,byte)");
}
}
Output:β
add(byte,int)
add(int,byte)
β Method selected based on parameter order & type
Lab389.javaβ
class Lab389 {
public static void main(String[] args) {
Hello h = new Hello();
byte b = 20;
h.add(b, b);
}
}
class Hello {
void add(byte a, int b) {
System.out.println("add(byte,int)");
}
void add(int a, byte b) {
System.out.println("add(int,byte)");
}
}
Result:β
- Compilation Error (Ambiguity)
β Both methods are possible after widening
β Compiler cannot decide β ambiguous call
Lab390.javaβ
class Lab390 {
public static void main(String[] args) {
Hello h=new Hello();
h.show(null);
h.show("JavaWorld");
h.show(h);
}
}
class Hello {
void show(String str) {
System.out.println("show(String)");
}
void show(Objectstr) {
System.out.println("show(Object)");
}
}
Output:β
show(String)
show(String)
show(Object)
β String is more specific than Object
β null prefers most specific reference type
Lab391.javaβ
class Lab391 {
public static void main(String[] args) {
Hello h=new Hello();
h.show("JavaWorld");
h.show(h);
}
}
class Hello {
void show(String str) {
System.out.println("show(String)");
}
void show(Hellostr) {
System.out.println("show(Hello)");
}
}
Output:β
show(String)
show(Hello)
β Exact type match is chosen
Lab392.javaβ
class Lab392 {
public static void main(String[] args) {
Hello h=new Hello();
h.show(null);
}
}
class Hello {
void show(String str) {
System.out.println("show(String)");
}
void show(Hellostr) {
System.out.println("show(Hello)");
}
}
Result:β
- Compilation Error (Ambiguity)
β null matches both String and Hello
β No clear "most specific" β ambiguity
Key Takeaways
β Ambiguity in Overloadingβ
Occurs when:
- Multiple methods match equally well
- Compiler cannot choose best method
β Method Selection Priorityβ
- Exact match
- Most specific type
- Widening
β Special Case: nullβ
| Scenario | Result |
|---|---|
| String vs Object | String chosen β |
| Two unrelated types | Ambiguity |
π§ Interview Quick Points
π What is ambiguity in Java?
β When compiler can't decide method
π Which is more specific: String or Object?
β String
π What happens with null?
β Chooses most specific OR gives error
π Can ambiguity happen with primitives?
β Yes (due to widening)
3.4.5.4 Recursion
β Recursion is the process of calling one method from itself.
Lab393.javaβ
class C {
void m3(int x) {
System.out.println("m3 Begins :" + x);
System.out.println("m3 Ends :" + x);
}
}
class B {
void m2(int x) {
System.out.println("m2 Begins :" + x);
new C().m3(x + 10);
System.out.println("m2 Ends :" + x);
}
}
class A {
void m1(int x) {
System.out.println("m1 Begins :" + x);
new B().m2(x + 10);
System.out.println("m1 Ends :" + x);
}
}
class Lab393 {
public static void main(String[] args) {
int x = 10;
System.out.println("main begins :" + x);
new A().m1(x + 10);
System.out.println("main ends :" + x);
}
}
Execution Flow (Call Stack Explanation)
Step-by-step calls:β
-
main()
x = 10calls m1(20) -
m1(20)
calls m2(30) -
m2(30)
calls m3(40) -
m3(40)
executes and return s -
Control returns back:
m3 β m2 β m1 β main
π§Ύ Output
main begins :10
m1 Begins :20
m2 Begins :30
m3 Begins :40
m3 Ends :40
m2 Ends :30
m1 Ends :20
main ends :10
π§ Call Stack Visualization
| Method | Value of x |
|---|---|
| main | 10 |
| m1 | 20 |
| m2 | 30 |
| m3 | 40 |
π Stack grows top-down (calls)
π Stack shrinks bottom-up (returns)
Key Concepts
β Each method call creates a new stack frame
β Execution is Last-In-First-Out (LIFO)
β Control always returns to the caller
β Recursion uses the same stack behavior
β οΈ Important Note
π This example shows method chaining, not true recursion
(True recursion = method calling itself)
Example of real recursion:
void m(int x) {
if (x == 0) return;
System.out.println(x);
m(x - 1);
}
Lab394.javaβ
class Hello {
void m1(int x) {
System.out.println("m1 Begins :" + x);
if (x != 0) m1(x / 10);
System.out.println("m1 Ends :" + x);
}
}
class Lab394 {
public static void main(String[] args) {
int x = 98;
System.out.println("main begins :" + x);
new Hello().m1(x);
System.out.println("main ends :" + x);
}
}
Execution Flow
Initial call:β
main β m1(98)
Recursive calls:β
m1(98)
β m1(9)
β m1(0)
Return phase:β
m1(0) return s
m1(9) return s
m1(98) return s
main ends
π§Ύ Output
main begins :98
m1 Begins :98
m1 Begins :9
m1 Begins :0
m1 Ends :0
m1 Ends :9
m1 Ends :98
main ends :98
π§ Stack Behavior
Call Stack (Top β Bottom during execution)β
| Method Call | Value of x |
|---|---|
| m1 | 0 |
| m1 | 9 |
| m1 | 98 |
| main | 98 |
β Calls go downward (stack grows)
β Returns go upward (stack shrinks)
Key Concept
β Base Conditionβ
if (x!=0)
- Stops recursion when
x == 0
β Recursive Stepβ
m1(x/10);
- Reduces problem size (98 β 9 β 0)
β οΈ Important Note
π If the condition was missing:
m1(x/10);
It would cause StackOverflowError
π‘ Insight
This program:
- Breaks a number digit by digit
- Shows how recursion unwinds (reverse order execution)
Lab395.javaβ
class Hello {
long factorial(int n) {
if (n == 0 || n == 1) return 1;
else return n * factorial(n - 1);
}
}
class Lab395 {
public static void main(String[] args) {
Hello h = new Hello();
int n = 4;
long fac = h.factorial(n);
System.out.println("Factorial is : " + fac);
}
}
Execution Flow (Recursive Calls)
Calls:β
factorial(4)
β 4 * factorial(3)
β 3 * factorial(2)
β 2 * factorial(1)
β 1 (base case)
π Return Phase (Unwinding)
factorial(1) = 1
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24
π§Ύ Output
Factorial is : 24
π§ Call Stack Visualization
| Call Level | n value |
|---|---|
| factorial | 4 |
| factorial | 3 |
| factorial | 2 |
| factorial | 1 |
β Stack grows while calling
β Stack shrinks while returning
Key Concepts
β Base Conditionβ
if (n==0||n==1)
return1;
β Recursive Caseβ
return n*factorial(n-1);
β οΈ Important Notes
- Without base condition β StackOverflowError
- Each recursive call creates a new stack frame
- Execution follows LIFO (Last In First Out)
π‘ Insight
π This is a classic recursion problem used in:
- Interviews
- Algorithm design
- Understanding stack behavior
3.4.5.5 Call by Value / Call by Reference
πΉ Call by Valueβ
β When you invoke a method by passing primitive data types, it is called CALL BY VALUE.
β In call by value:
- Changes made inside the called method will NOT affect the original value in the caller method.
Lab396.javaβ
class Lab396 {
public static void main(String[] args) {
int a = 99;
Hello h = new Hello();
System.out.println("main begin : " + a);
h.m1(a);
System.out.println("main ends : " + a);
}
}
class Hello {
void m1(int a) {
System.out.println("m1 begin : " + a);
a = a + 10;
System.out.println("m1 ends : " + a);
}
}
Outputβ
main begin : 99
m1 begin : 99
m1 ends : 109
main ends : 99
π Original value not changed β because of call by value
πΉ Call by Referenceβ
β When you invoke a method by passing reference types (objects), it is called CALL BY REFERENCE (conceptually in Java).
β Behavior:β
- Modifying object properties
- Changes ARE reflected in caller
- Modifying reference itself
- Changes ARE NOT reflected in caller
Lab397.javaβ
class Lab397 {
public static void main(String[] args) {
Hai hai=new Hai();
hai.a=99;
Hello hello=new Hello();
System.out.println("main begin : "+hai.a);
hello.m1(hai);
System.out.println("main ends : "+hai.a);
}
}
class Hello {
void m1(Haihai) {
System.out.println("m1 begin : "+hai.a);
hai.a=hai.a+10;
System.out.println("m1 ends : "+hai.a);
}
}
class Hai {
int a;
}
Outputβ
main begin : 99
m1 begin : 99
m1 ends : 109
main ends : 109
π Object property changed globally β behaves like reference sharing
π§ Important Interview Insight
π Java is strictly Call by Value
- For primitives β value copied
- For objects β reference value copied (not actual object)
β Thatβs why:
- You can modify object state
- But cannot change the reference itself
Quick Comparison
| Feature | Call by Value | Call by Reference (Java behavior) |
|---|---|---|
| Data Type | Primitive | Object |
| Value Change | Not reflected | Reflected |
| Reference Change | Not applicable | Not reflected |
Lab398.javaβ
class Lab398 {
public static void main(String[] args) {
Hai hai=new Hai();
hai.a=99;
Hello hello=new Hello();
System.out.println("main begin : "+hai.a);
hello.m1(hai);
System.out.println("main ends : "+hai.a);
}
}
class Hello {
void m1(Haihai) {
System.out.println("m1 begin : "+hai.a);
hai=new Hai();// new object created
hai.a=hai.a+10;// default value 0 + 10 = 10
System.out.println("m1 ends : "+hai.a);
}
}
class Hai {
int a;
}
Outputβ
main begin : 99
m1 begin : 99
m1 ends : 10
main ends : 99
π§ Concept Explainedβ
π Inside method:
hai=new Hai();
- A new object is created
- Reference is changed locally only
π So:
- Original object in
main()remains unchanged - Changes inside method are NOT reflected
β This proves:
Java passes reference value (copy), not actual reference
πΉ 3.4.5.6 Var-Args (Variable Arguments)
β What is Var-Args?β
- Introduced in Java 5
- Used when a method needs to accept multiple values of same type
π Before Var-Args (Traditional Approaches)β
1. Method Overloadingβ
class Hello {
void sum(int a,int b) { }
void sum(int a,int b,int c) { }
void sum(int a,int b,int c,int d) { }
}
2. Using Arrayβ
class Hello {
void sum(int[]arr) { }
}
π These approaches become complex and less flexible
β Using Var-Argsβ
β Advantage:β
- Accepts any number of arguments
- Cleaner and flexible
π§Ύ Syntaxβ
<return Type><methodName>(<dataType>fixedArg,<dataType>...varArgs) {
// method body
}
Examplesβ
void sum(int...values) { }
void sum(String str,int...values) { }
Key Rulesβ
β Var-args must be last parameter
β Only one var-args allowed per method
β Internally treated as an array
π‘ Example Usageβ
void sum(int... values) {
int total = 0;
for (int v : values) {
total += v;
}
System.out.println(total);
}
Call:
sum(10,20);
sum(10,20,30,40);
β οΈ Important Insight
π Var-args = syntactic sugar over arrays
sum(1,2,3)
π internally becomes:
sum(new int[]{1,2,3})
Lab399.javaβ
class Lab399 {
public static void main(String[] args) {
Hello h = new Hello();
h.sum(12, 23);
}
}
class Hello {
void sum(int a, int b) {
System.out.println("-- sum(int,int) --");
System.out.println(a + b);
}
}
Outputβ
-- sum(int,int) --
35
Lab400.javaβ
class Lab400 {
public static void main(String[] args) {
Hello h = new Hello();
h.sum(12, 23);
h.sum(12, 32, 43);
}
}
class Hello {
void sum(int a, int b) {
System.out.println("-- sum(int,int) --");
System.out.println(a + b);
}
void sum(int a, int b, int c) {
System.out.println("-- sum(int,int,int) --");
System.out.println(a + b + c);
}
}
Lab401.javaβ
class Lab401 {
public static void main(String[] args) {
Hello h = new Hello();
h.sum(new int[0]);
h.sum(new int[] { 12, 34, 54 });
h.sum(new int[] { 10, 20, 50, 30 });
// Not allowed directly:
// h.sum(12, 34, 54);
// h.sum(10, 20, 50, 30);
}
}
class Hello {
void sum(int arr[]) {
System.out.println("-- sum(int[]) --");
System.out.println("Length : " + arr.length);
int s = 0;
for (int a : arr) {
s = s + a;
}
System.out.println("Sum is : " + s);
}
}
Lab402.javaβ
class Lab402 {
public static void main(String[] args) {
Hello h = new Hello();
h.sum(new int[0]);
h.sum(new int[] { 12, 34, 54 });
h.sum(new int[] { 10, 20, 50, 30 });
h.sum(); // allowed
h.sum(12, 34, 54); // allowed
h.sum(10, 20, 50, 30); // allowed
}
}
class Hello {
void sum(int... arr) {
System.out.println("-- sum(int...) --");
System.out.println("Length : " + arr.length);
int s = 0;
for (int a : arr) {
s = s + a;
}
System.out.println("Sum is : " + s);
}
}
π§ Concept Summary
πΉ Array Parameterβ
- Must pass array explicitly
sum(new int[]{1,2,3});
- Cannot pass values directly
πΉ Var-Argsβ
- Flexible input
sum(1,2,3);
sum();
- β Accepts zero or more values
- Internally treated as array
Key Differences
| Feature | Array Parameter | Var-Args |
|---|---|---|
| Syntax | int[] arr | int... arr |
| Call style | Need array | Direct values allowed |
| Flexibility | Less | More |
| Zero arguments | Not allowed | β Allowed |
π‘ Important Insight
π Var-args makes method calls cleaner and more readable
Instead of:
sum(new int[]{10,20,30});
You can write:
sum(10,20,30);
Lab403.javaβ
class Lab403 {
public static void main(String[] args) {
Hello h = new Hello();
h.show();
}
}
class Hello {
void show(int a, int... arr) {
System.out.println("\nshow(int,int...)");
}
}
Lab404.javaβ
class Lab404 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
h.show(10, 20);
h.show(10, 20, 30);
}
}
class Hello {
void show(int a, int... arr) {
System.out.println("\nshow(int,int...)");
}
}
Lab405.javaβ
class Lab405 {
public static void main(String[] args) {
Hello h = new Hello();
h.m1(); // Ambiguity
}
}
class Hello {
void m1(int... a) {
System.out.println("m1(int...)");
}
void m1(String... a) {
System.out.println("m1(String...)");
}
}
β οΈ Result:β
- Compilation Error β Ambiguous method call
- Because no arguments β both methods match
Lab406.javaβ
class Lab406 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
}
}
class Hello {
void show(int... arr1, int... arr2) {
// INVALID
System.out.println("\nshow(int...,int...)");
}
}
Rule:β
- Only one var-args parameter allowed
- It must be last parameter
Lab407.javaβ
class Lab407 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10);
}
}
class Hello {
void show(int... arr1) {
System.out.println("\nshow(int...)");
}
void show(int[] arr1) {
System.out.println("\nshow(int[])");
}
}
β οΈ Result:β
- Compilation Error β Duplicate method
- Because:
int... β‘int[]
π§ Concept Summary
πΉ 1. Var-Args with Fixed Parameterβ
void show(int a,int...arr)
β First argument mandatory
β Remaining optional
πΉ 2. Allowed Callsβ
show(10);
show(10,20);
show(10,20,30);
πΉ 3. Ambiguity Problemβ
m1(int...)
m1(String...)
m1();// ambiguous
πΉ 4. Restrictionsβ
- Only one var-args allowed
- Must be last parameter
- Cannot overload with array of same type
Interview Tips
π int... is internally treated as int[]
π Var-args improves readability but can create ambiguity
π Always avoid overloading with multiple var-args
Lab408.javaβ
class Lab408 {
public static void main(String[] args) {
Hello h = new Hello();
h.show(10); // Cannot pass single int directly
}
}
class Hello {
void show(int[]... arr) {
System.out.println("\nshow(int[]...)");
}
}
Note:β
int[]...means var-args of arrays (2D concept)- You cannot pass a single int
- You must pass arrays
Lab409.javaβ
class Lab409 {
public static void main(String[] args) {
Hello h = new Hello();
h.show();
int arr1[] = new int[] { 10 };
int arr2[] = new int[] { 20, 30, 12 };
h.show(arr1, arr2); // Valid
}
}
class Hello {
void show(int[]... arr) {
System.out.println("\nshow(int[]...)");
}
}
Valid Usage:β
- Passing multiple arrays
- Passing zero arguments also allowed