In Java, non-primitive data types are also known as reference types or objects. These data types do not contain the actual data, but rather they refer to an instance of a class that contains the data. Here are some common non-primitive data types in Java:
- String: This data type is used to store a sequence of characters. Strings are immutable, meaning they cannot be changed once they are created.
- Arrays: Arrays are used to store a fixed-size collection of elements of the same type. They can be one-dimensional, two-dimensional, or multi-dimensional.
- Classes: Classes are used to define custom data types. They contain data members and methods that define the behavior of the object.
- Interfaces: Interfaces define a set of methods that a class must implement. They are used to define a contract between different parts of a program.
- Enumerations: Enumerations are used to define a set of named constants. They are often used to represent a fixed set of options, such as the days of the week.
- Collections: Collections are used to store and manipulate collections of objects. They include classes such as ArrayList, HashSet, and HashMap.
To use these non-primitive data types, you must first declare a variable of the appropriate type. For example, to declare a String variable, you would use the following syntax:
String myString;
Once the variable is declared, you can assign a value to it using the = operator, like this:
myString = "Hello, world!";
You can also create objects using the new keyword like this:
MyClass myObject = new MyClass();
This creates a new instance of the MyClass class and assigns it to the myObject variable. Once you have an object or variable of a non-primitive data type, you can access its properties and methods using the . operator, like this:
myObject.myMethod();
This calls the myMethod() method on the myObject instance of the MyClass class.
Here are some examples of Java Non-Primitive Data Types:
- String: A String is a sequence of characters. In Java, Strings are objects of the String class. Strings are widely used in Java programs to represent text.
String myString = "Hello, World!";
- Arrays: An array is a collection of elements of the same data type. In Java, arrays are objects of the Array class. Arrays are widely used in Java programs to store and manipulate data.
int[] myArray = {1, 2, 3, 4, 5};
- Classes: A class is a blueprint for creating objects. In Java, classes are objects of the Class class. Classes are widely used in Java programs to model complex data types.
public class MyClass {
private int myInt;
public void setMyInt(int value) {
myInt = value;
}
public int getMyInt() {
return myInt;
}
}
MyClass myObject = new MyClass();
myObject.setMyInt(42);
- Interfaces: An interface is a collection of abstract methods that can be implemented by any class. In Java, interfaces are objects of the Interface class. Interfaces are widely used in Java programs to define a common set of methods that can be implemented by different classes.
public interface MyInterface {
public void myMethod();
}
public class MyClass implements MyInterface {
public void myMethod() {
System.out.println("Hello, World!");
}
}
MyInterface myObject = new MyClass();
myObject.myMethod();
- Enumerations: An enumeration is a set of named constants. In Java, enumerations are objects of the Enum class. Enumerations are widely used in Java programs to represent a fixed set of options.
public enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
DayOfWeek today = DayOfWeek.MONDAY;
- Collections: A collection is a group of objects. In Java, collections are objects of the Collection class. Collections are widely used in Java programs to store and manipulate groups of related objects.
import java.util.ArrayList;
ArrayList<String> myCollection = new ArrayList<String>();
myCollection.add("apple");
myCollection.add("banana");
myCollection.add("orange");