+91 81900 98289

Java Strings

A string literal sits in double quotes. length() returns how many UTF-16 code units are in the text. charAt starts at 0, so the first character of "Java" is J and the last index is length() - 1.

Strings cannot be changed after they are created. Methods such as toUpperCase return a new string. The original variable still points at the old text until you assign the result back.

Example

Java has four characters. Index 0 is the letter J.

public class Words { public static void main(String[] args) { String course = "Java"; System.out.println(course.length()); System.out.println(course.charAt(0)); } }
Compare text with course.equals("Java"). equalsIgnoreCase is the version that ignores capitals. == asks whether two variables point at the same object, which is the wrong question for text you built separately.