2. DATA TYPES IN JAVA
• Data types in Java are classified into
• Primitive types • Non-primitive types
• Primitive types are also known as simple data types
• A primitive type is predefined by the language
• Primitive types are further classified into
• Numeric types
• Non-numeric types
4. • Primitive Java data types can be grouped into
• integers
• floating point numbers
• characters
• boolean
5. INTEGER TYPES
• Java defines four integer types: byte, short, int and long
• All these are signed, positive and negative values
• Java does not support unsigned types
• byte is 8 bit width , short is 16 bit width int is 32 bit width and long is 64 bit width
6. BYTE TYPE
• The smallest type is byte
• Range from -128 to +128
• Byte variables are declared by use of keyword ‘byte ‘
eg.
byte x, y;
7. SHORT TYPE
• short is a signed 16 bit type.
• Its range is from -32,768 to +32,767
• It is probably the least used data type in Java
• Keyword used is ‘short’
eg.
short x;
8. INT TYPE
• The most commonly used integer data type is int
• It has a range of -2,147,483,648 to 2,147,483,647
• Keyword used is ‘int’
eg.
int x;
9. LONG TYPE
• long is a signed 64 bit type
• Useful where an int type is not large enough to hold the desired value
• The range of large is quite large
• This makes it useful when large, whole numbers are needed
• Keyword used is ‘long’
eg. :
long seconds;
10. FLOATING POINT TYPES
• floating point numbers are also known as real numbers
• Are used when fractional components are required
• float and double are two kinds of floating point types
• Keywords used are ‘float’ and ‘double’
11. FLOAT TYPE
• float specify a single precision value that uses 32 bits of storage
• float type is useful when you require fractional component, but does not require a large
degree of precision
eg.
float area
12. DOUBLE TYPE
• double uses 64 bits of storage
• When you need to maintain accuracy over many iterative calculations or manipulating
large valued numbers double is the best choice
eg. :
double area;
13. CHARACTER TYPE
• In Java the data type used to store character is char
• char in Java is different from char in C and C++
• Java used Unicode to represent characters
• Unicode was selected because it would help in internationalization of Java as a programming
language
• Java character is 16 bit type.
The range of the characters is 0 to 65,536
14. BOOLEAN TYPE
• boolean data type is used to represent logical values that can be either true or false
• All relational, conditional and logical operators return boolean values
• It used only one bit of storage
• Keyword used is ‘boolean’
eg;.
boolean flag;
15. THE SIMPLE DATA TYPES IN JAVA
• In summary Java defines eight simple types of data.
They are
• byte
• short
• int
• long
• char
• float
• double
• boolean