Hey guys!! today we are going to move forward with this Java tutorial series. In this lecture, we can see that Java Data Types and their uses. At first, we have to know that what is java data types?
What is Java Data Types?
Data Types works as an allocator of different sizes and types of values that can be stored in the variable. Based on the data type of a java variable, the operating system assigns memory and decides what can be stored in the reserved memory. Therefore, by allowing different data types to the variables, we can store integers, decimals, or characters in these variables.
Java Data types are two types:
- Primitive Data Types
- Non-Primitive Data Types or Reference Data Types
Primitive Data types:
Primitive Data Types are predefined and available within the Java language. Primitive values do not share the state with other primitive values.
These are 8 which includes byte, short, int, long, char, float, double, and Boolean Integer data types.
- Boolean data type
- byte data type
- char data type
- short data type
- int data type
- long data type
- float data type
- double data type
Boolean Data Types:
Like other programming languages, java also has a Boolean data type that works the same way as others. Boolean data types are used to store only two values that are True and False.
The Boolean data type identify one bit of information, but its “size” can’t be defined precisely.
For Example: Boolean one = false
Byte Data Types:
In Java Data Types, the byte data type is one the primitive’s data type that is an 8-bit signed two’s complement integer. Its value range lies between -128 to 127 (inclusive). Its minimum value is -128 and its maximum value is 127. 0 is the default value.
For example: byte a = 20, byte b = -10
Char Data Types:
The char data types contain a single 16-bit Unicode character. Its range lies between’\u0000′ (or 0) to ‘\uffff’ (or 65,535 inclusive).The char data type is used to store characters.
Example: char letterA = ‘A’
Short Data Types:
The short data type is a 16-bit signed two’s complement integer. Its value lies between -32,768 to 32,767. Its minimum value is -32,768 and its maximum value is 32,767. 0 is the default value.
The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller than an integer.
Example: short s = 10000, short r = -5000
Int Data Types:
It contains integer type value. This data type is 32 bits signed two’s complement integer. Its value lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648and maximum value is 2,147,483,647.
Example: int a = 200000, int b = -100000
Long Data Types:
The long data type of java is a 64-bit two’s complement integer. Its value lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
Example: long a = 500000L, long b = -300000L
Float Data Types:
Java’s float data type is a single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
Example: float f1 = 434.5f
Double Data Types:
The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value range is unlimited. The double data type is generally used for decimal values just like float.
Example: double d1 = 13.3
Non-Primitive Data Type or Reference Data Types
Non-Primitive data types refer to objects that’s why it is called reference types. Non-primitive data types include Strings, Arrays, Classes, Interface, etc.
We will discuss briefly Strings, Arrays, Classes, Interface in the coming lecture. So, Please stay with us.
FAQs:
What is Difference between primitive and non-primitive data types?
Primitive types are predefined data types in Java. Non-primitive types are created by the programmer and are not defined by Java-like strings, classes, arrays.
Why char uses 2 byte in java and what is \u0000 ?
It is because Java uses a Unicode system not an ASCII code system. The \u0000 is the lowest range of the Unicode system. To get a detailed explanation about Unicode visits the next page.