• Recent Posts

  • Archives

  • Categories

  • Episode 4: Strings and concatenation

    Welcome back to this new episode in this series of tutorials dedicated to programming, and more specifically programming using the amazing Java language. In the last episode we saw how to declare, initialize, modify, sum and display integer variables. In this episode we will learn how to declare, display and use one of the most important types in Java: the String type. Let’s go.

    The String type

    String is a data type in the same way as int. The difference is that int is used to represent integer numbers while String is used to represent words, sentences and text in general. Actually we have already used it in our Hello World program :

    1
    2
    3
    4
    5
    6
    7
    
    public class Main 
    {
        public static void main(String[] argv) 
        {
            System.out.println("Hello world");
        }
    }

    “Hello world” is a the String in question. In fact it is a literal String, in the same way that 16 or 7 are literal integers.

    A literal String is a combination of characters inclosed by double quotes. Here are some examples : “What ?”, “5*5*5″, “—…—” and “”. This last example is called an empty String simply because it dose not contain any character. Notice that you should not forget the double quotes or you will get a compilation error ;)

    Now instead of directly displaying a literal String, let’s declare one as we did with int and then use System.out.println to display it. Therefor instead of writing :

    int age= 15;

    we will write :

    String phrase = "Hello world";

    Easy, isn’t it ? We have declared a variable named phrase of type String and initialize it with the value “Hello world”. Here is the complete program :

    1
    2
    3
    4
    5
    6
    7
    8
    
    public class Main 
    {
        public static void main(String[] argv) 
        {
            String message = "Hello world";
            System.out.println(message);
        }
    }

    The result remains the same :

    Hello world

    The only difference again between displaying the content of a variable instead of directly using a String literal is that the variable (message in our example) still keeps track of the String “Hello world” so that it can be used later in the program.

    The String concatenation operator

    Take a look at this program :

    1
    2
    3
    4
    5
    6
    7
    
    public class Main 
    {
        public static void main(String[] argv) 
        {
            System.out.println("Bob is " + "15");
        }
    }

    Wow!! Are you trying to sum two Strings ? 8O

    Before answering, go ahead and try it on Eclipse. It will not generate any compilation errors, it will be executed flawlessly and the result will be :

    Bob is 15

    Actually the + sign here is not the addition operator, it is the String concatenation operator which is a different operator that accepts two arguments as well and produces a String representing the concatenation of the two arguments. You may ask the question how to know if the + sign designates the addition operator or the String concatenation operator ? The answer is in the image above :

     

    To sum up the rule above: if one of the two arguments of the + operator is not a number then it is a String concatenation. If both arguments are numbers then it is a sum.

    Here are some examples of using the + operator:

    Now let’s see if you correctly understood the above rule, take a look at the following tricky program ;) :

    1
    2
    3
    4
    5
    6
    7
    
    public class Main 
    {
        public static void main(String[] argv) 
        {
            System.out.println(15 + 5 +" is " + 15 + 5);
        }
    }

    Provided what you learned so far what will be the result of the execution of the above program ?

    Answer:

    The compiler will proceed from left to right, here is what he will do :

    First we have 15 + 5 : both arguments are numbers, therefor it’s a sum and the result is 20 which is an int

    Then we have 20 + ” is “: the second argument is not a number, therefor it’s a concatenation and the result is “20 is ” which is a String

    Next we have “20 is ” + 15: this time the first is not a number, therefor it’s a concatenation and the result is “20 is 15″ which is a String

    Finally we have “20 is 15″ + 5: the first argument is not a number, therefor it’s another concatenation and the final result is “20 is 155″

    20 is 155

    You see how amazingly the first two numbers have been summed while the last ones have been concatenated, resulting in this awkward “20 is 155″ which is not true mathematically speaking of course :) .

    Now what you can do in order to tell the compiler to compute the sum of the last two numbers is adding parenthesis as in the following program:

    1
    2
    3
    4
    5
    6
    7
    
    public class Main 
    {
        public static void main(String[] argv) 
        {
            System.out.println(15 + 5 + " is " + (15 + 5));
        }
    }

    When the compiler finds an opening parenthesis he will always look for the corresponding closing parenthesis and compute what is inside the two parenthesis before anything else. So in our case he is forced to compute (15 + 5) which is 20 and the final result will be as expected :

    20 is 20

    Which makes more sense :D

    What is next ?

    In the next episode I will show you the rest of the Java arithmetic operators and the precedence rules that govern them. Till then do not forget to practice what you have learned so far about numbers and strings on Eclipse. If you have any questions feel free to leave them as comments. I would answer them gladly. See you in the next episode.

    January 1, 2013 at 00:01 | Java tutorials| Be the first to comment this post

    Leave a Reply

    You must be logged in to post a comment.