0% found this document useful (0 votes)
18 views5 pages

Type Promotion

The document discusses method overloading in Java, showcasing how different parameter types can invoke specific methods. It also explains type promotion for various data types and the use of the static keyword for memory management, including its implications for methods and variables. Additionally, it highlights the characteristics of static methods, including their inability to access non-static members directly.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Type Promotion

The document discusses method overloading in Java, showcasing how different parameter types can invoke specific methods. It also explains type promotion for various data types and the use of the static keyword for memory management, including its implications for methods and variables. Additionally, it highlights the characteristics of static methods, including their inability to access non-static members directly.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

class Calculation1{

void sum(int a, long b)


{
System.out.println(a+b);
}

void sum(int a, int b, int c)


{
System.out.println(a+b+c);
}

public static void main(String args[]){

Calculation1 obj=new Calculation1();

obj.sum(20, 20);//now second int literal will be promoted to long

obj.sum(20, 20, 20);


}
Type Promotion  Byte can be promoted to short, int, long, float or double.

 The short datatype can be promoted to int, long, float or


double.

 The char datatype can be promoted to int, long, float or


double.
class Calculation2{
void sum(int a, int b)
{
System.out.println("int arg method invoked");
}

void sum(long a, long b)


{
System.out.println("long arg method invoked");
}

public static void main(String args[]){


Calculation2 obj=new Calculation2();

obj.sum(20, 20);//now int arg sum() method gets invoked


}
}
Static keyword
 The static keyword is used for memory management.
 We can apply static keyword with variables, methods, blocks and nested classes.

class Student{
int rollno; //instance variable public class Test{
String name; public static void main(String args[]){

static String college =“SHARDA"; Student s1 = new Student(111,“Abc");


//static variable Student s2 = new Student(222,“Xyz");

s1.display();
Student(int r, String n){ s2.display();
rollno = r; }
name = n; }
}

void display ()
{
System.out.println(rollno+" "+name+" "+college);
Java static method
•A static method belongs to the class rather than the object of a class.

•A static method can be invoked without creating an instance of a class.

•The static method can not use non-static data members or call non-static methods
directly.

•this and super cannot be used in static context.

int add( int a, int b) {


even(a);
return b;
}

static void even(int a) {


if(a%2==0) {
System.out.println("EVen");
}
else
System.out.println("ODD");
}

You might also like