0% found this document useful (0 votes)
15 views

Data Type Conversion

This document discusses data type conversion in Java, including implicit and explicit conversion. It provides examples of automatic (implicit) conversion between primitive data types of different sizes. Implicit conversion occurs from lower to higher size types but explicit conversion is needed in the opposite direction. The document also discusses object references and how implicit conversion applies between subclasses and parent classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Type Conversion

This document discusses data type conversion in Java, including implicit and explicit conversion. It provides examples of automatic (implicit) conversion between primitive data types of different sizes. Implicit conversion occurs from lower to higher size types but explicit conversion is needed in the opposite direction. The document also discusses object references and how implicit conversion applies between subclasses and parent classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented Programming & Java

Data Type Conversion

by
Mr. Santanu Basak
Assistant Professor, Department of Computer Science &
Engineering,
University of Engineering & Management, Jaipur
Data Type Conversion
• Implicit Conversion
– Automatic Conversion
• Explicit Conversion
– Conversion by typecasting
Data Type Conversion : Implicit conversion
Lower size data type to higher size data type:
byte -> short -> int -> long -> float -> double

● byte is converted into


○ short, int, long, float, double
● short is converted into
○ int, long, float, double
● int is converted into
○ long, float, double
● float is converted into
○ double
Data Type Conversion : Implicit conversion
In opposite direction we use explicit conversion and that
may cause data loss

double -> float -> long -> int -> short -> byte
Data Type Conversion : Implicit conversion
int i=10;
double d=10.11;

● Automatic Conversion int to double


○ d=i;
● Causes Error
○ i=d
● Explicit Conversion - Lossy Conversion
○ i=(int)d
Automatic Conversion : Uses
• To resolve call, if no matching method found
– If we are passing values with lower size data
type to a method, but that method accepts
values with higher size data type, then data
type is converted automatically (implicitly)
Automatic Conversion
class A { Output:
void play(int a){ int
System.out.println("int"); double
} double
void play(double a){
System.out.println("double"); Explanation:
} a.play(10) calls play(int)
} a.play(10.10) calls play(double)
class Test { a.play(10.10f) calls play(double)
public static void main(String args[]){
A a=new A();
a.play(10);
a.play(10.10);
a.play(10.10f);
}
}
Automatic Conversion
class Cal { Output:
int add(int a, int b){ 30
return a+b; 30.3
} 30.7
double add(double a, double b){ Explanation:
return a+b; c.add(10, 20) calls add(int, int)
} c.add(10.1f, 20.2f) calls add(double, double)
} c.add(10.1, 20.2) calls add(double, double)
class MyClass {
public static void main(String args[]){

Cal c=new Cal();

System.out.println(c.add(10, 20));
System.out.println(c.add(10.1f, 20.2f));
System.out.println(c.add(10.3, 20.4));
}
}
Automatic Conversion
class A { Output:
void play(int a, double b){ int, double
System.out.println("int, double"); double, int
}
void play(double a, int b){ Explanation:
System.out.println("double, int"); a.play(5, 7.1) calls play(int a, double b)
} a.play(8.1, 9) calls play(double a, int b)
}
class MyClass {
public static void main(String args[]){
A a=new A();

a.play(5, 7.1);
a.play(8.1, 9);
}
}
Automatic Conversion : Ambiguous
class A { Explanation:
void play(int a, double b){ • Both method play(int, double) in A and
System.out.println("int, double"); method play(double, int) in A match as
} • a.play(2, 3) can call both methods
– play(int a, double b)
void play(double a, int b){
• int 3 can be converted into double 3
System.out.println("double, int"); – play(double a, int b)
} • int 2 can be converted into double 2
} • So it calling is ambiguous
class MyClass {
public static void main(String args[]){
A a=new A();

//Causes Error
a.play(2, 3);
}
}
Object as Parameter
class Cal { class MyClass2 {
int a,b; public static void main(String args[]){
Cal(){ Cal c1=new Cal();
a=0;
Cal c2=new Cal(10,20);
b=0;
Cal c3=new Cal(10,20);
}
Cal(int i, int j) {
a=i;
System.out.println(c1.equal(c2));
b=j; System.out.println(c2.equal(c3));
}
boolean equal(Cal c) { }
if(a==c.a && b==c.b) }
return true;
else
return false;
}
}
Object as Return type
class Cal { class MyClass2 {
int a; public static void main(String
args[]){
Cal(int i) { Cal c2=new Cal(10);
a=i; Cal c3;
}
c3=c2.incr();
Cal incr() {
System.out.println(c3.a);
Cal c=new Cal(a+1);
}
return c;
} }
}
Automatic Conversion with Objects
class A { Output:
int i=0; 0
} 0
class B extends A {
int i=1;
}
class Test{
void display(A a){
System.out.println(a.i);
}
public static void main(String args[]){
A a=new A();
B b=new B();
Test t=new Test();
t.display(a);
t.display(b);
}
}
Automatic Conversion with Objects
class A { Output:
void play(){
System.out.println("play of A"); play of A
} play of B
}
class B extends A {
void play(){
System.out.println("play of B");
}
}
class Test{
void display(A obj){
obj.play();
}
public static void main(String args[]){
A a=new A();
B b=new B();
Test t=new Test();
t.display(a);
t.display(b);
}
}
Reference/s

Java
The Complete Reference
by
Herbert Schildt

You might also like