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
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 }
The following are non-primitive data types available in java:
Different examples are mentioned below
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:
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:
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!