Java has two categories of data types,
- Primitive data type
- Non-Primitive data type
Data types in Java
- Primitive Data Type in Java
- Boolean Type – boolean
- char
- byte
- int
- short
- long
- double
- float
1. Boolean represents only single bit of data either true or false.
public class Solution {
public static void main(String args[]) {
boolean var = true;
if (var == true) {
System.out.println("Boolean value is true");
}
}
}
2. char represents a single 16-bit Unicode character.
public class Solution {
public static void main(String args[]) {
char var = 'A';
System.out.println("char value is " +var);
}
}
3. byte represents a single 8-bit complement integer. The byte values represent between -128 to 127.
public class Solution {
public static void main(String args[]) {
byte a = 5;
byte b = -10;
System.out.println("byte a value is " +a);
System.out.println("byte b value is " +b);
}
}
4. int represents 32-bit unsigned integer. The int values range between 0 to 2^32 – 1
public class Solution {
public static void main(String args[]) {
int var1 = 0;
int var2 = 100;
System.out.println("int var1 value is " +var1);
System.out.println("int var2 value is " +var2);
}
}
5. short represents is a 16-bit signed two’s complement integer.
public class Solution {
public static void main(String args[]) {
short var1 = 0;
short var2 = 50;
System.out.println("short var1 value is " +var1);
System.out.println("short var2 value is " +var2);
}
}
6. long represents is a 64-bit two’s complement integer.
public class Solution {
public static void main(String args[]) {
long var1 = 752;
long var2 = 507589947;
System.out.println("long var1 value is " +var1);
System.out.println("long var2 value is " +var2);
}
}
7. float represents is a single-precision 32-bit IEEE 754 floating point.
public class Solution {
public static void main(String args[]) {
float var1 = 12.495956f;
float var2 = 5.265616f;
System.out.println("float var1 value is " +var1);
System.out.println("float var2 value is " +var2);
}
}
8. double represents is a double-precision 64-bit IEEE 754 floating point.
public class Solution {
public static void main(String args[]) {
double var1 = 12.2;
double var2 = 5.26;
System.out.println("double var1 value is " +var1);
System.out.println("double var2 value is " +var2);
}
}
Non-Primitive Data Type in Java
- String – Strings are defined as an array of characters in java.
public class Solution {
public static void main(String args[]) {
String var = "World";
System.out.println("Hello " + var);
}
}
Output: Hello World
- Arrays – An arrays are a group of like-typed variables that are referred to by a common name in java.
public class Solution {
public static void main(String args[]) {
int[] Array = {4,5,8,9,7};
System.out.println("Array first element is " + Array[0]);
System.out.println("Array last element is "+ Array[4]);
}
}
Java is the most popular programming language and widely-used programming language for many years. You can free learning java programming by following our Java articles sequentially.
815 total views, 3 views today