Java String Concatenation
Java String Concatenation
Home Assignment
Java String
Before doing the activities About Java Strings Activities
today, watch the video on the
next side (Video also available Work through the following activities during
Saturday, May 17, at home.
in this module page).
You can work alone or in groups of two or three
Please watch the video again students as you study and do examples.
after doing all activities here.
However, every student submit his or her own work.
WARNING!
Adding Numbers and
Strings Java uses the + operator for both addition
and concatenation.
Therefore z = 30
Java Numbers
Numbers and Strings
and Strings
Example 1b:
Adding Numbers and
Strings.
String x = "10";
String y = "20";
If you add two strings, the String z = x + y; // z will be 1020 (a String)
result will be a string
concatenation: Java uses the + operator to concatenate the
number 10 and 20.
x + y will return the string stored in x and y
and assign it to z.
Therefore z = 1020
Java String
Numbers and Strings
Concatenation
The + operator can be used Activity 2:
between strings to
combine them. Try it Yourself.
Write two programs in that uses the snap code in
This is called Example 2a (Class Name NumSum) and
concatenation. Example 2b (Class Name NumCon).
Because strings must be String txt = "We are the so-called "Vikings" from the north.";
written within quotes, Java
will misunderstand this Try it Yourself.
string, and generate an Write a program that uses the snap code above.
error: Run your code. Study the error massage.
The solution to avoid this String txt = "We are the so-called \"Vikings\" from the north.";
The solution to avoid this For each Backslash escape character used
problem, is to use the below, write and submit a code that
backslash escape illustrate how is it used
character.
Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash
Java Strings
Special Characters
Special
Characters Activity 4a
String txt = "We are the so-called \"Vikings\" from the north.";
Java Strings
Special Characters
Special
Characters Activity 4b
Java Strings
Special Characters
Special
Characters Activity 4c