Help coderanch get a
new server
by contributing to the fundraiser
  • 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 about string concatenation

 
Ranch Hand
Posts: 42
  • 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 + " ");
System.out.print(foo() + x + 5 + " ");
System.out.println(x + y + foo());
}
static String foo() {return "foo";}
}




can anyone please tell me what is the output of this program?
in the book it is given that

72 foo425 86foo

is the correct answer but i think the output should be

72 foo425 4244foo
[ December 28, 2007: Message edited by: sonia jaswal ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • 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".

Your example would NOT compile and run because of syntax errors. Please check those and also try to make use of Code tags when pasting the java code so that it will be easy for others as well.
[ December 28, 2007: Message edited by: Raghavan Muthu ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the concatenation only happens when is found a string.
Then,


When a string is found, all "+" act like concatenation, and not sum.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Feline {
public static void main(String[] args){
long x = 42L;
long y = 44L;
System.out.println(" " + 7 + 2 + " "); //1
System.out.println(foo() + x + 5 + " "); //2
System.out.println(x + y + foo()); //3
System.out.println(foo()+x+y); //4
System.out.println(x + y + 34); //5
}
static String foo() {return "foo";}

}

o/p
72 //1
foo425 //2
86foo //3
foo4244 //4
120 //5

System.out.println(" " + 7 + 2 + " ");
In this line,first evaluation is space " " and integer.space is like as string. the result should be a string.
o/p: 72

System.out.println(foo() + x + 5 + " ");
In this line,first evaluation is method.that method returns string.next evaluation is string and number.the result should be string.
o/p:foo425

System.out.println(x + y + foo());
In this line,first evaluation is integer and integer then result also integer and next evaluation is number and string then the result should be string.
o/p:86foo

System.out.println(foo()+x+y);
In this line,first evaluation is method.that method returns string.next evaluation is string and number.the result should be string.
o/p:foo4244

System.out.println(x + y + 34);
In this line,first evaluation is integer and integer then result also integer and next evaluation is integer and integer then the result should be integer.
o/p:120
 
My cellmate was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic