8/25/2016
Java Strings
1. Initializing a variable
Example:
Storing Text in Strings String sampleString = “This is a Java
String”;
Special Characters in Strings
Displaying Strings 2. Assigning of values
Concatenating Strings Example:
sampleString = “This is a Java String”;
Other Variables with Strings
Special Character Effect
Why can’t I use some characters in Java strings? \’ displays single quotation mark
\” displays double quotation mark
\\ displays backslash
The Backslash Symbol (\)
\t adds a tab to string
Example: \b inserts backspace
String sample1 = “He quoted \” I think, therefore I
\r initiates a carriage return
am \” to his friend.”;
\f initiates a form feed
will return as \n moves to a new line
He quoted “I think, therefore I am” to his friend.
1
8/25/2016
What is concatenation?
Example:
Statements used to display strings in Java:
String a = “Hello”;
1. System.out.print( ) String b = “World”;
2. System.out.println( ) String message = a + b;
What is printing? String variable message will return as Hello World
when called or printed
Example:
Int length = 120;
char rating = ‘R’;
System.out.println(“Running Time:” + length +
“minutes”);
System.out.println(“Rated” + rating)
Output:
Running Time: 120 minutes
Rated R
2
Computer Programming (JAVA) – Week 8 Page 1 of 2
Computer Programming (JAVA) – Week 8 Page 2 of 2