DEV Community

Jayashree
Jayashree

Posted on

String in Java - Complete guide for Beginners

Introduction

In Java, a String is used to store text data.

Example:

String name = "Jay";
Enter fullscreen mode Exit fullscreen mode

Here "Jay" is a String.

Strings are one of the most commonly used data types in Java because almost every application works with text like usernames, emails, messages, passwords, etc.

What is String in Java?

A String is a sequence of characters.

Example:

String word = "Hello";
Enter fullscreen mode Exit fullscreen mode

Characters:

H e l l o
Why String is Important?

Strings are used everywhere:

  • Login systems
  • Search functionality
  • Form inputs
  • APIs
  • Database values
  • Chat applications

Without Strings, handling text is impossible.

How to Create a String?

1. Using String Literal

String s1 = "Hello";
Enter fullscreen mode Exit fullscreen mode

This is the most commonly used way.

Java stores it inside the String Pool.

2. Using new Keyword

String s2 = new String("Hello");
Enter fullscreen mode Exit fullscreen mode

This creates a new object in heap memory.

Difference Between Literal and new Keyword

String Literal new Keyword
Stored in String Pool Stored in Heap
Memory efficient Creates separate object
Faster Slightly slower

Example:

String a = "Java";
String b = "Java";


System.out.println(a == b);
Enter fullscreen mode Exit fullscreen mode

Output:

true

  • Because both refer to the same object.
String a = new String("Java");
String b = new String("Java");

System.out.println(a == b);
Enter fullscreen mode Exit fullscreen mode

Output:

false

  • Because two different objects are created.

String is Immutable

Immutable means:

Once a String is created, it cannot be changed.

Example:

String s = "Hello";

s.concat(" World");

System.out.println(s);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello

  • Because concat() creates a new object.

Correct way:

s = s.concat(" World");

System.out.println(s);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World

Important String Methods

length()

Returns total characters.

String s = "Java";

System.out.println(s.length());
Enter fullscreen mode Exit fullscreen mode

Output:

4

charAt()

Gets character by index.

String s = "Java";

System.out.println(s.charAt(1));
Enter fullscreen mode Exit fullscreen mode

Output:

a

toUpperCase()

String s = "java";

System.out.println(s.toUpperCase());
Enter fullscreen mode Exit fullscreen mode

Output:

JAVA

toLowerCase()

String s = "JAVA";

System.out.println(s.toLowerCase());
Enter fullscreen mode Exit fullscreen mode

Output:

java

equals()

Compares values.

String a = "Java";
String b = "Java";

System.out.println(a.equals(b));
Enter fullscreen mode Exit fullscreen mode

Output:

true

equalsIgnoreCase()

Ignores uppercase/lowercase.

String a = "java";
String b = "JAVA";

System.out.println(a.equalsIgnoreCase(b));
Enter fullscreen mode Exit fullscreen mode

Output:

true

contains()

Checks whether text exists.

String s = "Java Programming";

System.out.println(s.contains("Java"));
Enter fullscreen mode Exit fullscreen mode

Output:

true

startsWith()

String s = "Java";

System.out.println(s.startsWith("Ja"));
Enter fullscreen mode Exit fullscreen mode

Output:

true

endsWith()

String s = "Java";

System.out.println(s.endsWith("va"));
Enter fullscreen mode Exit fullscreen mode

Output:

true

replace()

String s = "Java";

System.out.println(s.replace('a', 'o'));
Enter fullscreen mode Exit fullscreen mode

Output:

Jovo

substring()

  • Extracts part of String.
String s = "Programming";

System.out.println(s.substring(0, 6));
Enter fullscreen mode Exit fullscreen mode

Output:

Progra

split()

  • Splits String into parts.
String s = "Java Python React";

String arr[] = s.split(" ");

for(String word : arr)
{
    System.out.println(word);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Java
Python
React

== vs equals()

==

  • Checks memory reference.

equals()

  • Checks actual content.

Example:

String a = new String("Java");
String b = new String("Java");

System.out.println(a == b);
System.out.println(a.equals(b));
Enter fullscreen mode Exit fullscreen mode

Output:

false
true
String Pool

Java stores String literals in a special memory area called:

String Constant Pool

Example:

String a = "Java";
String b = "Java";
Enter fullscreen mode Exit fullscreen mode
  • Both point to the same object.
  • This saves memory.

StringBuilder

Since String is immutable, repeated modifications create many objects.

To solve this, Java provides:

StringBuilder

Example:

StringBuilder sb = new StringBuilder("Hello");

sb.append(" World");

System.out.println(sb);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World
StringBuffer

Similar to StringBuilder but thread-safe.

StringBuilder StringBuffer
Faster Slower
Not synchronized Synchronized

Common Interview Programs

Reverse String

String s = "Java";

for(int i = s.length()-1; i >= 0; i--)
{
    System.out.print(s.charAt(i));
}
Enter fullscreen mode Exit fullscreen mode

Output:

avaJ

Palindrome String

String s = "madam";
String rev = "";

for(int i = s.length()-1; i >= 0; i--)
{
    rev += s.charAt(i);
}

if(s.equals(rev))
{
    System.out.println("Palindrome");
}
else
{
    System.out.println("Not Palindrome");
}
Enter fullscreen mode Exit fullscreen mode

Output

Palindrome

And so on

Conclusion

String is one of the most important concepts in Java.

To become strong in Java:

  • Understand immutability
  • Learn String methods
  • Practice String programs
  • Know String Pool concept
  • Learn difference between == and equals()

Mastering Strings will help a lot in:

  • Interviews
  • Backend Development
  • Spring Boot
  • Problem Solving
  • Real-world Applications

Top comments (0)