Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wonder why the double quotation marks is not shown in the actual output - just after the equal sign:
Java
String word = "" + c1 + c2 + "ll";

The full code as follows:
Java
public class InstantMethodsIndexOf
{
    public void start()
    {
        String greeting = new String ("Hello World");

        int position = greeting.indexOf('r');
        char c1 = greeting.charAt(position + 2);
        char c2 = greeting.charAt(position - 1);

        String word = "" + c1 + c2 + "ll";

        System.out.println(word);
    }
}
Posted
Updated 4-Jul-14 17:21pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Jul-14 0:41am    
By the same reason +, =, and blank space are not shown — they are not part of string, they are part of syntax.
By the way, always use string.Empty instead of "".
—SA
DamithSL 5-Jul-14 1:14am    
You are correct, initially I thought OP need to know why there is "" used. I have change my answer.

"" is empty string and it will not adding anything to your final word object.
if it is not adding anything why there is ""?
The ‘+’ operator in java is overloaded as a String concatenator. Anything added to a String object using the ‘+’ operator becomes a String. Since c1 and c2 are char types, once you summed up them with '+' the result will be the addition of char codes of values in the two variable (ex. 'a' + 'b' produces 195). You can convert these chars to strings by start concatenation with empty string.

""+'a' is equal to "a".
and then "a"+'b' is equal to "ab".

This is applicable to any numeric data type.

for example: below will give error: incompatible types
Java
String x = 5+7; //error: incompatible types

but
Java
String x = ""+ 5+7;// successfully compile and give you 57


read:Is concatenating with an empty string to do a string conversion really that bad?[^]
 
Share this answer
 
v5
why u use ""? only u can use '+' operator for concatenation
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900