Java Reference
In-Depth Information
c. s1 == s4
d. s1.equals(s3)
e. s1.equals(s4)
f. "Welcome to Java" .replace( "Java" , "HTML" )
g. s1.replace( 'o' , 'T' )
h. s1.replaceAll( "o" , "T" )
i. s1.repla ceFirst( "o" , "T" )
j. s1.toCharArray()
10.16
To create the string Welcome to Java , you may use a statement like this:
String s = "Welcome to Java" ;
or:
String s = new String( "Welcome to Java" );
Which one is better? Why?
10.17
What is the output of the following code?
String s1 = "Welcome to Java" ;
String s2 = s1.replace( "o" , "abc" );
System.out.println(s1);
System.out.println(s2);
10.18
Let s1 be "Welcome" and s2 be "welcome" . Write the code for the following
statements:
a. Replace all occurrences of the character e with E in s1 and assign the new string
to s2 .
b. Split Welcome to Java and HTML into an array tokens delimited by a space
and assign the first two tokens into s1 and s2 .
10.19
Does any method in the String class change the contents of the string?
10.20
Suppose string s is created using new String() ; what is s.length() ?
10.21
How do you convert a char , an array of characters, or a number to a string?
10.22
Why does the following code cause a NullPointerException ?
1 public class Test {
2
private String text;
3
4
public Test(String s) {
5
String text = s;
6 }
7
8 public static void main(String[] args) {
9 Test test = new Test( "ABC" );
10 System.out.println(test.text.toLowerCase());
11 }
12 }
10.23 What is wrong in the following program?
1 public class Test {
2 String text;
3
 
 
Search WWH ::




Custom Search