• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in String concatenation (KB Chapter 4)

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't understand why the answer to KB Question #7 of Chapter 4 was G. My answer was H.

Here's the question:

Given:
class Feline {
public static void main(String[] args) {
Long x = 42L;
Long y = 44L;
System.out.print (" " + 7 + 2 + " ") ;
System.out.print(foo () + x + 5 + " ");
System.out.println(x + y + foo());
}
static String foo() { return "foo"; }
}
What is the result?
A.9 foo47 86foo
B.9 foo47 4244foo
C.9 foo425 86foo
D.9 foo425 4244foo
E.72 foo47 86foo
F.72 foo47 4244foo
G.72 foo425 86foo
H.72 foo425 4244foo
I.Compilation fails.

Thanks!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ans is G:

System.out.println(x + y + foo());
first id adds x and y.after that it concats the string.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

System.out.println(x + y + foo());



The expression "x+y+foo()" in println is evaluated from left to right.

If either operand is a String, the + operator becomes a String concatenation operator. If both operands are numbers, the + operator is the addition operator.

So, as x and y are numbers(long) they get added and gives 86 and after the result of addition, we are left with 86 and other String operand("foo") which gets concatenated resulting in to: 86foo.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the String Concatenation works in such a way. Once it encounters the first operand as String, then the rest all would be treated as String only.

In your example,







Line 1 and 2 has a string content as one of the operands in the first concatenation. Remember, "" is a string and the method foo() returns a String content. That's why you get the output as 72 for first [7,2 being treated as String only] and "foo425" in next output. Same steps as explained for the 1st output.

In Line 3, the values of x and y are treated as they are (here of type long) and their sum 86 is concatenated with the output of method foo() which is a string "foo". That's why the output is "86foo" and NOT "4244foo".

[ January 29, 2008: Message edited by: sonia jaswal ]

[ January 29, 2008: Message edited by: sonia jaswal ]
[ January 29, 2008: Message edited by: sonia jaswal ]
 
Jart Bo
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all! Now I've realized I need to look at the very first operand from the left..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic