惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
月光博客
月光博客
博客园_首页
博客园 - 叶小钗
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
量子位
小众软件
小众软件
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
IT之家
IT之家
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
String in Java - Complete guide for Beginners
Jayashree · 2026-05-20 · via DEV Community

Jayashree

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