Non Primitive type

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 4:- Non Primitive type

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:

  1. Objects: Objects are instances of classes in Java. A class is a blueprint that defines the properties and behaviors of objects. When you create an object, it allocates memory in the heap and holds the data defined by the class. You can create multiple objects from a single class.

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;

  1. Arrays: Arrays are used to store multiple values of the same data type. They provide a way to store data in a contiguous block of memory. In Java, arrays have a fixed size that needs to be specified during declaration, and their size cannot be changed later.

Example:

javaCopy code

int[] numbers = new int[5]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50;

  1. Interfaces: An interface is a reference type that contains abstract methods and constants. It provides a contract that implementing classes must adhere to. In Java, a class can implement multiple interfaces, allowing for multiple inheritance of types.

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.

2. Variable And Data Types

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support