JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
String Methods
MirrorLesson 8 of 10
Lesson 8

String Methods

String manipulation and formatting

Introduction

In this lesson, you'll learn about string methods in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with string manipulation and formatting.

JV
In Java:

Java has its own approach to string manipulation and formatting, which we'll explore step by step.

The Java Way

Let's see how Java handles this concept. Here's a typical example:

JV
Java Example
String s = "Hello, World!";
s.length();              // 13  — note: method, not property
s.toUpperCase();         // "HELLO, WORLD!"
s.toLowerCase();         // "hello, world!"
s.contains("World");     // true
s.startsWith("Hello");   // true
s.indexOf("o");          // 4
s.substring(7, 12);      // "World"
s.replace("World","Java"); // "Hello, Java!"
s.split(", ");           // String[]{"Hello","World!"}
s.strip();               // removes whitespace (Java 11+)

// String.format (printf-style)
String name = "Alice";
String.format("Hello, %s!", name); // "Hello, Alice!"
// Java 15+ text blocks
String json = """
    {"name": "Alice"}
    """;

// Padding (Java 11+)
"5".repeat(3);            // "555"
String.format("%3s","5").replace(' ','0'); // "005"

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
const s = "Hello, World!";
s.length;              // 13
s.toUpperCase();       // "HELLO, WORLD!"
s.toLowerCase();       // "hello, world!"
s.includes("World");   // true
s.startsWith("Hello"); // true
s.indexOf("o");        // 4
s.slice(7, 12);        // "World"
s.replace("World","JS");// "Hello, JS!"
s.split(", ");         // ["Hello", "World!"]
s.trim();              // removes whitespace

// Template literals
const name = "Alice";
`Hello, ${name}!`;    // "Hello, Alice!"

// Padding
"5".padStart(3, "0");  // "005"
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

length is a method length() in Java, not a property

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

substring(start, end) vs JS slice(start, end) — same semantics

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

contains() vs JS includes(); indexOf() works the same

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

String.format() uses printf-style %s/%d/%f placeholders

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java strings are immutable objects; all methods return new strings

Step-by-Step Breakdown

1. length() is a Method

In Java, String.length() is a method call, not a property access. Arrays use .length (no parens), but String uses .length().

JS
JavaScript
s.length
JV
Java
s.length()   // String
arr.length   // array (no parens)
Common Pitfall
Mixing up s.length vs s.length() is a very common Java mistake.

2. Substrings

substring(start, end) returns [start, end) — same as JS slice. But JS also supports negative indices; Java does not.

JS
JavaScript
s.slice(7, 12)
JV
Java
s.substring(7, 12)

3. String.format

Use String.format() for formatted strings with %s (string), %d (integer), %f (float), %.2f (2 decimal places).

JS
JavaScript
`Balance: ${balance.toFixed(2)}`
JV
Java
String.format("Balance: %.2f", balance)

4. String Comparison

Never use == to compare Java strings — it compares references, not content. Always use .equals() or .equalsIgnoreCase().

JS
JavaScript
s1 === s2
JV
Java
s1.equals(s2)
s1.equalsIgnoreCase(s2)
Common Pitfall
s1 == s2 checks reference equality in Java, not content. This is a notorious source of bugs.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • length is a method length() in Java, not a property
  • substring(start, end) vs JS slice(start, end) — same semantics
  • contains() vs JS includes(); indexOf() works the same
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • length() is a method; substring(), contains(), replace() all return new strings
  • String.format("%s %d %.2f", ...) for printf-style formatting
  • Always use .equals() for string comparison, never ==
  • Java 15+ text blocks (triple-quote) replace messy string concatenation
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext