|
You are familiar with + and - operators since childhood. In java, they are used to do the basic arithmetic of addition and subtraction. Generally they are applied only to the numeric types. However there are few exceptions. For instance, you can apply the + and += operators to the operands of type String.
String Concatenation and the + operator
Commonly the + operator is used to perform mathematical addition. Besides that, it has one unusual use. You can use it for string concatenation. When both operands are strings, then + operator will simply concatenate second string to the first to produce the resultant string. For example, after executing the following code, the result string would be "Hello World!".
String str1 = "Hello ";
String str2 = "World!";
String result = str1 + str2; // String concatenation using + operator
Sometimes one or both of the operands may not be a string object. If the both operands are numeric values, then + will simply do mathematical addition. If only one operand is string, Java needs to do some additional processing on the non-string operand before applying +. Java implicitly converts it into a string object. How Java does that depends on the operand's type. If the operand is an object reference, Java implicitly calls the tostring() method on that object to convert it into a String object. This method is defined in java.lang.Object class. So every Java class inherits it. In the following code fragment, the + operator operates on a string object and a date object.
String str1 = "Man landed on the moon on ";
Date date = new Date(69,06,20,19,56);
String result = str1 + date;
Java evaluates the expression str1 + date in the following steps:
1. First operand is a string object. Therefore, do nothing about it.
2. The second operand is date object, call its toString() method. This method is defined in the Date class and returns a string representation of the date object as Sun Jul 20 19:56:00 PDT 1969.
3. Now the + operator concatenates the two string objects to create a third string object Man landed on the moon on Sun Jul 20 19:56:00 PDT 1969
Most Java classes override the toString() method of Object class, so that their state is properly returned in form of string. For example, the toString() method of Hashtable collection class overrides toString() method to display the elements it is holding. However if a class does not override this method then the method from Object class is called. This method produces string that contains the name of that object's class followed by @, some unique identifying value. For instance, if you declare a class MyClass without overriding toString() method, create its instance and call a toString() method, it will return string that will look something like MyClass@5e9756. This string representation is not very useful as it does not tell much about the object.
It is usually a good practice to override the toString() method of classes you define. You should write it in such a way that it would represent the state of the object properly. This can be useful for debugging purposes.
|
|
You will find lot of exam question in which the question code has System.out.println() to print information as output. This method call often uses the string concatenation operator while printing the output. |
The println() method in call System.out.println() is overloaded method accepting different argument types. You can pass any primitive or object to this method. It converts it into a string before writing it on output stream. If you have a Java expression as argument, then first the expression is evaluated and then it is passed to this method. In the following code, the expression a+b evaluates as int value.
int a=3,b=5;
System.out.println(a + b); // prints 8
However if any one operand of + operator is a string, it treats the others as string as well. Following examples have two int operand and one string operand.
int a=3,b=5;
System.out.println( + a + b); // prints 35
System.out.println(a + + b); // prints 35
Note that the output is 35 and not 3 5, because there is no space between the two numbers when they are concatenated as strings.