In this article, you are going to learn about different Data Types used in Java Programming Language. You May Join W3training school Java training program to learn deeply about data types with help of java industry professionals.
Data types are used to assign different values(int, string, char,bool etc.) to variables in order to employ and ensure type safety. They tells the compiler what type of value a programmer wants to store/allocate in the memory.
Almost all the programming languages require data types to prevent type errors which occurs by assigning illegit values to the variables. Like other languages, java also supports a bunch of data types which we will discuss briefly later in this article.
Whenever an expression gets its data type, the computer allocates memory to store the value of that expression(variables, function etc.).
Basically, there are two types of data types in java programming language which are further divided into groups as shown below:
- Primitive Data Types
(a) Boolean
(b) Character
(c) Integer
→ int
→ short
→ long
→ byte
(d) Floating-point
→ float
→ double - Non-primitive Data Types/ Reference Data Types
→ String
→ arrays, etc.
PRIMITIVE DATA TYPES
Java Programming Language supports following four major primitive data types:
[1] Boolean(bool)-
⏩ It is a logical data type used for conditional statements.
⏩ It has two truth values True and False.
⏩ In Java, the comparison operators like ≠ and > returns the boolean value.
⏩ The logician George Boole(Eng), creator of boolean algebra, is the eponym of boolean data type.
⏩ The default value of boolean data type is False and default size is 1 bit.
[2] Character(char)-
⏩ Char data type in java follows Unicode encoding model and represents a single 16-bit character.
⏩ The default value of char data type is ‘\u0000’ or ‘0’(minimum) and can hold upto a maximum value of ‘\uffff’ corresponding to an inclusive figurative value 65,535 (216-1).
⏩ The default size is 2 bytes or 16 bits.
⏩ Char data type is used to store values including numerical digits, letters, whitespace and some punctuation marks.
⏩ When characters are combined in a sequence, they results in a string.
[3] Integer- There are four types of integer data type used in java.
(a) int-
⏩ Int data type has a default size of 4 bytes or 32 bits.
⏩ The default value is 0.
⏩ Minimum value =-2,147,483,648 (-231)
⏩ Maximum value = 2,147,483,647 (231-1)
⏩ The value of the int primitive data type is wrapped in an object by the java.lang.Integerclass.
(b) short-
⏩ Short primitive type has a size of 2 byte or 16 bits by default.
⏩ It has a default value equal to zero.
⏩ Minimum value = -32,768 (-215)
⏩ Maximum value = 32,767 (215-1)
⏩ This primitive data type is very useful in case of large arrays and can be used to save memory.
(c) long-
⏩ By default, the size of long primitive type is 8 byte or 64 bits.
⏩ Default value = 0L
⏩ Minimum value = -9,223,372,036,854,775,808 (-263)
⏩ Maximum value = 9,223,372,036,854,775,807 (263-1)
⏩ The value of the long primitive data type is wrapped in an object by the java.lang.Long class.
(d) byte-
⏩ Byte has a default size of 1 byte or 8 bits.
⏩ Default Value = o
⏩ Minimum value = -128 (-27)
⏩ Maximum value = 127 (27-1)
⏩ The value of the byte primitive type is wrapped in an object by the java.lang.Byte class.
⏩ It can serve as an alternate option for int data type.
⏩ Like short type, it is also useful when dealing with large arrays in order to save the memory.
[4] Floating-point- In java, two types of floating-point primitive data types are used:
(a) float-
⏩ It is a 32-bit(4 byte) single precision IEEE 754 floating point data type.⏩ Default value = 0.0f
⏩ float cannot be used for precise values(i.e, currency).
⏩ It can also be used in case of large arrays in order to save memory.
⏩ The value of the float primitive type is wrapped in an object by the java.lang.Float class.
(b) double-
⏩ It is a 64-bit(8 byte) double precision IEEE 754 floating point data type.
⏩ Default value = 0.0d
⏩ Like float, it cannot be used for precise values(i.e, currency).
⏩ Decimal values like 2.5, 4.7 etc are considered as double data type. To use them as float, suffix ‘f’ should be added. For example, float a = 4.7f
⏩ Decimal values with suffix ‘d’ is an optional way to define a double data type. For example, double a = 2.5 or 2.5d
NON-PRIMITIVE/REFERENCE DATA TYPES
⏩ These are also called Object Data Types.
⏩ String, arrays, random, int[], Scanner, etc. are the example of non-primitive data types.
⏩ These are different from the primitive data types in a way that these are used to store the address rather than storing primitive values.
⏩ The reference data types have a default value = Null
⏩ These are not predefined.
Also Read:- How To Create An Object In Java
We have provided you the description on Various Data Types In Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on Java Programming Language.