In this article we learn the what is data types in java, types of data types and difference between primitive and non-primitive data types.
Data Types In Java (Java Data Types)
1. Data types in java specify the different sizes and values that can be stored in the variable.
2. Data types in java can be used to refer to different types of values.
Types of Data Types In Java
- byte
- short
- int
- long
- float
- double
- boolean
- char
- String
- Array
- Class
- Interface
A. Primitive Data Types in Java
These data types act as the basic building blocks of data manipulation in Java. Primitive data types have a constraint that they can hold data of the same type and have a fixed size. Primitive data types are also called intrinsic data types. We can also perform operations on primitive data types.
There are eight primitive data types in Java.
1.byte
The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127.
Example:
byte myNum = 100;
System.out.println(myNum);
2. short
The short data type can store whole numbers from -32768 to 32767:
Example:
short myNum = 5000;
System.out.println(myNum);
3. int
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.
Example:
int myNum = 100000;
System.out.println(myNum);
4. long
The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an “L”.
Example
long myNum = 15000000000L;
System.out.println(myNum);
5. float
You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.
Example:
float myNum = 5.75f;
System.out.println(myNum);
6. double
The float and double data types can store fractional numbers. Note that you should end the value with an “f” for floats and “d” for doubles:
Example
double myNum = 19.99d;
System.out.println(myNum);
7. boolean
A boolean data type is declared with the boolean keyword and can only take the values true or false:
Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
8. char
The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:
Example:
char myGrade = ‘B’;
System.out.println(myGrade);
b. Non-Primitive Data Types in Java
Non-primitive data types are called reference types because they refer to objects.
The term non-primitive data type means that these types contain “a memory address of the variable”.
Non-primitive data types do not store the value itself, but they store a reference or address (memory location) of that value.
They can call methods to perform a particular function. They can also be null.
1. String
The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:
Example:
String greeting = “Hello World”;
System.out.println(greeting);
2. Array
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
String[] cars;
We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal – place the values in a comma-separated list, inside curly braces:
String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”};
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
3. Class
Class is a collection of multiple statements.
Example:
public class Main {
int x = 5;
}
4. Interface
Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely “abstract class” that is used to group related methods with empty bodies:
Example:
// interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
Difference Between Primitive And Non-primitive Data Types in Java
1. Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String).
2. Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
3. A primitive type has always a value, while non-primitive types can be null.
4. A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
5. The size of a primitive type depends on the data type, while non-primitive types have all the same size.