Differences between primitive types and non primitive data types?
When to use a primitive and when to use a non-primitive data type in Java? We explain the basics behind it in this tutorial. Go on reading to get the full explanation of the usages. All code snippets will work in your preferred IDE.
Table of Contents
In the tutorial What are data types in Java we have explained the primitive Java data types. Now we need to explain non primitive data types.
With Java we work with a object oriented programming language. So we work with classes. A class contains typically a set of primitive types as their fields. Also we have beside of the primitive type representation, classes with additional functions/methods. This classes are therefore named non primitive data types or sometimes also complex data types.
Non primitive data types are also called reference types because they refer to an object. They are created by programmer and not by Java like primitive types are. As primitive types store a values (for example int = 42), non primitive types store a reference to that value. They can be a class, interface, or array variable.
1. Array variable
An array is a single object that contains multiple values of the same type. They can be very useful when we want to declace multiple variables, so instead of typing this:
int num0 = 1;
int num1 = 2;
int num2 = 3;
.
.
.
We can use arrays and in with them declaring multiple variables. Here is how to use arrays:
package myTestPackage
public class ArrayExample {
public static void main(String[] args) {
int a[] = new int[4]; //declaration
a[0] = 1: //initializing arrays
a[1] = 2:
a[2] = 3:
a[3] = 4:
for (int i=0; i<a.length; i++) { //length of property of array
System.out.println(a[i]); //printing out values of array
}
}
}
Explanation of the code:
Firstly we initialized an array and restricted it to only 4 values, after that we added a for statement in which we initialized a value (int = 0) which we will use to check the values of arrays. and after that we will print out the values.
We can also initialize arrays using only one line. Here is an example:
package myTestPackage
public class ArrayExample {
public static void main(String[] args) {
int a[] = {1,2,3,4}
for (int i=0; i<a.length; i++) //length of property of array
System.out.println(a[i]); //printing out values of array
}
}
Notice how here we didn't restricted an array to certain amount of values, here we added values to array immediately.
2. Class Data Types
Classes are really common in programing, we will make on just for an example:
class Example {
//code area
}
In order to create a new non-primitive or reference variable for this class, we have to create a new instance of the Example class. The new keyword is used to create an object.
Example something = new Example();
Now that we have a variable of "something" it is an instance of Example class, and not a set value like the primitive variables.
3. Interface Data Types
An interface is like a class except a Java interface can only contain method signatures and fields. Java Interface cannot contain an implementation of the methods, only the signature name of the method. Here is an example how to use Interface
public interface MyInterface {
public String hello = "Hello";
public void sayHello();
}
As shown, the interface is declared using the Java interface keyword. Just like classes, a Java interface can be declared as "public"
The interface in previous example contains one variable and one method. The variable can be accessed directly from the interface.
System.out.println(MyInterface.hello);
Now, before we can use an interface we need to implement it in some Java class. Here is a way to do it:
public class Example implements MyInterface {
public void sayHello(){
System.out.println(MyInterface.hello);
}
}
Notice the "implement" part above class declaration. This will tell Java compiler that the "Example" class implements the MyInterface.
More readings about