Home > Articles

  • Print
  • + Share This
This chapter is from the book

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 int primitive, but a value containing a decimal point is assumed to be the 64-bit double, unless you append an F or f to indicate the 32-bit float primitive.

In line 1, an unmodified literal integer is assumed to be in base ten format. In line 3, a number starting with a leading zero followed by an uppercase or lowercase X is interpreted as a hexadecimal number. In line 4, a number with a leading zero and no X is interpreted as an octal number. Appending an uppercase or lowercase L indicates a long integer.

Tricky Literal Assignment Facts

The compiler does a variety of automatic conversions of numeric types in expressions, but in assignment statements, it gets quite picky as a defense against common programmer errors. In the following code, lines 1 and 3 cause a compiler "possible loss of precision" error:

1. int n2 = 4096L ; // would require a specific (int) cast
2. short s1 = 32000 ; // ok 
3. short s2 = 33000 ; // out of range for short primitive 
4. int secPerDay = 24 * 60 * 60 ;

Although 4096 would fit in an int primitive, the compiler would object to line 1 because the literal is in the long format. It would require a special operator called a cast to allow the statement. A Java cast operator takes the form of a type enclosed in parentheses. It is an instruction to the compiler to allow conversion of one variable type to another. Casts are discussed in detail in Chapter 6, "Converting and Casting Primitives and Objects."

The compiler also pays attention to the known range of primitives, passing line 2 in the previous example but objecting to line 3. You could force the compiler to accept line 3 with a specific (short) cast, but the result would be a negative number due to the high bit being set.

In line 4, the compiler pre-computes the resulting value rather than writing code to perform the multiplication. This handy feature lets you write out the factors of a useful number such as secPerDay without any runtime penalty in memory used.

Numeric Wrapper Classes

Each of the primitive data types has a corresponding wrapper class in the Java standard library. Java uses these classes for several purposes. The static variables of a wrapper class hold various constants, and the static methods are used for convenient conversion routines, such as the toString method, which returns a String representing a primitive value. You can also create objects that contain a primitive value, using either literals or primitive variables, as in the following examples:

1. Integer cmd = new Integer( 42 ) ;
2. Boolean flag = new Boolean( false ) ;
3. Character pi = new Character( '\u03c0' ) ;
4. Long lx = new Long( x ) ; // where x is a long variable

The values contained in a wrapper object cannot be changed, so they are not used for computation. Wrapper objects are useful when you want to store primitive values using Java's utility classes, such as Vector, Stack, and Hashtable, that work only with objects. The names of the wrapper classes are Byte, Short, Character, Integer, Long, Float, Double, and Boolean. As you can see, the names reflect the primitive values they contain but start with a capital letter. These wrapper classes are discussed more extensively in Chapter 11, "Standard Library Utility Classes."


The most important point to remember about the wrapper class objects is that the contained value cannot be changed. These objects are said to be immutable.

Character Literals

Even though typical Java code looks as if it is made up of nothing but ASCII characters, you should never forget that Java characters are, in fact, 16-bit Unicode characters. The following code shows legal ways to declare and initialize char type primitive variables:

1. char c1 = '\u0057' ; // the letter W as Unicode
2. char c2 = 'W' ;
3. char c3 = (char) 87 ; // the letter W 
4. char cr = '\r' ; // carriage return

Line 1 illustrates the Unicode representation of a character indicated by the leading \u sequence. The numeric value is always a four-digit hexadecimal number in this format. Line 2 uses single quotation marks surrounding the literal character, and an integer is cast to the char primitive type in line 3. Line 4 shows a literal representing a nonprinting character with an escape sequence.

You can also mix in Unicode characters and special characters with ASCII strings using the \u escape sequence (as shown in the "String Literals" section later in this chapter). Table 3.1 summarizes Java escape sequences that can be used in strings or to initialize character primitives.

Table 3.1 Java Escape Sequences

Escape Sequence

Character Represented




Horizontal tab.


New line (line feed).


Form feed.


Carriage return.


Double quotation mark.


Single quotation mark.




A character in octal representation; xxx must range from 000 through 377.


A Unicode character, where xxxx is a hexadecimal-format number in the range 0000 through FFFF. Note that this is the only escape sequence that represents a 16-bit character.

Special Precautions to Take with Unicode

Java translates Unicode characters as it reads the text, so you can't insert the code for carriage return or line feed characters using Unicode in program source code. The compiler sees the carriage return or line feed as an end-of-line character and reports an error. That is why you must use the escape sequences shown in Table 3.1 to insert these characters in string literals.

String Literals

Because Java does not deal with strings as arrays of bytes, but as objects, the compiler has to do a lot of work behind the scenes. The following sample code shows the declaration and initialization of String variables with literal values enclosed in double quotation marks. Note that unlike some languages, Java does not allow single quotation marks as an alternative for delineating strings. Single quotes are only used for character literal values.

1. String name = "" ; // an empty string, but still an object
2. String type = ".TXT" ;
3. String longtxt = "A great long bunch of text \n" 
4.    + "to illustrate how you break long lines." ; 

At the end of line 3, notice the sequence \n; this is an example of an escape code sequence used to insert special characters—in this case, a line feed. Line 4 illustrates the special meaning of the + operator when used with strings; this is discussed in "String Objects and the + Operator" section later in this chapter. Here is an example of a String literal with the Unicode representation of a capital Greek letter delta inserted between two double quotation mark characters:

String tx = "Delta values are labeled \"\u0394\" on the chart.";


Remember that string literals create String objects, not byte arrays. Most of the things you are used to doing with strings in C will not work in Java.

Boolean Literals

Fortunately, boolean literals are simple. Only the Java reserved words true and false can be used. Note that these words are always all lowercase. If you write

boolean flag = True ;

the compiler goes looking for a boolean variable named True.


C programmers should remember that integer variables can never be interpreted as boolean values. You must leave behind all of your tricks that depend on zero being interpreted as false.

  • + Share This
  • 🔖 Save To Your Account