0% found this document useful (0 votes)
6 views19 pages

Java String Concatenation

The document outlines a Java home assignment focused on string manipulation, including concatenation, adding numbers and strings, and handling special characters. Students are required to complete various coding activities and submit their work by specified deadlines. Additionally, it emphasizes the use of the backslash escape character to manage special characters in 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)
6 views19 pages

Java String Concatenation

The document outlines a Java home assignment focused on string manipulation, including concatenation, adding numbers and strings, and handling special characters. Students are required to complete various coding activities and submit their work by specified deadlines. Additionally, it emphasizes the use of the backslash escape character to manage special characters in 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/ 19

Java

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.

The Quiz activities should be completed by Friday,


May 23, 2025, at 2359 Hours
Java String
Java String
Concatenation
Concatenation
The + operator can be used Example 1:
between strings to
combine them. String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
This is called
concatenation.

Note that we have added an empty text (" ")


to create a space between firstName and
lastName on print.
Java String
Concatenation
Concatenation
The + operator can be used Activity 1:
between strings to
combine them. Try it Yourself. Class Name MyName
Write the program in that uses the snap code in
This is called Example 1.
concatenation.
The program should print the value of variable
firstName and lastName with a space.

Submit it as part of Assignment 3.


Java Numbers
Numbers and Strings
and Strings

WARNING!
Adding Numbers and
Strings Java uses the + operator for both addition
and concatenation.

Numbers are added.


Strings are concatenated.
Java Numbers
Numbers and Strings
and Strings
Adding Numbers and Example 2a:
Strings.
int x = 10;
If you add two numbers, int y = 20;
the result will be a int z = x + y; // z will be 30 (an integer/number)
number:
Java uses the + operator to add the two
number 10 and 20.
x + y will return the sum of x and y and
assign it to z.

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).

For Each snap code, the program should print the


value of variable z.

Submit it as part of Assignment 3.


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).

For Each snap code, the program should print the


value of variable z.

Submit it as part of Assignment 3.


Java Strings
Special Characters
Special
Characters Example 3 and Activity 3

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.

Write down what you understand happened using


the commenting tool in Java. Submit your code as
activity 3, part of Assignment 3.
Java Strings
Special Characters
Special
Characters Example 4

The solution to avoid this String txt = "We are the so-called \"Vikings\" from the north.";

problem, is to use the


backslash escape
character. The backslash (\) escape character turns
special characters into string characters:
Java Strings
Special Characters
Special
Characters Example of The backslash (\)
escape character
The solution to avoid this
problem, is to use the Escape character Result Description
backslash escape \' ' Single quote
character. \" " Double quote
\\ \ Backslash

The backslash (\) escape character turns


special characters into string characters:
Java Strings
Special Characters
Special
Characters Activity 4

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

The solution to avoid this Use a backslash escape character to inserts a


problem, is to use the double quote in a string.
backslash escape
character.
The sequence \" inserts a double quote
in a string. Example -

String txt = "We are the so-called \"Vikings\" from the north.";
Java Strings
Special Characters
Special
Characters Activity 4b

The solution to avoid this Use a backslash escape character to inserts a


problem, is to use the single quote in a string.
backslash escape
character.
The sequence \’ inserts a double quote in
a string. Example -

String txt = "It\'s alright.";


\\ inserts a single backslash in a string:

Java Strings
Special Characters
Special
Characters Activity 4c

The solution to avoid this Use a backslash escape character to inserts a


problem, is to use the single quote in a string.
backslash escape
character.
The sequence \\ inserts a backslash in a
string. Example -

String txt = "The character \\ is called backslash.";


Java Strings
Special Characters
Special
Characters
Code Result
Other common escape \n New Line
sequences that are valid in \r Carriage Return
Java are: \t Tab
\b Backspace
\f Form Feed
Java String
Java
Assignments
Java Strings
Work with your partner,
Assignment 3 – Coding Activities
discuss and complete
assignment 3 due Saturday
Concatenation
May 17 and Assignment 4 the Numbers and Strings
Quiz is due Sunday, May 18, Java Special Characters
2025, at 2359 Hours. Due Sunday, May 17, 2025

Assignment 4 – The Quiz


Quiz – Due Friday, May 23, 2025

You might also like