File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes system.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "system.out.println" Watch "system.out.println" New topic


shandilya popuru
Ranch Hand

Joined: Dec 21, 2004
Posts: 95
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;

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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?

[Jess in Action][AskingGoodQuestions]
Nitin Jawarkar
Ranch Hand

Joined: Dec 18, 2004
Posts: 79

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


Cheers<br />Nitin
Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
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



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.

Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229

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:

Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
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.?

I agree. Here's the link:
subject: system.out.println
It's not a secret anymore!