PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
Java's standard library already knows how to perform many common operations on a String. For String text = "Learn Java", length() counts the characters, contains("Java") checks whether the requested text occurs, and substring(6) creates the part beginning at index 6. You use each method by following its contract instead of implementing the operation yourself.
String text = "Learn Java";
int count = text.length();
boolean found = text.contains("Java");
String word = text.substring(6);
System.out.println(count); // 10
System.out.println(found); // true
System.out.println(word); // JavaA method's contract tells you what calls it accepts, what result it returns, and what effect you can observe. length() accepts no argument and returns the number 10 for this text. contains("Java") accepts the requested character sequence and returns true because those four characters occur starting at index 6. substring(6) returns a new String containing "Java".
For String text = "Learn Java", what do text.length(), text.contains("Java"), and text.substring(6) return, in that order?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An instance method is called through a particular object, so the expression before the dot supplies the object it acts through. In text.charAt(6), text is that object. A static method is called through its class name because it does not need one particular object as its receiver. Character.isUpperCase(text.charAt(6)) first gets the character at index 6, then asks the Character class whether that character is uppercase.
String text = "Learn Java";
char letter = text.charAt(6); // 'J'
boolean uppercase = Character.isUpperCase(letter); // true
boolean directCheck = Character.isUpperCase(text.charAt(6)); // trueThe two calls have different responsibilities. charAt(6) needs a String object because the character depends on the contents of text. isUpperCase(...) needs only the character supplied as its argument, so Character provides it as a static method. The expression before the dot tells you where Java looks for the method and, for an instance method, which object supplies the data.
Calling text.toUpperCase() calculates and returns an uppercase String, but it does not assign that result back to text. If the returned value is ignored, the next use of text still produces "Learn Java". String values cannot be edited in place, so a method that appears to transform a String gives you another value to use.
String text = "Learn Java";
text.toUpperCase();
System.out.println(text); // Learn Java
System.out.println(text.toUpperCase()); // LEARN JAVA
String upper = text.toUpperCase();
System.out.println(upper); // LEARN JAVAYou can use the returned String directly, store it in another variable, pass it to another method, or chain another instance method onto it. In text.substring(6).toUpperCase(), substring(6) returns "Java", and toUpperCase() runs on that returned String. The chain therefore produces "JAVA", while text remains "Learn Java".
Fix the code so it prints "LEARN JAVA".
text.toUpperCase();
System.out.println(text);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A signature tells you the method name, its accepted arguments, and its return type. Those details define which calls are valid and what Java can produce from them. For the String text = "Learn Java", the relevant signatures and results are:
int length()
char charAt(int index)
String substring(int beginIndex)
boolean contains(CharSequence sequence)
String toUpperCase()| CALL | RETURN TYPE | RESULT FOR TEXT |
|---|---|---|
| text.length() | int | 10 |
| text.charAt(6) | char | 'J' |
| text.substring(6) | String | "Java" |
| text.contains("Java") | boolean | true |
| text.toUpperCase() | String | "LEARN JAVA" |
The valid String indices in text are 0 through 9. Therefore text.charAt(6) returns 'J', but text.charAt(10) is outside the String and cannot return a character. Java throws StringIndexOutOfBoundsException for that call. The method signature permits an int argument, but an argument having the right type is not automatically a valid value. The contract also limits the range that value may have.
The same contract-based reading applies to the other calls. length() has no argument, contains(...) needs a character sequence to search for, substring(6) begins at the supplied index, and toUpperCase() returns an uppercase String. Reading the signature before calling a library method helps you predict both its result and the boundary where Java reports an error.