Home >Java >javaTutorial >Non-Primitive Data Types in java

Non-Primitive Data Types in java

WBOY
WBOYOriginal
2024-08-30 15:15:38461browse

Non Primitive Data types are those data types in java that have the same size and provide additional methods to perform certain operations; in short, non-primitive data types in java refer to objects and are also named reference types; examples of non-primitive data types available in java include array, string, classes, and interfaces.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Below is the syntax showing how non-primitive data types are used in java:

Array: An Array in java is used as the following:

int[] intArray;                     // declaring array of type int
byte[] byteArray               // declaring array of type byte
long[] longArray;                     // declaring array of type long
short[] shortArray               // declaring array of type short
float[] floatArray;                     // declaring array of type float
long[] longArray               // declaring array of type long
char[] charArray               // declaring array of type char
ClassName[] classArray    // declaring array a particular class
Object[]  objectArray      // declaring array of objects

From the above, we can see that we can create an array of different data types or even objects and classes. Note that in an array, all elements should be of the same data type.

String: A Java String can be used like the following:

String str = "Edubca";         // declaring a string in java

Classes: Here is how a java class is created:

package <package name>;     // declaring package of class
public class <ClassName>{
//contains data and methods
}

Interfaces: Here is how an interface is created in java:

package <package name>;     // declaring package of interface
public interface <InterfaceName>{
//contains unimplemented methods and data if needed
}

Non Primitive Types in Java

The following are non-primitive data types available in java:

  • Array: An Array can be defined as a homogenous collection of elements having a fixed size. Arrays can store one or more values belonging to the same data type, and individual elements of an array can be accessed through an index. This means an array follows the index-based approach for accessing elements.
  • String: A string can be defined as a sequence of characters. A String is java is represented as an object of java.lang.String class. To create an instance of string in java, we need to create an object of java.lang.String class is an immutable and thread-safe class.
  • Class: A java class can be defined as a blueprint of data defined by the user and used to create objects. A class in java defines a set of properties or methods used to define an object’s state or behavior.
  • Interface: Interface in java is used to provide an abstraction over data. Similar to a class, interfaces contain data and methods, but methods declared inside an interface are by default abstract. Abstract methods are those methods that do not contain anybody; they only contain the method signature.

Examples of Non-Primitive Data Types in Java

Different examples are mentioned below

Example #1

In this example, we will show how to use array and string in java:

import java.util.*;
public class DataTypeDemo {
public static void main(String[] args) {
byte[] byteArray= {88,77,66,55};                 //declaring byte Array
int[] intArray= {20,15,10,4};                    // declaring integer Array
short[] shortArray= {6000,5000,4000,3000};       //declaring short Array
long[] longArray = {20000000000000L,30000000000000L,40000000000000L,50000000000000L};  //declaring long Array
float[] floatArray= {1.1f,1.2f,1.3f,1.4f};            // declaring float Array
double[] doubleArray = {29.94d,19.98d,20,87d};       // declaring double Array
boolean[] booleanArray= {true,false,true, true};       //declaring boolean Array
char[] charArray = {'A','B','C','D'};            // declaring character Array
System.out.println("Array Declared using Byte Data Type is  " + Arrays.toString(byteArray));
System.out.println("Array Declared using Integer Data Type is  " + Arrays.toString(intArray));
System.out.println("Array Declared using Short Data Type is  " + Arrays.toString(shortArray));
System.out.println("Array Declared using Long Data Type is  " + Arrays.toString(longArray));
System.out.println("Array Declared using Float Data Type is  " + Arrays.toString(floatArray));
System.out.println("Array Declared using Double Data Type is  " + Arrays.toString(doubleArray));
System.out.println("Array Declared using Boolean Data Type is  " + Arrays.toString(booleanArray));
System.out.println("Array Declared using Character Data Type is  " + Arrays.toString(charArray));
}
}

Output:

Non-Primitive Data Types in java

Example #2

In this example, we will see how classes and interfaces are used in java:

Here is how an interface is declared in java:

// declaring an interface
interface namePrinter{
// declaring abstract method (method without body)
String getName();
}
//creating java class implementing interface namePrinter
public class Main implements namePrinter{
public static void main(String[] args) {
Main main =new Main();
String name=main.getName();
System.out.println("Name returned from getName method is >> " + name );
}
// overriding method of an interface
@Override
public String getName() {
String name ="Edubca";
// TODO Auto-generated method stub
return name;
}
}

Output:

Non-Primitive Data Types in java

Conclusion

Properly understanding different data types is very important to learning any programming language. The above article explains the types in detail with examples and the significance of each data type.

The above is the detailed content of Non-Primitive Data Types in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn