Created
August 25, 2017 20:32
-
-
Save adeelibr/fc344496608b8501f579524269859769 to your computer and use it in GitHub Desktop.
Type casting in java widening casting vs narrowing casting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package lab2TypeCasting; | |
/** | |
* | |
* @author Adeel | |
*/ | |
public class TypeCasting { | |
public static void main(String[] args) { | |
/* | |
* | |
* Widening Casting | |
* | |
*/ | |
byte b = 4; | |
short s = b; | |
System.out.println(s); | |
int i = s; | |
System.out.println(i); | |
long l = i; | |
System.out.println(l); | |
float f = l; | |
System.out.println(f); | |
double d = f; | |
System.out.println(d); | |
/* Widening Casting Ends Here */ | |
/* | |
* | |
* Narrowing Casting | |
* | |
*/ | |
f = (float)d; | |
System.out.println("Double to float is: "+f); | |
l = (long)f; | |
System.out.println("Float to long is: "+l); | |
i = (int)l; | |
System.out.println("long to int is: "+i); | |
s = (short)i; | |
System.out.println("int to Short is: "+s); | |
b = (byte)s; | |
System.out.println("Short to byte is: "+b); | |
/* Narrowing Casting Ends Here */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment