Command Palette

Search for a command to run...

Back to the lesson: Topic 1.10 — Strings: The Basics
Core Java · Example 2 of 4

+ joins left to right

A String is an object that holds a fixed sequence of characters. You create one with double quotes, join Strings with +, read characters by position starting at 0, compare contents with equals (never ==), and every method that seems to change a String actually returns a new one.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Given String word = "programming";, print its length, its first and last characters, and the middle part without the first and last characters.

02

Given String email = "asha.k@example.com";, use indexOf and substring to print the user part (before @) and the domain (after @).

03

Given String a = "Level";, print whether it reads the same forwards and backwards ignoring case, by comparing its lower-case form with the reverse built from charAt in a simple sequence (the word has 5 letters).

Explain it without notes

01

Why is String immutable, and what does that let the JVM do?

02

Explain the difference between == and equals for Strings, and why == sometimes returns true for equal text.

03

What do 1 + 2 + "3" and "1" + 2 + 3 evaluate to, and why?

04

What is the difference between "", " " and null, and how do you test for each?

+ joins left to right
Sign in to run this example in your browser.

Expected output

33
123
15
195!
ab
score: 9.5, passed: true
value: null