Java Program - 03
Write a java program to show the concept of type conversion.
public class StudyViral_03 { public static void main(String[] args) { int a = 127; a = a + 1; System.out.println("New Value of A = "+a); byte b = 127; b = b + 1;//Error explicit required System.out.println("New Value of B = "+b); int c = 100; c = c + 1.0;//Error explicit required System.out.println("New Value of C :: "+c); double d = c + 1.0; System.out.println("New Value of D :: "+d); } }
==============================================================
//Correct Program
public class StudyViral_03 { public static void main(String[] args) { int a = 127; a = a + 1; System.out.println("New Value of A = "+a); byte b = 127; b = (byte) (b + 1);//Explicit casting (Forcefully Convert int to byte)
//because 1 is int and variable b is of type byte System.out.println("New Value of B = "+b); int c = 100; c = (int) (c + 1.0);//Explicit casting (Forcefully Convert 1.0 double to int)
//1.0 is double and variable c is of type int System.out.println("New Value of C :: "+c); double d = c + 1.0;//Implicit Type Casting done
//Convert from lower to higher Data Type System.out.println("New Value of D :: "+d); } }
0 comments:
Post a Comment