Using Literals

Literals are used to create values that are assigned to variables, used in expressions, or passed to methods. You need to know the correct ways of creating integer, floating-point, character, string, and boolean literals.

Numeric Literals

Literal numbers can appear in Java programs in base ten, hexadecimal, and octal forms, as shown in the following sample code statements, which combine declaring a variable and initializing it to a value:

1.  int n = 42 ;
2.  long j =  4096L ; // appending L or l makes it a long
3.  long k =  0xFFFFFFL ;
4.  byte b2 =  010 ;  // an octal literal
5.   double f2 = 1.023 ;  // double is assumed
6.   float f2 = 1.023F ;   // F or f makes it a float

Notice that an unmodified integer value is assumed to be the 32-bit ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.