Types in Java
There are two basic data categories in Java, Objects and Primitives . All data members and local variables must have a data type that is either a primitive or some Objects (class type). A primitive data type uses a small amount of memory to represent a single item of data. While an Object is a large chunk of memory that can potentially contain a great deal of data along with methods to process that data.
Primitives Data Types
There are only eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. A Java program cannot define any other primitive data types. Primitives are used so often, making them objects would greatly degrade performance. The primitive data types are following:
The boolean primitive data type has two possible values, either true or false . These are also both lowercase words. A boolean is used to perform logical operations, most commonly to determine whether some condition is true.
Java evaluates a boolean expression by first evaluating the expression on the left, then evaluating the expression on the right, and finally applying the relational operator to determine whether the entire expression evaluates to true or false.
The smallest of the integral data types is the byte. It has a minimum value of -128 and a maximum value of 127 . The default value for byte data type is zero -'0'.
The char data type is based on sixteen-bit Unicode characters. Unicode enables you to specify all the characters of most character sets for the world's languages. '\u' in front of the hex codes represents that the character is a unicode. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff'. Unlike C, Java does not support signed characters. The Java designers felt that signed characters and unsigned numbers were sources for program bugs.
In addition, Java supports a number of other escape characters that make it easy both to represent commonly used nonprinting ASCII characters such as newline and to escape certain punctuation characters that have special meaning in Java. For example:
char backspace = '\b';
char tab = '\t';
A short is sixteen bits long. It has a minimum value of -32,768 and a maximum value of 32,767 . Because a short is signed, and a char is not signed, a char is numerically larger than a short, so you cannot assign a char to a short. default value for short is zero -'0'.
short i = 12000;
An int is a four-byte number—thirty-two bits and it represents a total of 4,294,967,296 numbers. Java accomplishes being platform-neutral by always making an int 32 bits in every JVM, a short always 16 bits, a long always 64 bits, and so forth. This saves Java from the problems that 'C' programmers encounter when porting code between platforms. For example, an int in a 'C' program can be two bytes on one Operating System and four bytes on another Operating System. Like all numeric data types int may be cast into other numeric types (byte, short, long, float, double) . When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.
A long is sixty-four bits long—eight bytes. The range of a long is quite large. This makes it useful when big, whole numbers are needed. Use this data type when you need a range of values wider than those provided by int.
This data type is used to represent the numbers with decimals. In Java, a float is made up of 32-bits IEEE 754 floating point. Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.
Double is a data type that is twice the size of a float. I.e. it is made up of 64-bit IEEE 754 floating point. You can also use exponential notation with floats and doubles. A double complies with the IEEE 754 specification for this data type. Because most systems can only do 64-bit emulation, don't use a double when a float would do. Generally, the performance hit for emulation is significant. Default value is 0.0.
Note: All the numeric primitives are signed, the only way to move a value from a large primitive to a smaller primitive is to use casting. In the next lesson you can study in deatil about TypeCasting.