3. Recap
In the previous class we have discussed
• White spaces, separators, literals and keywords
• Comments in Java
3
4. 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
5. Data types in Java
• Non-primitive types are also known as reference
types
• Non-primitive types are
• Classes
• Interfaces
• Arrays
5
6. The classification of data types in Java
Data types in Java
Primitive Non-primitive
Numeric Non-numeric
classe Arrays
s
integer
char Interface
Floating point Boolea
n
Fig. 6.1 Data types in Java
6
7. Data types
• Primitive Java data types can be grouped into
• integers
• floating point numbers
• characters
• boolean
7
8. 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
8
9. 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;
9
10. 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;
10
11. 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;
11
12. 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;
12
13. 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’
13
14. 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;
14
15. 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;
15
16. 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
16
17. 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;
17
18. 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
18
19. Summary
• In this class we have discussed
• Data types in Java
• Literals in Java
19
20. Quiz
1.Which of the following data type can store only
two values
a) int
b) float
c) boolean
d) All of the above
20
22. Frequently Asked Questions
1. List the different data types available in Java
2. Explain the various data types available in Java
3. List the Literal in Java
4. Explain the Literals in Java
22