PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java Strings

← All stacks

Theory

73/270

Java Strings

String is text in Java — a sequence of characters. Create: String s = "Java";. That looks simple, and then interviews fail people on it. Two rules from day one: String cannot change in place (immutable), and you compare letters with equals, not ==.

Immutable means this: String a = "Java"; a.toUpperCase(); print a; still prints Java. toUpperCase returns a new String. The old one stays. If you want the upper text, write a = a.toUpperCase(); or store it in another variable. Freshers think the method edits a. It does not.

Let’s take comparison on the board. String a = new String("Hi"); String b = new String("Hi"); print a == b; print a.equals(b);. == is false — two different objects in memory. equals is true — same letters. User input from Scanner is like new String. Never compare it with ==. Literals "Hi" == "Hi" can be true because of the String Pool. That pool trick does not save you in real input.

Everyday APIs you should write from memory: length(), charAt(0), substring(0, 2), contains("va"), equalsIgnoreCase. Index starts at 0. substring end index is exclusive: "Java".substring(0, 2) is "Ja", not "Jav". Say that before you code.

Interview line: immutable + equals for content + pool for literals. If they ask only one trap, give == vs equals. If they ask why immutable, say pool + HashMap keys + security, then point to StringBuilder when you need to change text many times.

Diagram
s = "Java"
       │ toUpperCase()
       ▼
  new "JAVA"     (old s stays "Java")
Exam tip

== vs equals is the #1 String interview trap.

Example

public class Main {
  public static void main(String[] args) {
    String a = "Java";
    String b = "Java";
    System.out.println(a.equals(b));
    System.out.println(a.length());
  }
}

Java Strings — output: true then 4. "Java".equals("Java") is content; length() is 4.

Short notes

  • DefString is immutable text. Methods return a new String.
  • RuleCompare content with equals, not ==.
  • Rememberlength, charAt, substring. Literals can use the String Pool.
  • Trap== on user input strings is almost always wrong.

Questions

1

Is String mutable?

2

How do you compare two strings?

3

What does == compare?

Previous← Dynamic Method Dispatch in JavaNextWhy String is Immutable in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.