About byte, int , long, short primitive.
843785Jan 31 2009 — edited Feb 5 2009Hi,
I am new to Java. I was trying to write a program that could test the maximum value of integer data type mentioned above. My code like this:
public class IntegerTest
{
public static void main()
{
byte testbyte = (byte)Math.pow(2,7);
short testshort = (short)Math.pow(2,15);
int testint = (int)Math.pow(2,31);
long testlong = (long)Math.pow(2,63);
System.out.println("Test Byte: " +testbyte);
System.out.println("Test Short: " +testshort);
System.out.println("Test Int: "+testint);
System.out.println("Test long: "+testlong);
}
}
i got output as:
Test Byte: -128
Test Short: -32768
Test Int: 2147483647
Test long: 9223372036854775807
First two output are smallest and last two are largest. I think this is due to type cast. Since Math.pow returns double. Using the same technique i am getting smallest and largest for different types. I am too confused about what's going with the code.
Can you anyone help me out?
Thank you in advance