Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungWhy Claude Code keeps writing shell commands that fail on your Mac(20.09.2026 um 21:06 Uhr)
Sichere Programmierungllms.txt v2: What the Spec Says, and What 137,000 Domains Show(20.09.2026 um 21:17 Uhr)
Sicherheitslücken (CVE)NiceTryGPT: Less pattern matching. More actual hacking.(20.09.2026 um 21:19 Uhr)
IT Security VideoActivities BoF (kde2026)(20.09.2026 um 00:00 Uhr)
IT Security Toolsirdoc-app(20.09.2026 um 20:33 Uhr)
Sichere ProgrammierungWhy Claude Code keeps writing shell commands that fail on your Mac(20.09.2026 um 21:06 Uhr)
Sichere Programmierungllms.txt v2: What the Spec Says, and What 137,000 Domains Show(20.09.2026 um 21:17 Uhr)
Sicherheitslücken (CVE)NiceTryGPT: Less pattern matching. More actual hacking.(20.09.2026 um 21:19 Uhr)
IT Security VideoActivities BoF (kde2026)(20.09.2026 um 00:00 Uhr)
IT Security Toolsirdoc-app(20.09.2026 um 20:33 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

String in Java - Complete guide for Beginners

Reagiere als Erste:r — dein Feedback zählt!

Introduction

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

Example:

String name = "Jay";

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";

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";

This is the most commonly used way.

Java stores it inside the String Pool.

2. Using new Keyword

String s2 = new String("Hello");

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);

Output:

true

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

System.out.println(a == b);

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);

Output:

Hello

  • Because concat() creates a new object.

Correct way:

s = s.concat(" World");

System.out.println(s);

Output:

Hello World

Important String Methods

length()

Returns total characters.

String s = "Java";

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

Output:

4

charAt()

Gets character by index.

String s = "Java";

System.out.println(s.charAt(1));

Output:

a

toUpperCase()

String s = "java";

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

Output:

JAVA

toLowerCase()

String s = "JAVA";

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

Output:

java

equals()

Compares values.

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

System.out.println(a.equals(b));

Output:

true

equalsIgnoreCase()

Ignores uppercase/lowercase.

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

System.out.println(a.equalsIgnoreCase(b));

Output:

true

contains()

Checks whether text exists.

String s = "Java Programming";

System.out.println(s.contains("Java"));

Output:

true

startsWith()

String s = "Java";

System.out.println(s.startsWith("Ja"));

Output:

true

endsWith()

String s = "Java";

System.out.println(s.endsWith("va"));

Output:

true

replace()

String s = "Java";

System.out.println(s.replace('a', 'o'));

Output:

Jovo

substring()

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

System.out.println(s.substring(0, 6));

Output:

Progra

split()

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

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

for(String word : arr)
{
    System.out.println(word);
}

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));

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";
  • 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);

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));
}

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");
}

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
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten String in Java - Complete guide for Beginners

Thematisch verwandte Begriffe: String, Java, Complete, guide · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick