DEV Community

KIRUBAGARAN .K
KIRUBAGARAN .K

Posted on

What is String in Java?

A String in Java is an object used to store and work with text, such as words, sentences, or numbers written as characters. It belongs to the String class, not a primitive data type. One important feature of a String is that it is immutable, which means once it is created, its value cannot be changed.

String is an Object
Unlike primitive data types (int, char), a String is a class in Java.
String s = "Hello";

Strings are Immutable
Once a String is created, it cannot be changed.
String s = "Hello";
s.concat(" World");
System.out.println(s)// Hello;

Created in Two Ways
String s = "Java";// Using Literal
String s = new String("Java");// Using new keyword

String Comparison
String a = "Java";
String b = new String("Java");
...
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true

== .. compares memory
equals() .. compares value

Common String Operations

  • Find length → length()
  • Compare → equals()
  • Convert case → toUpperCase()
  • Remove spaces → trim()
  • Split → split()
  • Replace → replace()

A String in Java is an immutable object used to store and manipulate text.

Top comments (0)