CHAPTER 3 ■ DATA TYPES
Listing 3-1. String examples
String myString = "my string";
String yourString = "your string";
String ourString = myString + " " + yourString;
System.out.println(myString);
System.out.println(yourString);
System.out.println(ourString);
The output is in Listing 3-2.
Listing 3-2. String example output
my string
your string
my string your string
Notice that the value of ourString consists of the concatenation of three values: myString, " ", and
yourString. " " is a String literal. That's another way String differs from other objects; no other object
(remember, primitives aren't objects) can have a literal.
Literals
All the primitives and the String class can have literal values. A literal is a constant value that
corresponds to a particular data type. Table 3-2 provides examples for each of the primitive data types
and the String class.
Table 3-2. Literal examples
Type Literal
byte int a = -100;
short int b = 1000;
int int c = -10000;
long int d = 100000000000;
float float e = 12.34f;
double double f = -56.78;
boolean boolean iLoveJava = true;
char char aChar = 'a';
String String greeting = "Hi, there!";