Day 7 : Java Strings (Text Magic in Java)

Strings in Java

Hey friends, welcome back to 100 Days of Java.
Yesterday we taught Java how to make decisions with if else.
But here’s a thought: what if you don’t just want numbers or true or false? What if you want words, names, sentences?

That’s where Strings come in.

What is a String

In Java,

  • A String is simply text inside double quotes.
  • Example:
String name = "Tony Stark";

Here, "Tony Stark" is a String.
Think of it this way:

  • Numbers are the calculator’s language.
  • Strings are the human language.

Joining Strings (Concatenation)

Concatenation means joining two strings together.

String firstName = "Tony";
String lastName = "Stark";
String fullName = firstName + " " + lastName;

System.out.println("Full Name: " + fullName);

Output:

Full Name: Tony Stark

It’s like connecting Lego blocks; you can build bigger pieces of text.

Numbers and Strings

Strings can also hold numbers as text.

String age = "30";
System.out.println("I am " + age + " years old.");

Output:

I am 30 years old.

But remember, "30" here is text, not math.
Try this:

System.out.println("10 + 20 = " + 10 + 20);

Output:

10 + 20 = 1020

Why? Because Java is treating 10 and 20 as text.
To fix it, use parentheses so Java calculates first:

System.out.println("10 + 20 = " + (10 + 20));

Output:

10 + 20 = 30

Special Characters in Strings

Sometimes you want to include quotes or a new line inside your text.
That’s where escape characters help.

  • \n = new line
  • \t = tab (extra spaces)
  • \" = add quotes inside text

Example:

System.out.println("Hello\nWorld");
System.out.println("He said, \"I am Iron Man\"");

Output:

Hello
World
He said, "I am Iron Man"

Real-Life Example: Chat Message App

public class ChatApp {
    public static void main(String[] args) {
        String sender = "Tony";
        String message = "Meet me at Avengers Tower!";
        String time = "5 PM";

        String chat = sender + ": " + message + " (at " + time + ")";
        System.out.println(chat);
    }
}

Output:

Tony: Meet me at Avengers Tower! (at 5 PM)

It looks just like a chat message. Apps like WhatsApp or Discord use Strings everywhere for this.

Wrap Up

Today you learned:

  • What Strings are
  • How to join Strings
  • How Strings handle numbers differently
  • How to use special characters
  • How to build a simple chat message app

Tomorrow we’ll go deeper into String methods counting letters, changing text to uppercase, and more.

References

You can also check out:

About Us

If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.

Leave a Comment

Your email address will not be published. Required fields are marked *