• 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

system.out.println

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

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

Thanx
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic