Datatypes
Data types means to identify the type of the data.
Data types define the value that a variable can take.
Data types also tells us information about:
• The size of the memory location
• The maximum and minimum value range that can store in the
memory location.
There are two data types available in Java −
1)Primitive Data Types
2)Non-Primitive Data Types
1
2
1.Primitive Data Types
primitive data types are the predefined data types of Java.
They specify the size and type of any standard values.
There are eight primitive datatypes supported by Java.
3
1.byte
• byte data type is used to save space ,since a byte is four times
smaller than an integer.
• Size: 8 bits
• Minimum value : -128 (-2^7)
• Maximum value : 127 (inclusive)(2^7 -1)
• Default value : 0
• Example: byte a = 100, byte b = -50
4
2. short
• A short is 2 times smaller than an integer
• Size: 16-bits
• Minimum value : -32,768 (-2^15)
• Maximum value : 32,767 (inclusive) (2^15 -1)
• Default value : 0.
• Example: short s = 10000, short r = -20000
5
3. int
• integer is generally used as the default data type for integral
values.
• Size: 32-bits
• Minimum value : -2,147,483,648 (-2^31)
• Maximum value : 2,147,483,647(inclusive) (2^31 -1)
• The default value : 0
• Example: int a = 100000, int b = -200000
6
4. long
• The long data type is used when you need a range of values
more than those provided by int.
• Size: 64-bits
• Minimum value : -9,223,372,036,854,775,808(-2^63)
• Maximum value : 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• Default value : 0l
• Example: long a = 100000l, long b = -200000l
7
5. float
• float is mainly used to store decimal numbers.
• Size: 32-bits
• Minimum value : 1.4e^-45
• Maximum value : 3.4028235e^38
• Default value : 0.0f
• Example: float f1 = 2.5f
8
6.double
• It provides higher precision than float.
• Size: 64-bits
• Minimum value : 4.9e^-324
• Maximum value : 1.7976931348623157 *10^308
• Default value : 0.0d
• Example: double d1 = 3.4d
9
7. boolean
• boolean data type represents one bit of information
• There are only two possible values: true and false
• This data type is used for to track true/false conditions
• Default value is false.
10
8. char
• Char data type is used to store a single character.
• The character must be enclosed within single quotes.
• Size: 16-bit Unicode character
• Minimum value : '\u0000' (or 0)
• Maximum value : '\uffff' (or 65,535 inclusive)
• Example: char letterA = 'A'
11
2. Non-Primitive Data Types in Java
In contrast to primitive data types, which are defined by Java, non-
primitive data types are not defined or created by Java but they
are created by the programmers.
They are also called Reference data types.
Examples of Non-primitive data types are:
• Classes
• Arrays
• Interfaces
12