Back

Activity 1.3 Strings

divider

Introduction

Activity 1.3

Strings

Topics

  • The string data type
  • String formatting
  • String concatenation

The String Data Type

The string data type represents a sequence of characters used for text.

"Hello friend."
"Are you a 1 or a 0?"

I am happy! "I am sad!

"I said "don't" do it." 'Goodbye.'

String Formatting

The process of inserting values into a string to create a complete, readable message.

System.out.println("You can retire in");
System.out.println(67 - 43);
System.out.println("years.");
Terminal window
You can retire in
24
years.

String Formatting

The process of inserting values into a string to create a complete, readable message.

Terminal window
You can retire in 24 years.

String Concatenation

Use addition sign (+) to combine strings.

System.out.println("Mortimer " + "rules");
System.out.println("You owe me $" + 1000);

String Concatenation

Use addition sign (+) to combine strings.

System.out.println("Mortimer " + "rules");
System.out.println("You owe me $" + 1000);
Terminal window
Mortimer rules
You owe me $1000

Issues with String Concatenation

Math expressions can accidentally concatenate before the expression is evaluated.

System.out.println("After interest, you will earn $" + 1000 + 100 + " over one year.");

Issues with String Concatenation

Math expressions can accidentally concatenate before the expression is evaluated.

System.out.println("After interest, you will earn $" + 1000 + 100 + " over one year.");
Terminal window
After interest, you will earn $1000100 over one year.

Issues with String Concatenation

Surround math expressions with parentheses.

System.out.println("After interest, you will earn $" + (1000 + 100) + " over one year.");
Terminal window
After interest, you will earn $1100 over one year.

Key Terms

String
A data type used to represent a sequence of characters, like letters, numbers, and symbols.
String Formatting
The process of creating a new string by combining a template with specific values. This allows you to easily insert data into a sentence or phrase.
String Concatenation
the process of joining two or more strings together to create a single, longer string, often using the plus symbol (+).

'F' → Fullscreen

Objectives

  • icon Formatting strings using string concatenation
divider

Activity Tasks

  • icon Create a new Java project named 1-3-Strings.
  • icon Complete each task individually.

Task 1: Basic String Concatenation

  • icon Practice simple combinations with strings and numeric values.
Program.java
public class Program {
public static void main(String[] args) {
// --- Basic String Concatenation ---
System.out.println("Part 1: Basic String and Number Concatenation");
System.out.println("Hello" + " world!");
System.out.println("Today's number is " + 7);
System.out.println("Pi is approximately " + 3.14159);
System.out.println("------------------------------------");
//
}
}

Task 2: Strings and Numbers

  • icon Use string concatenation to append math expressions to string.
Program.java
public class Program {
public static void main(String[] args) {
// --- Basic String Concatenation ---
System.out.println("Part 1: Basic String and Number Concatenation");
System.out.println("Hello" + " world!");
System.out.println("Today's number is " + 7);
System.out.println("Pi is approximately " + 3.14159);
System.out.println("------------------------------------");
// --- Combining Strings with Arithmetic Expressions ---
System.out.println("Part 2: Combining with Math Expressions");
System.out.println("5 squared is " + (5 * 5));
System.out.println("The area of a circle with a radius of 4 is " + (4 * 4 * 3.14));
System.out.println("If I have 25 marbles and my friend gives me 15 more, I have a total of " + (25 + 15) + " marbles.");
System.out.println("------------------------------------");
//
}
}

Task 3: Building Sentences

  • icon Use multiple instances of concatenation to build sentences.
Program.java
public class Program {
public static void main(String[] args) {
// --- Basic String Concatenation ---
System.out.println("Part 1: Basic String and Number Concatenation");
System.out.println("Hello" + " world!");
System.out.println("Today's number is " + 7);
System.out.println("Pi is approximately " + 3.14159);
System.out.println("------------------------------------");
// --- Combining Strings with Arithmetic Expressions ---
System.out.println("Part 2: Combining with Math Expressions");
System.out.println("5 squared is " + (5 * 5));
System.out.println("The area of a circle with a radius of 4 is " + (4 * 4 * 3.14));
System.out.println("If I have 25 marbles and my friend gives me 15 more, I have a total of " + (25 + 15) + " marbles.");
System.out.println("------------------------------------");
// --- Building a Sentence with Multiple Parts ---
System.out.println("Part 3: Building a Full Sentence");
System.out.println("The current year is " + 2024 + ". The next year will be " + (2024 + 1) + ".");
System.out.println("The temperature in my city is " + 72 + " degrees Fahrenheit.");
System.out.println("A dozen is 12. Two dozen is " + (12 + 12) + ".");
System.out.println("------------------------------------");
//
}
}

Task 4: Side Effects with Strings and Math

  • icon Experiment with using parentheses and string concatenation.
Program.java
public class Program {
public static void main(String[] args) {
// --- Basic String Concatenation ---
System.out.println("Part 1: Basic String and Number Concatenation");
System.out.println("Hello" + " world!");
System.out.println("Today's number is " + 7);
System.out.println("Pi is approximately " + 3.14159);
System.out.println("------------------------------------");
// --- Combining Strings with Arithmetic Expressions ---
System.out.println("Part 2: Combining with Math Expressions");
System.out.println("5 squared is " + (5 * 5));
System.out.println("The area of a circle with a radius of 4 is " + (4 * 4 * 3.14));
System.out.println("If I have 25 marbles and my friend gives me 15 more, I have a total of " + (25 + 15) + " marbles.");
System.out.println("------------------------------------");
// --- Building a Sentence with Multiple Parts ---
System.out.println("Part 3: Building a Full Sentence");
System.out.println("The current year is " + 2024 + ". The next year will be " + (2024 + 1) + ".");
System.out.println("The temperature in my city is " + 72 + " degrees Fahrenheit.");
System.out.println("A dozen is 12. Two dozen is " + (12 + 12) + ".");
System.out.println("------------------------------------");
// --- Converting an Expression to a String First ---
System.out.println("Part 4: Converting Expressions to Strings");
System.out.println("Here is a number " + (10 + 20)); // The parentheses force the addition first.
System.out.println("But what if we did not have parentheses?");
System.out.println("If you put a string in front, it all becomes a string: " + 10 + 20);
System.out.println("To fix this, use parentheses: " + (10 + 20));
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
Part 1: Basic String and Number Concatenation
Hello world!
Today's number is 7
Pi is approximately 3.14159
------------------------------------
Part 2: Combining with Math Expressions
5 squared is 25
The area of a circle with a radius of 4 is 50.24
If I have 25 marbles and my friend gives me 15 more, I have a total of 40 marbles.
------------------------------------
Part 3: Building a Full Sentence
The current year is 2024. The next year will be 2025.
The temperature in my city is 72 degrees Fahrenheit.
A dozen is 12. Two dozen is 24.
------------------------------------
Part 4: Converting Expressions to Strings
Here is a number 30
But what if we did not have parentheses?
If you type 10 + 20, the result will be a number: 30
But if you put a string in front, it all becomes a string: 1020
To fix this, use parentheses: 30
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What is the key difference between string concatenation and a mathematical operation? Describe a scenario from this activity where a missing pair of parentheses could change the outcome of an expression.
  2. Thinking about the concept of a string as a "sequence of characters," how is it fundamentally different from a number like an int or a double? How does Java's behavior with the + operator change depending on whether it's used with a string or a number?
  3. Based on your experience with this activity, why are parentheses so important when combining strings with mathematical expressions? How do they act as a "traffic light" for your code, telling the computer which operation to perform first?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete