This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

system.out.println

 
shandilya popuru
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does this code compile without errors

int d1=10,d2=20;
system.out.println("value is"+(d1+d2));

and this not

String d1="aa";
int d2=20;
system.out.println(d1+d2);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandy,

Welcome to JavaRanch!

Neither one of them will compile as written, as the "S" in "System" must be capitalized. But given that, they're actually both perfectly fine, as long as they're within a method or initializer block. What compiler errors are you seeing?
 
Nitin Jawarkar
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

The S of System class must be in caps letter, rest code is fine.

Thanx
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandy,

The code is Perfectly alright if you use S instead of s in System.out.println(...)

output for the first code is
---------------------------------

Value is 30

----------------------------------

and the output for the 2nd code is
---------------------------------

aa20

----------------------------------

I think here you are getting confused, 2nd output is like aa20 because you are printing d1 then with +d2 you are getting the value of d2. I think for you more explanation is required so i request Mr Ernest Friedman-Hill to explain this topic, because i know this wil b result but dont know the exact answer. So plz explain this.


Thanxs
Sheetal
 
Saket Barve
Ranch Hand
Posts: 229
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Concept is the same in either case, Sheetal.

Primitive types can be printed as is inside System.out.println() without the compiler grumbling. If you are doing any operations with the primitives inside the System.out.println() these will get evaluated first and then the result printed. You don't need to concatenate a string literal before (or after) the primitive in order to get the above results.

Thus, take a look at the following piece of code:



Regards,
Saket
 
Sheetal Kaul
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saket Barve,

Here in this case we are not concatinating the string right? ya i got Your Point but in the previous case, value of d1+d2 is aa20. means it is adding the vaue of d1 with d2. right? means it is adding 'aa' with 20 so the result is aa20. am i right? but when we add supose d1 value is 'a' & d2 value value is 20, the result is not like a20. but it wiil take the ASCII value. So why not here(aa20) it takes the ASCII value, may be the reason is that 'aa' doesnt have any ASCII value. right? is it so.? or any other reason? please explain, coz i'm getting confused here? i know it is very stupid kind of Qn but please explain me why it is behaving like this.?

Thanxs
Sheetal
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic