-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStrings.java
executable file
·46 lines (37 loc) · 1.66 KB
/
Strings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Strings {
public static void main(String[] args) {
// These are all legal ways of initializing Strings.
String foo = new String("foo");
String bar = "bar"; // The most common way
String baz;
baz = "baz";
// Strings can be concatenated with other Strings
String bam = foo + bar + baz;
// Strings can also be concatenated with other objects
String s = bam + (42 + 1);
String t = 42 + 1 + bam; // t is "42foobarbaz"
System.out.println("bam=\"" + bam + "\"");
System.out.println("s=\"" + s + "\"");
System.out.println("t=\"" + t + "\"");
/* Strings are immutable. None of the previous operations
modified the original Strings */
System.out.println("foo=\"" + foo + "\"");
System.out.println("bar=\"" + bar + "\"");
System.out.println("baz=\"" + baz + "\"");
System.out.println("bam=\"" + bam + "\"");
// The String class contains many useful methods
int bPos = bam.indexOf("b");
System.out.println("In bam, \"b\" first occurs at index " + bPos);
System.out.println("bam.substring(3, 6)= \""
+ bam.substring(3, 6) + "\"");
System.out.println("\"a\".compareTo(\"z\")" + "returns "
+ "a".compareTo("z"));
// The empty String is just that:
String empty = "";
System.out.println("empty has length " + empty.length());
int aPos = empty.indexOf("a");
System.out.println("In empty, \"a\" first occurs at index " + aPos);
String boom = null;
int aPosInBoom = boom.indexOf("a"); // NullPointerException
}
}