Basic Java Programming Language Types
The Java programming language has many built in data types. These fall into two broad categories: class types and primitive types. Primitive types are simple values, are not objects. Class types are used for more complex types, including all of the types that you declare yourself. Class types are used to create objects.
The Java programming language defines eight primitive data types, which can be considered in four categories:
● Logical – boolean
● Textual – char
● Integral – byte, short, int, and long
● Floating point – double and float
Logical – boolean
Logical values are represented using the boolean type, which takes one of two values: true or false. These values can be used to represent any two states, such as on and off, or yes and no. The boolean type has two literal values: true and false. The following code is an example of the declaration and initialization of a boolean type variable:
// declares the variable truth as boolean and
// assigns it the value true
boolean truth = true;
Note – There are no casts between integer types and the boolean type. Some languages, most notably C and C++, allow numeric values to be interpreted as logical values. This is not permitted in the Java programming language; when a boolean type is required, you can use only Boolean value.
Textual – char
Single characters are represented by using the char type. A char represents a 16-bit, unsigned Unicode character. You must enclose a char literal in single quotes (‘ ‘).
‘a’ The letter a‘\t’ A tab‘\u????’
A specific Unicode character, ????, is replaced withexactly four hexadecimal digits.
For example, ’\u03A6’ is the Greek letter phi [Φ].
Textual – String
You use the String type, which is not a primitive but a class, to represent sequences of characters. The characters themselves are Unicode, and the backslash notation shown previously for the char type also works in a String. Unlike C and C++, strings do not end with \0.
A String literal is enclosed in double quote marks:
“The quick brown fox jumps over the lazy dog.” Some examples of the declarations and initialization of char and String type variables are:
// declares and initializes a char variable
char ch = ’A’;
// declares two char variables
// declares two String variables and initializes them
String greeting = “Good Morning !! \n”;
String errorMessage = “Record Not Found !”;
// declares two String variables
Integral – byte, short, int, and long
There are four integral types in the Java programming language. Each type is declared using one of the keywords byte, short, int, or long. You can represent literals of integral type using decimal, octal, or hexadecimal forms as follows:
All numeric types in the Java programming language represent signed numbers.
Integral literals are of type int unless followed explicitly by the letter L that indicates a long value. In the Java programming language, you can use either an uppercase or lowercase L. A lowercase l is not recommended because it is hard to distinguish from the digit 1.
Long versions of the literals shown previously are:
The size and range for the four integral types are shown in Table. The range representation is defined by the Java programming language specification as a 2’s complement and is platform-independent.
3 This is the decimal form for the integer
3.077 The leading 0 indicates an octal value.
0xBAAC The leading 0x indicates a hexadecimal value.
2L The L indicates that the decimal value
2 is represented as a long value.
077L The leading 0 indicates an octal value.
0xBAACL The 0x prefix indicates a hexadecimal value.
Table Integral Data Types – Size and Range
IntegerLength Name or Type Range
8 bits byte From -27 to 27 -1
16 bits short From -215 to 215 -1
32 bits int From -231 to 231 -1
64 bits long From -263 to 263 -1
Floating Point – float and double
You can declare a floating-point variable using the keywords float or double. The following list contains examples of floating-point numbers. A numeric literal is a floating point if it includes either a decimal point or an exponent part (the letter E or e), or is followed by the letter F or f (float) or the letter D or d (double). Some examples of floating-point literals include:
3.14 A simple floating-point value (a double)
6.02E23 A large floating-point value
2.78F A simple float size value
323.4E+306D A large double value with redundant D
Note – The 23 after the E in the second example is implicitly positive. That example is equivalent to 6.02E+23.
Floating point literals are double by default. You can declare a literal of type float by appending F or f to the value.
The format of a floating point number is defined by The Java Language Specification to be Institute of Electrical and Electronics Engineers (IEEE) 754, using the sizes shown in Table. This format is platform independent.
Table Floating Point Data Type Size
Float Length Name or Type
32 bits float
64 bits double
Note – Floating point literals are double unless declared explicitly as float.