Copyright, 2002, Susan Anderson-Freed


Primitive Data Types in Java

Our previous examples always extracted string information from text fields. We may also obtain numeric and logical information; however, this information must be converted from a string to the appropriate type before the information is usable.

If you know C or C++, the Java primitive data types will come as no surpise. These primitive types are listed in the following table. Several aspects of this table are worth noting:

  1. Most of the Java primitive data types are identical to the C and C++ primitive data types, e.g., byte, char, int, float, double, short, and long.

  2. Each of the primitive types contains an object wrapper: a class bearing a name similar to the primitive type. The object wrapper contains conversion methods that let a programmer easily change a datum's type.

  3. Unlike C and C++, Java contains a "real" boolean type with true and false values.

Java Primitive Types
from smallest to largest
Type name Encoding Wrapper Class
boolean true, false Boolean
byte 8-bit signed integer Byte
char 16-bit unsigned integer in UniCode characters Character
short 16-bit signed integer Short
int 32-bit signed integer Integer
long 64-bit signed ineger Long
float 32-bit in IEEE 754 standard Float
double 64-bit in IEEE 754 standard Double

The Primitive program uses the Stdin class to read in each of the primitive types. The link to the program is contained in the following table.

Primitive Types Program

Java also provided three additional numeric classes. Two of these classes, BigInteger and BigDecimal, let a programmer create numbers with greater precision than the primitive types. Thus, we can create objects that have a greater integer precision than long, or a greater floating point precision than double. The remaining class, math contains methods that implement trigonometric and other common mathematical functions, e.g., exponentiation, square roots, and flooring and ceiling operations.

The Wrapper Classes

Each of the wrapper classes contains constructors that have either the corresponding primitive type as a parameter or a string. The constructors create a new object of the wrapper class. We will use the wrapper classes when we create polymorphic functions, that is, functions that work with many types of data.

The following table uses the Integer class to illustrate wrapper class methods. Each of the remaining numeric wrapper classes contains the same methods as listed in this table. Of course, the name of the constructor matches the name of the wrapper class.

ConstructorReturn float equivalent
Frequently used Integer class methods
MethodExplanation
Integer(int i)
Integer(String str)Constructor
double doubleValue()Return double equivalent
float floatValue()
int intValue()Return int equivalent
long longValue()Return long equivalent
short shortValue()Return short equivalent
boolean equals(Object obj)Return true if parameter matched Integer object; return false otherwise.
String toString(int i)Return String equivalent
String toString(int i, int radix)Return String equivalent using specified radix.

Operator Hierarchy

The Java math operators and precedence hierarchy is identical to the hierarchy used by C, C++, JavaScript and Perl. A table of the operators, listed by precedence follows:

Arithmetic Operators

Java Numeric Operators: Highest to lowest precedence
Operator Description
 ++
 --
 +
 -
Increment
Decrement
Unary Plus
Unary Minus
 *
 /
 %
Multiplication
Division
Modulus
 +
 -
Addition
Subtraction
 <<
 >> 
 >>>
Left bit shift
Right bit shift, sign extension
Right bit shift, zero extension
You may access a table containing the complete Java Operator Hierarchy by clicking on the following link.

Click Me!
Complete Java Operator Hierarchy