Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Non Primitive Data Types in Java

Last Updated on April 18, 2023 by Prepbytes

Data Types define the type of data which will be stored in the variable or a data structure. In Java, there are two datatypes that are Primitive Data Type and Non Primitive Data Types in Java. Here our main focus will be on the Non Primitive Data Types in Java. So let’s begin with the introduction to Data Type in Java.

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

Java has a set of data types that are already defined within the language, known as primitive data types. 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 the fractional numbers and is 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 of Primitive Data Types in Java:

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 in Java are not pre-defined by the programming language itself, unlike primitive data types. Instead, they are created by programmers as needed for their programs. The users can effortlessly create and Also can modify this easily. They can store a couple of values and methods to sure certain tasks. Non Primitive data types in Java are not predefined. Non primitive data types in Java are consumer–defined data types.

Whilst we define a variable of non primitive data types in Java, 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 absolute object reference variable.

This object reference variable lives in the stack memory and the object to which it points constantly lives in the heap memory. The stack holds a pointer to the items on the heap.
All non primitive data types in Java are simply referred as objects which might be created through instantiating a class.

Kinds of Non Primitive Data Types

The following are the different types of Non Primitive Data Types in Java.

  • Class and Objects
  • String
  • Array
  • Interface

Class & Objects:
Class in Java is a user-defined data type. It is designed by the programmer and serves as a template or blueprint for creating data structures. It consists of member variables and methods that define its behavior. An object is an instance of a class, which can access the member variables and methods of the class.

Example:

class PrepBytes {  
      
        int a = 50;  
        int b = 30;  
        int c;  
   
        public void add () {  
            int c = a + b;  
            System.out.println("Addition of numbers is: " + c);  
        }  
  
        public void sub () {  
            int c = a - b;  
            System.out.println("Subtraction of numbers is: " + c);  
        }  
      
    public static void main (String[] args) {  
        PrepBytes obj = new PrepBytes();  
   
        obj.add();  
        obj.sub();  
    }  
}

Output:

Addition of numbers is: 80
Subtraction of numbers is: 20

String:
Strings in Java is a collection of characters that can either be same or different. Strings in Java represent a sequence of characters.It’s located in java. lang.String. However, strings end with a ‘\0’ character. Example: “presbyters”, “coding” etc.

Example:

class Main{  
    public static void main(String[] args) {  
           
        String str = "Hello! Welcome to Prebytes world";  
  
        // printing the string  
        System.out.println(str);  
    }  
}  

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 of Array in Java:

// 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] + " "); 
    } 
  } 
}

Input:

Enter the numbers (size = 5) :
56
43
22
1
7

Output:

Previous array with initialized size is:
1 2 3 6 9

The new array we have entered is:
56 43 22 1 7

Interface:
The interface is used to achieve abstraction in Java. Interfaces in Java contain both methods and variables. By default, methods declared in an interface are abstract. This means they do not have a body and must be implemented by the class that implements the interface. Interfaces have the following types of methods.

  • 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

Difference between Primitive and Non Primitive Data Types in Java

Here are some key differences between Primitive and Non Primitive Data types in Java.

  • Primitive data types in Java are pre-defined by the system, whereas non primitive data types in Java have to be created and defined by the programmer.
  • Variables of primitive data types can only hold one value at a time, while variables of non primitive data types in Java can hold multiple values of the same type or different types.
  • In terms of storage, data for primitive types are stored directly on the stack, while for non-primitive types, the stack holds a reference (pointer) to the object on the heap.
  • The naming convention for primitive types starts with a lowercase letter, while non primitive data types in java start with an uppercase letter.
  • The size of a primitive type depends on the specific data type, while non-primitive types have the same size.

Conclusion
In conclusion, non-primitive data types in Java are user-defined data types that are not predefined by the language. They are created as needed by programmers for their programs and include classes, arrays, and interfaces. These data types are more complex than primitive data types and can hold multiple values of different types. By creating and utilizing non-primitive data types, programmers can write more complex and sophisticated programs in Java.

Frequently Asked Questions (FAQs)

Here are some questions that are frequently asked on the topic of Non Primitive Data types in Java.

Ques 1. What are some examples of non primitive data types in Java?
Ans. Examples of non primitive data types in Java include classes, arrays, and interfaces.

Ques 2. How are non primitive data types declared in Java?
Ans. Non primitive data types are declared using the class keyword followed by the name of the data type.

Ques 3. How is a class declared in Java?
Ans. A class in Java is one of the Non Primitive Data Types in Java and is declared using the "class" keyword, followed by the class name and a pair of curly braces that contain the class definition.

Ques 4. Can a class implement multiple interfaces in Java?
Ans. Yes, a class in Java can implement multiple interfaces.

Leave a Reply

Your email address will not be published. Required fields are marked *