Java Data Type, Primitive and Non-Primitive Data type, Java Variables

Table of Contents
Java Data Types
Java categorizes the data type into two parts, primitive data types, and non-primitive data types respectively.
Primitive Data Types
The primitive data types: divide into four groups :
- Integers: This group is used to hold the whole-signed numbers, it includes short, byte, int, and long data types.
- Floating-point numbers: This group represents the fractional precision numbers, this group includes the double and float.
- Characters: This group represents the set of characters, it can be anything like numbers and letters. It includes the char.
- Boolean: This group represents the special types of data types true/false. It includes the boolean.
Non-Primitive Data Types
The non-primitive data types into three part :
- String: The string is a sequence of characters, but in the java programming language string is an object that holds the sequence of characters.
- Arrays: The array is the sequence of similar data types, but in java, Arrays is an object that holds the sequence of similar data type.
- Classes: Class is used to represent the objects in an imaginary, Object(class) is an encapsulation of data members and functions.
Java Primitive Data Types
The primitive data types are defined as simple types of data in java, which cannot be derived from existing data types e.g char.
Java Integers
Java Integers have four different types of data: byte, short, int, and long. Java doesn’t support unsigned, positive-only integers. The range of Integers given in the following table:
Name | Width(bits) | Range |
---|---|---|
byte | 8 | -128 to 127 |
short | 16 | -32,768 to 32,767 |
int | 32 | -2,147,483,648 to 2,147,483,647 |
long | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
1. byte
The byte is 8-bit signed data types, that can hold the data from -128 to 127 in range. Byte variables can be declared using the byte keyword, as shown below:
byte x,y,z;
In the above example, there are byte types data are given named as x, y, and z variables.
A byte is very useful in streaming data from a network or any type of file.
2. short
The short is signed 16-bit data types, that can hold the data from -32,768 to 32,767 in range. short variables can be declared using the short keyword. Example of short data types given below:
short x,y,z;
3. int
The int is signed 32-bit data types, int have range from -2,147,483,648 to 2,147,483,647. It is a commonly used data type that is used to holds numerical values.
int x,y,z;
4. long
The long is signed 64-bit data types, that can holds the data from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 in range. Long is used instead of int where the range of data cannot hold int data types.
long x,y,z;
Java Floating-point
A Floating-point data type is used to represent the fractional precision value. It is used to hold the results of any expression evaluate that requires the float number or real numbers.Java implements the standard (IEEE–754) for floating points. There are two different types of floating-points data types: float and double, which represent the single-precision value and the double-precision value respectively.
Name | Width(bits) | Range |
---|---|---|
float | 32 | 4.9e-324 to 1.8e+308 |
double | 64 | 1.4e-045 to 3.4e+038 |
1. float
The float is signed 32-bit data type, int has ranged from 4.9e-324 to 1.8e+308. The float data type specifies the single-precision value.
int x,y,z;
2. double
The double data type is a signed 64-bit data type, that can hold the data from 1.4e-045 to 3.4e+038 in range. The double data type specifies the single-precision value. Double data types is used to perform the math functions such as sin( ), cos( ), and sqrt( ), and return double values as a results.
double x,y,z;
Java Characters
In Java, char is used to holds the character single value. Java uses Unicode to represent the characters using char data types. Java char is 64-bit signed data type with a range from 0 to 65,536. Following char example given:
class CharDemoClass {
public static void main(String[] args){
char c1 , c2;
c1 = 67; // code for C;
c2 = 'D';
System.out.print("C1 and C2 : ");
System.out.println(c1 + " " + c2);
}
}
Output of the Above Program :
C1 and C2 : C D
In the above char program, there is two char data type declared as c1 and c2 respectively. 67 is assigned to c1, which represents the ASCII(Unicode) value of the ‘C’ character and ‘D’ is assigned to c2 as the character. The Java char data type automatically converts the 67 into character using ASCII(Unicode) value because 67 represents the char ‘C’ in ASCII(Unicode) table.
Java Boolean
In java, boolean is used to represent the logic values, It can hold only two possible values, either True or False. The boolean value can be achieved by expressions like x > z case, In if() condition boolean can be used. A boolean example is given below:
public class BooleanDemoClass {
public static void main(String[] args){
boolean b1 , b2,result;
b1 = true;
b2 = false;
System.out.println("Value of B1 : "+b1);
System.out.println("Value of B2 : "+b2);
if(b1==b2){
System.out.println("Value of B1 and B2 is equal");
}
if(b1!=b2){
System.out.println("Value of B1 and B2 is not equal");
}
}
}
Output of the Above Program :
Value of B1 : true
Value of B2 : false
Value of B1 and B2 is not equal
Leave a Reply