Java is a strongly typed language. Every type is strictly defined and all assignments (including parameters passing) are checked for type compatibility. There are no automatic conversions of conflicting types as in some languages. Any type mismatches are errors that must be corrected before the compiler will finish compiling the class.
The 8 primitive data types are not object-oriented since making the primitive types into the object would have degraded the performance too much. C & C++ allow the size of an int to vary based on the execution environment. But in Java, because of the portability requirements, all data types have a strictly defined range (eg) int —32 bits (4 bytes).
- No support for unsigned, positive only integers.
- long 64 bits
int — 32 bits
short — 16 bits
byte — 8 bits (-128 to 127)
- byte: variables of this type are useful while working with a stream of data from a network or a file and working with raw binary data.
(eg) byte a,b; a& b can store any number between -128 to +127
- short : short n;
- int: Always better to use 'int' even to control loops and to index arrays; although short or byte would be sufficient, because when these are used in an expression, they are promoted to 'int', when the expression is evaluated.
- long: long x;
2. Floating point:
32 bits — single precision & faster on some processors and takes half as much space as double precision but imprecise, when values are too large or too small.
64 bits — faster than single precision on some modern processors. Optimized for high-speed mathematical calculations.
Floating-point data types (float and double) support special values such as POSITIVE INFINITY, NEGATIVE INFINITY & NaN (Not a Number). NaN is used to representing the results of operations such as division of zero by zero, where an actual number is not produced. These are static constants defined in both Float and Double wrapper classes.
- Not like in C/C++, which is 8 bits wide.
- Java uses Unicode to represent a character. It can represent all of the characters found in all human languages and hence it is an international character set. So, it requires 16 bits (2 bytes — 0 to 65536). No negative char is allowed. The standard ASCII character set ranges from 0 to 127 and the extended 8-bit, charset ISO-Latin-1 ranges from 0 to 255 within the Unicode.
For global portability, Unicode is used to represent characters, though not efficient for languages like English, French,... It supports more than 34,000 defined characters derived from 24 languages from America, Asia, etc. The Unicode standard has now been extended to allow up to 1,112,064 characters and these extra characters are called supplementary characters.
- char — can also be used to perform arithmetic operations.
(eg) char ch1 = 'X; ch1++; => Y
Boolean variables are used to denote true/false values (logical values).
Eg: boolean b=true, c=false;
System.out.println("b is" + b); ->b is true
System.out.println("not executed"); ->no o/p since 'c' is false
Note: The outcome of result of a relational operator is a Boolean value.
Eg: System.out.println(10 > 9); ->true