0% found this document useful (0 votes)
4 views4 pages

Computer Programming - Week 8

The document provides an overview of Java Strings, including how to initialize and assign values to string variables, as well as the use of special characters. It explains string concatenation and demonstrates how to display strings using print statements. Examples illustrate the effects of special characters and the output of concatenated strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Computer Programming - Week 8

The document provides an overview of Java Strings, including how to initialize and assign values to string variables, as well as the use of special characters. It explains string concatenation and demonstrates how to display strings using print statements. Examples illustrate the effects of special characters and the output of concatenated strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like