Skip to content

Instantly share code, notes, and snippets.

@adeelibr
Created August 25, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeelibr/fc344496608b8501f579524269859769 to your computer and use it in GitHub Desktop.
Save adeelibr/fc344496608b8501f579524269859769 to your computer and use it in GitHub Desktop.
Type casting in java widening casting vs narrowing casting
/*
* 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