0/30 Days Completed 0 Learning Days
Day 11 of 30 · Week 2 — Building Java Basics
Strings and common operations
1 Learn
A String is text. In Java, String is a class, so strings are objects with useful methods.
length() gives the number of characters, toUpperCase() makes capitals, charAt(i) gets one character.
Compare strings with equals(), not ==. == checks if they are the same object, not the same text.
2 See the Code
Java
public class Words {
public static void main(String[] args) {
String city = "Hyderabad";
System.out.println(city.length());
System.out.println(city.toUpperCase());
System.out.println(city.charAt(0));
System.out.println(city.equals("Hyderabad"));
}
}- Hyderabad has 9 characters.
- toUpperCase() returns a new String in capitals; city itself is unchanged.
- charAt(0) is the first character, H.
Output
9 HYDERABAD H true
3 Practice
- Ask yourself: print the last character of any word using charAt(word.length() - 1).
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. How should you compare two Strings' text?
2. "Java".length() is?
Step 5
Answer and check the Quick Check above to unlock this.