Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Saturday, 25 June 2011

Java program to cast integer into short and byte data type


import java.util.*;
class casting
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a;
short b;
byte c;

System.out.print ("\n\t Enter the value = ");
a=in.nextInt();

b=(short)a;
c=(byte)a;

System.out.println ("\n\t value of a in short form = "+b);
System.out.println ("\n\t Value of a in byte form= "+c);

}
}

Note:
         In the above program you might have noticed these two statements
  • b=(short)a;
  • c=(byte)a;
these are the actual statements that are casting the data type of a into short and byte.

No comments:

Post a Comment