If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Java, non-primitive data types are also known as reference types. Unlike primitive data types that store the actual values directly, reference types store references or memory addresses to the actual data, which reside in the heap memory. Non-primitive data types include objects, arrays, and interfaces. Here's a brief overview of each:
Example:
javaCopy code
// Defining a class
class Person {
String name;
int age;
}
// Creating objects
Person person1 = new Person();
Person person2 = new Person();
// Assigning values to object properties
person1.name = "Alice";
person1.age = 30;
person2.name = "Bob";
person2.age = 25;
Example:
javaCopy code
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
Example:
javaCopy code
// Interface definition
interface Animal {
void makeSound();
}
// Implementing the interface
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
In the example above, Dog
and Cat
are classes that implement the Animal
interface, and they both provide their own implementation for the makeSound()
method.
Non-primitive data types are more complex than primitive data types and can hold a larger amount of data. They play a crucial role in creating sophisticated applications and data structures in Java.
Comments: 0