Example:
"abc123" "Hello World" "Hello, what is your name ?" |
|
|
The String data type is a class that is constructed using the char data type:
|
|
Example:
"Hello World" |
|
|
Escape sequence | Denoted character | Example |
---|---|---|
\t | Tab | "\t" (string with a TAB character) |
\n | New line (NL) | "\n" (string with a NL character) |
\\ | Backslash (\) | "\\" (string with a \) |
\" | Double quote (") | "\"" (string with a ") |
public class Escape01 { public static void main(String[] args) { System.out.println("He said \t \"Hello\" \nI said \" \\ \" \n"); } } |
Output:
He said "Hello" I said " \ " |
String NameOfVariable ; |
Notes:
|
public class String01 { public static void main(String[] args) { String a; a = "Hello World"; // a = location of "Hello World" System.out.println(a); // Print the string at a } } |
How to run the program:
|
|
|
|
Explanation:
|
|
|
Explanation:
|
|
|
public class String02 { public static void main(String[] args) { String a; a = "abc"; // a = location + length of "abc" System.out.println(a); // Print the string at a System.out.println(a.length()); // Print the length of the string at a } } |
How to run the program:
|
|
|
|
|
Answer:
|
000...00001100 || |+--------------> 22 = 4 +---------------> 23 = 8 ----- 12 |
On the other hand, the string "12" is stored using the Unicode scheme as:
00000000 00110001 (code for '1') 00000000 00110010 (code for '2') |
Conclusion:
|
|
String a; int x; a = Integer.toString(x) ; // Returns the String representation // for the integer value in variable x |
String a; double x; a = Double.toString(x) ; // Returns the String representation // for the double value in variable x |
String a; float x; a = Float.toString(x) ; // Returns the String representation // for the float value in variable x |
public class String04 { public static void main(String[] args) { String s1, s2; // Strings int x = 12; double y = 3.1415; System.out.print("x + y = "); System.out.println( x + y ); // Add numbers s1 = Integer.toString(x); s2 = Double.toString(y); System.out.print("s1 + s2 = "); System.out.println( s1 + s2 ); // + on String is concatenation } } |
Output:
x + y = 15.1415 s1 + s2 = 123.1415 (Concatenation of Strings !) |
How to run the program:
|
|
Example:
"abc" + "12" returns the string: "abc12" |
public class String03 { public static void main(String[] args) { String a, b, c; a = "abc"; b = "12"; c = a + b; // String concatenation System.out.println("a = " + a); // String concatenation !! System.out.println("b = " + b); // String concatenation !! System.out.println("c = " + c); // String concatenation !! } } |
How to run the program:
|
00001100 || |+--------------> 22 = 4 +---------------> 23 = 8 ----- 12 |
On the other hand, the string "12" is stored using the Unicode scheme as 2 binary numbers:
00110001 (code for '1') 00110010 (code for '2') |
|
String s; int x; s = Integer.toString(x) ; // Returns the String representation // for the integer value in variable x x = Integer.parseInt(s) ; // Returns the binary number representation // for the numeric string in variable s |
String s; double x; s = Double.toString(x) ; // Returns the String representation // for the double value in variable x x = Double.parseDouble(s) ; // Returns the IEEE 754 double // precision number representation // for the numeric string in variable s |
String a; float x; s = Float.toString(x) ; // Returns the String representation // for the float value in variable x x = Float.parseFloat(s) ; // Returns the IEEE 754 single // precision number representation // for the numeric string in variable s |
public class String04 { public static void main(String[] args) { String s1, s2; // Strings int x = 12; double y = 3.1415; System.out.print("x + y = "); System.out.println( x + y ); // Add numbers s1 = Integer.toString(x); s2 = Double.toString(y); System.out.print("s1 + s2 = "); System.out.println( s1 + s2 ); // + on String is concatenation } } |
Output:
x + y = 15.1415 s1 + s2 = 123.1415 (Concatenation of Strings !) |
How to run the program:
|
|
Java's automatic conversion rule for number ⇒ string:
|
public class String05 { public static void main(String[] args) { String a, b; int x; a = "abc"; x = -12; b = a + x; System.out.println(b); // Prints "abc-12" b = x + a; System.out.println(b); // Prints "-12abc" } } |
Explanation:
a = "abc"; x = -12; b = a + x; is executed as follows: a + x = "abc" + (-12) convert number to string = "abc" + "-12" concatenate 2 strings = "abc-12" b = x + a; is executed as follows: x + a = (-12) + "abc" convert number to string = "-12" + "abc" concatenate 2 strings = "-12abc" |
How to run the program:
|
public class String06 { public static void main(String[] args) { System.out.println( "a" + 1 + 2 ); System.out.println( 1 + 2 + "a" ); } } |
Solution:
"a" + 1 + 2 = "a" + 1 + 2 (+ is left associative !) = "a" + "1" + 2 (convert 1 to "1") = "a1" + 2 = "a1" + "2" (convert 2 to "2") = "a12" |
How to run the program:
|
However, these operations are available as instance methods (that we will learn later)
Here, I will just give you a little taste of these operations without explaining the details
Example:
01234567890 <---- Character position String s = "Hello World"; char x; x = s.charAt(0); // x = 'H'; x = s.charAt(1); // x = 'e'; x = s.charAt(2); // x = 'l'; |
Example:
01234567890 <---- Character position String s = "Hello World"; char x; y = s.substring(0,1); // y = "H" y = s.substring(2,4); // y = "ll" |
public class String07 { public static void main(String[] args) { String s = "Hello World"; char x; String y; x = s.charAt(0); System.out.println("s.charAt(0) = " + x); x = s.charAt(2); System.out.println("s.charAt(2) = " + x); y = s.substring(0,1); System.out.println("s.substring(0,1) = " + y); y = s.substring(2,4); System.out.println("s.substring(2,4) = " + y); } } |
Output:
s.charAt(0) = H s.charAt(2) = l s.substring(0,1) = H s.substring(2,4) = ll |
How to run the program:
|
|
Note: in this case, you need to use a String typed variable to store a string !!!
import java.util.Scanner; public class Quiz1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int i; String s; s = in.next(); i = Integer.parseInt(); } } |
What effect does these 2 highlighted statements have ?
What have you learned that does the same thing ?
s = in.next(); // Read in a string i = Integer.parseInt(); // Converts to binary number (integer) // representation Total effect: read in a "integer numeric string" as a binary number Replaced by: i = in.nextInt(); |