Java Reference
In-Depth Information
9.20
Suppose that s1 and s2 are given as follows:
StringBuilder s1 = new StringBuilder( "Java" );
StringBuilder s2 = new StringBuilder( "HTML" );
Show the value of s1 after each of the following statements. Assume that the state-
ments are independent.
a. s1.append( " is fun" );
b. s1.append(s2);
c. s1.insert( 2 , "is fun" );
d. s1.insert( 1 , s2);
e. s1.charAt( 2 );
f. s1.length();
g. s1.deleteCharAt( 3 );
h. s1.delete( 1 , 3 );
i. s1.reverse();
j. s1.replace( 1 , 3 , "Computer" );
k. s1.substring( 1 , 3 );
l. s1.substring( 2 );
9.21
Show the output of the following program:
public class Test {
public static void main(String[] args) {
String s = "Java" ;
StringBuilder builder = new StringBuilder(s);
change(s, builder);
System.out.println(s);
System.out.println(builder);
}
private static void change(String s, StringBuilder builder) {
s = s + " and HTML" ;
builder.append( " and HTML" );
}
}
9.7 Command-Line Arguments
The main method can receive string arguments from the command line.
Key
Point
Perhaps you have already noticed the unusual header for the main method, which has the
parameter args of String[] type. It is clear that args is an array of strings. The main
method is just like a regular method with a parameter. You can call a regular method by pass-
ing actual parameters. Can you pass arguments to main ? Yes, of course you can. In the fol-
lowing examples, the main method in class TestMain is invoked by a method in A .
public class A {
public static void main(String[] args) {
String[] strings = { "New York" ,
"Boston" , "Atlanta" };
public class TestMain {
public static void main(String[] args)
{
for ( int i = 0 ; i < args.length; i++)
System.out.println(args[i]);
TestMain.main(strings);
}
}
}
}
A main method is just a regular method. Furthermore, you can pass arguments from the
command line.
 
 
Search WWH ::




Custom Search