There are two kinds of Java data: simple data and objects. Simple java data are boolean, character, integer, and real values. Java objects are encapsulations of various kinds of data components, together with methods for manipulating the components and returning information about them. Methods are like C functions, although the syntax for their use is different.
Simple Java data has one of eight types as listed below.
These types are similar to C and C++.
The main difference is that Java does not have signed
and unsigned
keywords.
All Java integers are signed.
Type | Contains | Size, Coding, or Values |
---|---|---|
boolean | truth value |
true ,
false
|
char | character | Unicode characters |
byte | signed integer | 8 bit two's complement |
short | signed integer | 16 bit two's complement |
int | signed integer | 32 bit two's complement |
long | signed integer | 64 bit two's complement |
float | real number | 32 bit IEEE 754 floating point |
double | real number | 64 bit IEEE 754 floating point |
The simple types of Java data can be used in much the same way as the corresponding types in C. The syntax for literals and expressions is identical except for some additional escape sequences to handle Unicode characters in character and String literals. Java does not have pointers, structs or unions. Their functionality, and more, is provided by Java objects.
With regard to simple data types, the main difference between C and Java
is that Java does not automatically coerce between the integral types,
the boolean type, and the character type.
This implies that Java integral types and characters cannot be used by
themselves as conditions in control statements.
For example, for an integer variable x
, the following
statement is legal in C.
while (x) { ... }
In Java, the above statement is not legal because the control expression in a while loop must express a boolean value. Thus the loop must be written as follows.
while (x != 0) { ... }
An object is an encapsulation of data along with methods for manipulating the data. Java objects are grouped into classes. Two objects in the same class contain the same kind of data components and are manipulated by the same set of methods. In Java, classes are regarded as a special kind of object.
There are many Java classes that are defined in the standard class library. In addition, programmers may define their own classes. In fact, almost all Java coding is involved in the definition of classes.