← Chapter 2 — Core Syntax & Data

8. Non-Primitive Types & Strings

mixed

Description

Non-primitive values are objects or references to objects. Strings, arrays, classes, and interfaces are common examples.

A String represents text and offers useful methods. Unlike a primitive, it can refer to an object and has behavior such as length and substring.

Subtopics

Reference values

Description

A reference points to an object.

Explanation

A reference can also be null when it points to no object.

Example

String name = "Aditi";

String methods

Description

Strings provide operations for text.

Explanation

String objects are immutable, so methods return new strings.

Example

int letters = name.length();

Explanation

A String is written with double quotes. Because strings are objects, compare their text with equals rather than ==, which compares references.

Example

public class StringTypeDemo {
    public static void main(String[] args) {
        String learner = "Aditi";
        System.out.println(learner.length());
    }
}

Key points

  • Strings are objects, not primitives.
  • References can point to objects.
  • Use equals to compare string content.

Open reference