• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Concatination Operator

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Feline
{
public static void main(String[] args)
{
Long x = 42L;
Long y = 44L;
System.out.print(" " + 7 + 2 + " "); // line---1
System.out.print(foo() + x + 5 + " "); //line--2
System.out.println(x + y + foo()); //line--3
}
static String foo() { return "foo"; }
}
Out Put:
72 foo425 86foo

Can any one explain the Concatination operator " + " at line 1, line 2 performing as a string concatination but at line 3 that is performing as addition operator...I'm confusing with this Please help me any one as early as possible.
Thanks in advance...
Ishmayel.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the lines above are evaluated left to right, so in the first two you have a string and add an integer (which is converted to a string and concatenated)

In the 3rd line your first two operands are integer values, so that + operator is an addition and not a string concatination. The code is equivalent to System.out.println((x + y) + foo()) which is the preferred way of writing it to avoid this confusion.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the expressions are evaluated from left to right. At least one of the operands of the first two in both the expressions line 1 and 2 are strings hence "+" behaves as concatenation operator. However, In the line 3, first two are long operands and hence the "+" is working as arthimetic operator.

Hope this helps!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 1,String constant came first so any thing after + is converted to String constant.

In line 3, x and y are not Strings so they added in the normal way then it found String after second + so it converts (x+y) value to String and gives the result 86foo.

The rule is like this.

"Until String operand is found expression is evaluated normally, once it found the String operand,then it Converts the remaining operands (after String) to String."
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ishmayel vemuru:
class Feline
{
public static void main(String[] args)
{
Long x = 42L;
Long y = 44L;
System.out.print(" " + 7 + 2 + " "); // line---1
System.out.print(foo() + x + 5 + " "); //line--2
System.out.println(x + y + foo()); //line--3
}
static String foo() { return "foo"; }
}
Out Put:
72 foo425 86foo

Can any one explain the Concatination operator " + " at line 1, line 2 performing as a string concatination but at line 3 that is performing as addition operator...I'm confusing with this Please help me any one as early as possible.
Thanks in advance...
Ishmayel.



Hi Ishmayel
The sum at line 3 gives an output of "86foo" because all atomic operations of the same kind are resolved from left to right (unless there are brackets, which change resolving order). This means that in

x + y is resolved before all, and since there are NO String OPERANDS (being x and y Long), the jvm read the '+' operator as a sum and not as a concatenation. After "x + y" is evaluated (giving a result of 86), the second '+' sign is read as a concatenation because method foo() returns the String operand needed for the jvm to behave this way and so the output is "86foo". The above code is equal to:

For the first 2 System.out.print(), all the '+' signs are read as concatenation operators because there is a String operand since the first atomic operation (" " and foo()), so they can be interpreted as:

Hope this helps
Regards
LR
 
ishmayel vemuru
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thank for your reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic