In this article String in Java we give the information about String is an object representing a sequence of characters.

String in Java

Definition

A String in Java is a sequence of characters enclosed within double quotes (” “).
It is a non-primitive data type because it is a class object that references memory in the heap or String Constant Pool (SCP).

In Java, strings are objects of the java.lang.String class.

Example:

class StringTest {

    public static void main(String[] args) {

        String name = “Rajveer”;

        System.out.println(name);

    }

}

Output:

Rajveer

Memory Concept

  • String is a reference data type.
  • The reference variable (object reference) is stored in stack memory.
  • The actual String object is stored in heap memory (or String Constant Pool).

Classes for String Handling

  1. String – Immutable (cannot be changed)
  2. StringBuffer – Mutable and thread-safe
  3. StringBuilder – Mutable and faster (not thread-safe)

Ways to Create a String Object

  1. Using String Literal

When a string is created using double quotes (” “), the JVM first checks the String Constant Pool (SCP).

  • If the string already exists in SCP → it reuses the existing object.
  • If not → it creates a new one in the pool.

Example:

String s = “hello”;

Memory efficient, as no duplicate objects are created.

  1. Using new Keyword

When a string is created using new, a new object is always created in the heap memory, even if the same value exists in SCP.

Example:

String s = new String(“hello”);

This creates two objects:

  1. One in Heap memory
  2. One in String Constant Pool

Syntax:

String stringName = new String(“string_value”);

Example:

public class MyClass {

    public static void main(String[] args) {

        String text = “Hello World”;

        System.out.println(text);

    }

}

Output:

Hello World

Important Notes

  • Using new keyword: Two objects → one in Heap, one in SCP.
  • Using literal: One object → only in SCP.
  • Strings in SCP are not garbage-collected because they are maintained internally by JVM.

Common String Methods

Method Description Example Output
length() Returns number of characters “Hello”.length() 5
toUpperCase() Converts to uppercase “java”.toUpperCase() JAVA
toLowerCase() Converts to lowercase “HELLO”.toLowerCase() hello
indexOf(‘W’) Finds position of character “Hello World”.indexOf(‘W’) 6

Example:

public class MyClass {

    public static void main(String[] args) {

        String text = “HELLO World”;

        System.out.println(text.length());

        System.out.println(text.toUpperCase());

        System.out.println(text.toLowerCase());

        System.out.println(text.indexOf(‘W’));

    }

}

Output:

11

HELLO WORLD

hello world

6

String Concatenation

Joining two or more strings is called Concatenation.

  1. Using + Operator

String s1 = “Ram”;

String s2 = “Singh”;

System.out.println(s1 + s2);      // RamSingh

System.out.println(s1 + ” ” + s2); // Ram Singh

  1. Using concat() Method

System.out.println(s1.concat(s2));       // RamSingh

System.out.println(s1.concat(” “).concat(s2)); // Ram Singh

Output:

RamSingh

Ram Singh

RamSingh

Ram Singh

Array in Java

Definition

An Array in Java is a data structure used to store multiple values of the same type in a single variable.

Features

  • Fixed Size: Once declared, the size cannot change.
  • Same Data Type: All elements must be of the same type.
  • Indexing: Starts at 0 and ends at length – 1.

Declaration and Initialization

int[] arr;           // Declaration

arr = new int[5];    // Initialization

int[] arr = new int[5]; // Declaration + Initialization

int[] arr = {10, 20, 30, 40, 50}; // With values

Example: 1D Array

public class ArrayExample {

    public static void main(String[] args) {

        int[] arr = {10, 20, 30, 40, 50};

        for (int i = 0; i < arr.length; i++) {

            System.out.println(“Element at index ” + i + ” : ” + arr[i]);

        }

    }

}

Output:

Element at index 0 : 10

Element at index 1 : 20

Element at index 2 : 30

Element at index 3 : 40

Element at index 4 : 50

Multi-Dimensional Array

A multi-dimensional array is an array of arrays.
The most common is the 2D array (matrix form).

Example: 2D Array

public class TwoDArrayExample {

    public static void main(String[] args) {

        int[][] arr = {

            {1, 2, 3},

            {4, 5, 6},

            {7, 8, 9}

        };

        for (int i = 0; i < 3; i++) {

            for (int j = 0; j < 3; j++) {

                System.out.print(arr[i][j] + ” “);

            }

            System.out.println(); // New line after each row

        }

    }

}

Output:

1 2 3

4 5 6

7 8 9

Summary

Concept Description
String Sequence of characters, stored as an object.
String Literal Stored in String Constant Pool.
new Keyword Creates new String in heap memory.
Array Fixed-size collection of same-type elements.
1D Array Single row of elements.
2D Array Matrix-like structure (rows × columns).

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *