Вы находитесь на странице: 1из 3

7/16/13

sout- concatenation (SCJP forum at JavaRanch)

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login A friendly place for programming This week's giveaway is in the Jobs Discussion forum. greenhorns! We're giving away four copies of Presenting for Geeks

and have Dirk Haun on-line! See this thread for details.

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

sout- concatenation
posted 4/5/2007 11:01 AM

Lovleen Gupta Ranch Hand Joined: Feb 26, 2007 Posts: 63

Here goes the code: [code] class Feline{ public static void main(String[] args){ Long x = 42l; Long y = 44l; System.out.println(" " + 7 + 2 + ""); //line1 System.out.println(foo() + x + 5 + ""); //line2 System.out.println(x + y + foo()); //line3 } static String foo(){return "foo";} } [\code] My Doubt: Why in line 1 the output is 72? and why in line 3 the output is 86foo? I don't see any consistency.. Can somebody please explain? Thanks.

www.coderanch.com/t/262281/java-programmer-SCJP/certification/sout-concatenation

1/3

7/16/13

sout- concatenation (SCJP forum at JavaRanch)

Chandra Bhatt Ranch Hand Joined: Feb 28, 2007 Posts: 1707

posted 4/5/2007 11:22 AM

Hi Guptajee,

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .

c l a s sF e l i n e { p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { L o n gx=4 2 l ; L o n gy=4 4 l ; S y s t e m . o u t . p r i n t l n ( ""+7+2+" " ) ;/ / l i n e 1 S y s t e m . o u t . p r i n t l n ( f o o ( )+x+5+" " ) ;/ / l i n e 2 S y s t e m . o u t . p r i n t l n ( x+y+f o o ( ) ) ;/ / l i n e 3 } s t a t i cS t r i n gf o o ( ) { r e t u r n" f o o " ; } }

My Doubt: Why in line 1 the output is 72? and why in line 3 the output is 86foo?

if any one of the left and right operand of the "+" concatenation operator is String, the resultant would be String concatenation. If both are number it would be addition. Remember left to right operation. Thanks and Regards, cmbhatt

cmbhatt

Lovleen Gupta Ranch Hand Joined: Feb 26, 2007 Posts: 63

posted 4/5/2007 11:44 AM

Thanks..Got it..!!

I agree. Here's the link: http://aspose.com/file-tools

www.coderanch.com/t/262281/java-programmer-SCJP/certification/sout-concatenation

2/3

7/16/13

sout- concatenation (SCJP forum at JavaRanch)

subject: sout- concatenation

Similar Threads Doubt in String concatenation (KB Chapter 4) Concatination Operator doubt about string concatenation concatenation Assertion topic
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 16, 2013 07:47:59 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/262281/java-programmer-SCJP/certification/sout-concatenation

3/3

Вам также может понравиться