0Pricing
Java Academy · Lesson

Strings

Strings

Strings is a free Java Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Hello,

The string data type is used in Java to hold our text data. Strings are reference data types, not primitive in Java.

When putting the text data into the variable of the String type, we need to put it between the double quote.

Example

Let's define a simple String.

String myText = "Coddy String";

String

After this definition, Java has created an object in the head for us with our String.

Since String is an object of reference data type, it includes methods built into us by Java API.

String concat

In Java, the (+) operator is used to combine two different Strings.



class Main {
    public static void main(String[] args) {
        String catOne = "Coddy";
        String catTwo = "Garfield";

        String catNames = catOne + " " + catTwo;
        System.out.println(catNames);
    }
}

concat method

In addition to the + operator, we can merge two different String with the concat method.

System.out.println(catOne.concat(catTwo));
//CoddyGarfield

lenght method

Perhaps the most used of these is the length() method.

With these methods, we can see how many characters a string expression consists of.



class Main {
    public static void main(String[] args) {
        String myText = "Coddy String";
        System.out.println("total char:" + myText.length());
    }
}

uppercase - lowercase

Now, let's learn how to use the toUpperCase and toLowerCase methods.



class Main {
    public static void main(String[] args) {
        String catOne = "Coddy";
        String catTwo = "Garfield";
        System.out.println(catOne.toUpperCase());   // CODDY
        System.out.println(catTwo.toLowerCase());   // garfield
    }
}

indexOf

If we want to search in a String, we will use the indexOf method.

In the example below, the character s was searched and it was found to be in the 7th row.



class Main {
    public static void main(String[] args) {
        String txt = "My cat is Coddy";
        System.out.println(txt.indexOf("s")); //7
    }
}

quote

Here the following question may have come to mind. 

While strings are placed between double quotes, what do we do if we want to pass double quotes in our String as below?

String txt = "My cat is "Coddy"";

escape character

The above code contains syntax errors and is not compiled by the compiler. If we want to use double trunks inside a String expression, we need to use the backslash which is the escape character.

This character tells the compiler that it is a String expression and should be evaluated within the text.



class Main {
    public static void main(String[] args) {
        String txt = "My cat is \"Coddy\"";
        System.out.println(txt); //My car is "Coddy"
    }
}

example

When defining the string, we stated that it is a reference type, not a primitive. 

Now let's define two strings of the same value.



class Main {
    public static void main(String[] args) {
        String catOne = "Coddy";
        String catTwo = "Coddy";
        boolean status = catOne == catTwo;
        //status => false
    }
}

equals method

== operator performs value control for primitive variables. Object types such as String hold the address in memory, not the object itself.

When we define two objects, even if they have the same value, because they are in different areas in the memory, catOne and catTwo variables actually hold different addresses. That's why when we compare it with the == control it returns false.

Equals method is used to compare the values of two strings.

if(catOne.equals(catTwo)){
	//codeblock works
}

equals method

The Equals method is used to compare the values of two strings.

if(catOne.equals(catTwo)){
	//codeblock works
}

example cont.

In the example above, the equals method will return true because it compares the data in the address, not the addresses of the objects.

Congratulations

In this section, we learned about Strings in Java. 

See you in the next lesson. 🥳

Strings — illustration 15

Frequently asked questions

Is the “Strings” lesson free?

Yes — the full text of “Strings” is free to read here on the web, and the Java Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Strings”?

Strings You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Strings” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to Java Academy