DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSJAVA FUNDAMENTALS

Standard library methods return results, they do not rewrite String variables

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 28

A library method gives you a documented result without making you rewrite its algorithm

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.

JAVAThree standard library methods perform three common String operations.
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);   // Java

A 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".

CHECKPOINT 1Not answered

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.

The expression before the dot determines how a library method is called

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.

JAVAThe instance call uses text, while the static call uses the Character class name.
String text = "Learn Java";

char letter = text.charAt(6);                 // 'J'
boolean uppercase = Character.isUpperCase(letter); // true
boolean directCheck = Character.isUpperCase(text.charAt(6)); // true

The 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.

A returned String does not replace the String that received the call

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.

JAVAThe returned uppercase String must be used; ignoring it leaves text unchanged.
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 JAVA

You 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".

The evaluation of text.substring(6).toUpperCase() for String text = "Learn Java"The variable text contains "Learn Java". Calling substring(6) produces the intermediate String "Java". Calling toUpperCase() on that intermediate String produces "JAVA". A separate marker shows that text still contains "Learn Java" after both calls."Learn Java""Java""JAVA"substring(6)toUpperCase()text still "Learn Java"text (original)intermediate resultnew resultEach call returns a new String; text itself is unchanged.
The chain creates results without changing text.
CHECKPOINT 2Not answered

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 method signature fixes both valid calls and failure boundaries

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:

JAVAThe signatures show each method's return type and required argument shape.
int length()
char charAt(int index)
String substring(int beginIndex)
boolean contains(CharSequence sequence)
String toUpperCase()
CALLRETURN TYPERESULT FOR TEXT
text.length()int10
text.charAt(6)char'J'
text.substring(6)String"Java"
text.contains("Java")booleantrue
text.toUpperCase()String"LEARN JAVA"
Method contracts for text

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.