Java Reference
In-Depth Information
A
String
object is created using the
new
operator. However, strings are used so often that there is a shortcut to
create a string object. All string literals, a sequence of characters enclosed in double quotes, are treated as
String
objects. Therefore, instead of using the
new
operator to create a
String
object, you can use string literals like so:
// Assigns the reference of a String object with text "Hello" to str1
String str1 = "Hello";
// Assigns the reference of a String object with text "Hello" to str1
String str1 = new String ("Hello");
■
there is a subtle difference between the above two statements, which assign a
String
object to
str1
with the
same text
"Hello"
. I will discuss the difference when I cover the
String
class in detail Chapter 11.
Tip
Primitive Data Types in Java
Java has eight primitive data types. They are
byte
,
short
,
int
,
long
,
char
,
float
,
double
, and
boolean
. They are divided
into two categories: boolean data type and numeric data type. The numeric data type can be further subdivided into
integral and floating-point types. All primitive data types and their categories are shown in Figure
3-4
.
Figure 3-4.
List of primitive data types in Java
Integral Data Types
An integral data type is a numeric data type whose values are integers. Java offers five integral data types:
byte
,
short
,
int
,
long
, and
char
. All integral data types are described in detail in the sections to follow.