Given a String variable bookTitle that has the value of “Introduction to Java”,
ID: 3703093 • Letter: G
Question
Given a String variable bookTitle that has the value of “Introduction to Java”, use a regular for loop to output each individual character, with one character per line.
Can we use if( s1 = = s2 ) to compare whether the two String object s1 and s2 has the same string content? If not, then how to compare two string variables?
What are the method return values of the following method invocations? Assume String s1 = "Forrest";
s1.length()
s1.indexOf('r')
s1.substring(2)
String.valueOf(14)
String.valueOf(14.5)
Given a String variable bookTitle that has the value of “Introduction to Java”, use a regular for loop to output each individual character, with one character per line.
Explanation / Answer
Given a String variable bookTitle that has the value of “Introduction to Java”, use a regular for loop to output each individual character, with one character per line.
Answer:
for(int i=0;i<bookTitle.length();i++) {
System.out.println(bookTitle.charAt(i));
}
Can we use if( s1 = = s2 ) to compare whether the two String object s1 and s2 has the same string content? If not, then how to compare two string variables?
Answer: No
if(s1.equals(s2)) {
}
Assume String s1 = "Forrest";
s1.length() will return 7
s1.indexOf('r') will return 2
s1.substring(2) will return "rrest"
String.valueOf(14) will return "14"
String.valueOf(14.5) will return "14.5"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.