Data types are defined as the type of data which is stored in the variable. Java has two types of data types: Primitive Data Type and Non – Primitive Data Types. In this article, we read about non primitive data types in java, their uses, and implementations.
Introduction to Data Types in Java
In a programming language, data types are used to define the kind of data that is to be stored in a variable. Java primitive data types are the ones that are predefined by the programming language which in this case is Java. There are in particular two forms of Data types in java:
- Primitive Data Types.
- Non Primitive Data Types.
Primitive Data Types in Java
A primitive data type states the size, type of variable values, and no additional methods. There are mainly 8 types of primitive data types in Java:
Float: Stores fractional numbers. Capable of storing 6 to 7 decimal digits
Short: Stores numbers from -32,768 to 32,767
Long: Stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Int: Stores numbers from -2,147,483,648 to 2,147,483,647
Bytes: Stores numbers from -128 to 127
Double: Stores fractional numbers. Capable of storing 15 decimal digits
Char: Stores a single character/letter as well as ASCII values
Boolean: Stores only true or false.
Example:
int number = 3;
float myfloatnumber = 3.2f;
char ch = ‘A’;
Boolean flag = false;
String str = “hello”;
Non Primitive Data Types in Java:
Non primitive data types in java are also known as reference types. Non primitive data types are created by means of programmers, this is not predefined like primitive data types of java. The users can effortlessly create them ans Also can modify this easily. They can store couple of values and methods to sure certain tasks. Non – Primitive data types are not predefined. Non–primitive data types in java are consumer–defined data types.
Whilst we define a variable of non primitive data types, it references a memory place wherein data is saved in the heap memory. That is, it references a memory where an item is clearly located.
Consequently, a non primitive data type variable is also referred as a referenced data type in Java or an absolutely object reference variable.
This object reference variable lives at the stack memory and the object to which it points constantly lives at the heap memory. The stack holds a pointer to the items on the heap.
In Java programming, all non primitive data types are simply referred as objects which might be created through instantiating a class.
Kinds of Non Primitive Data Types:
- Class and Objects
- String
- Array
- Interface
Class & Objects:
Class in java is a user-defined data type. It is created by user. It act as a blueprint to data which consists of member variables and methods. An object is variable of class, that could be access the elements of class methods and variables.
Example:
// importing required packages import java.io. * ; import java.util. * ; class ArrayExamplePrpbytes { public static void main(String[] args) throws IOException { int i; Scanner scn = new Scanner(System. in ); // declaring and initializing an array int arr[] = {1, 2, 3, 6, 9}; // defining another array arr1 int arr1[] = new int[5]; // reading values from the user System.out.println("Enter the numbers (size = 5) :"); for (i = 0; i < 5; i++) { arr1[i] = scn.nextInt(); } System.out.println("Previous array with initialized size is: "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println("\nThe new array we have entered is:"); for (i = 0; i < 5; i++) { System.out.print(arr1[i] + " "); } } }
Output:
Addition of a and b 80
Subtraction of a and b 20
String:
We know about String, String is a collection of many characters that either be same or different. String in java represents a sequence of characters.It’s located in java. lang.String. However, strings end with a ‘\0’ character. Example: “presbyters”, “coding” etc.
Example:
// importing required packages import java.io. * ; import java.util. * ; class ArrayExamplePrpbytes { public static void main(String[] args) throws IOException { int i; Scanner scn = new Scanner(System. in ); // declaring and initializing an array int arr[] = {1, 2, 3, 6, 9}; // defining another array arr1 int arr1[] = new int[5]; // reading values from the user System.out.println("Enter the numbers (size = 5) :"); for (i = 0; i < 5; i++) { arr1[i] = scn.nextInt(); } System.out.println("Previous array with initialized size is: "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println("\nThe new array we have entered is:"); for (i = 0; i < 5; i++) { System.out.print(arr1[i] + " "); } } }
Output:
Hello! Welcome to Prebytes world
Array:
Array is an object in java, and hold elements of same type. Array name is a reference value that includes the bottom cope with of continuous location of factors in an array. This makes it less complicated to calculate the placement of every element by way of actually including an offset to a base value, i.e., the memory location of the first element of the array (generally denoted through the cell of the array). The bottom value is index zero and the difference among the two indexes is the offset.
Example:
// importing required packages import java.io. * ; import java.util. * ; class ArrayExamplePrpbytes { public static void main(String[] args) throws IOException { int i; Scanner scn = new Scanner(System. in ); // declaring and initializing an array int arr[] = {1, 2, 3, 6, 9}; // defining another array arr1 int arr1[] = new int[5]; // reading values from the user System.out.println("Enter the numbers (size = 5) :"); for (i = 0; i < 5; i++) { arr1[i] = scn.nextInt(); } System.out.println("Previous array with initialized size is: "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println("\nThe new array we have entered is:"); for (i = 0; i < 5; i++) { System.out.print(arr1[i] + " "); } } }
Output:
Enter the numbers (size = 5) :
56
43
22
1
7
Previous array with initialized size is:
1 2 3 6 9
The new array we have entered is:
56 43 22 1 7
Interface:
Interface is used to achieve abstraction in Java. Interface have methods and variables. Methods declared in the interface by default abstract.
- Abstract methods
- Default and static methods
- Private methods
Example:
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ interface Operations { // Methods with signature declaration inside interface void multiple(); void division(); } class Solve implements Operations { // Solve class implements the Operations interface int a = 30, b = 60, c; // implementing the interface methods public void multiple() { int c = a * b; System.out.println("Multiplication of a and b: " + c); } public void division() { int c = b / a; System.out.println("Division of a and b: " + c); } } class Main { public static void main(String[] args) { Solve obj = new Solve(); obj. multiple(); obj.division(); } }
Output:
Multiplication of a and b: 1800
Division of a and b: 2
So this all above is about Non primitive Data Types in Java, Now we discussed the Difference between primitive data types in java and non – primitives data types. You have to also know the difference between them for interview purposes. So some of the main points regarding are following:
- Primitive data types are predefined.
- Non Primitive data typescan used to call techniques to do positive venture, while primitive data types cannot used for same.
- A primitive data type always has a value, whereas non primitive types can be null.
- A primitive type starts with a lowercase letter, whilst non primitive types start with an uppercase letter.
- The size of a primitive type depends on the data kind, while non primitive types have all the equal size.