Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0013 long

long is a signed 64-bit type and is used when an int type is not large enough. The range of long type is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 For example, here is a program that use long type to store the result. public class Main { public static void main(String args[]) { long result= (long)Integer.MAX_VALUE * (long)10; System.out.println(result);//21474836470 } } The result could not have been held in an int variable. To specify a long literal, you need to tell the compiler that the literal value is of type long by appending an upper- or lowercase L to the literal. For example, 0x7ffffffffffffffL or 123123123123L. public class Main { public static void main(String args[]) { long l = 0x7ffffffffffffffL; System.out.println("l is " + l); } } The output generated by this program is shown here: l is 576460752303423487