Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

println method giving error

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a doubt which may not be related to SJCP.
All of us know that println accepts all objects as inputs.
But when + operator is included like say
System.out.println(int + object);
Compiler gives cannot convert int to object, my doubt is
when object is given as input, its toString() method gets executed. Since tostring returns a String object why
is that we cannot use + operator with objects, I expected a
concatenation. Is is not a logical thing to expect a concatenation here . please help me in clearing this behaviour.
 
Enthuware Software Support
Posts: 4599
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not a problem with println. Remember, the + operator is overloaded only for strings. i.e.
"string" + anything or anything + "string" will be concatenated to produce a bigger string.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
http://pages.about.com/jqplus
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anil:
Yes, that is understood, but my question is,
Since toString() method on an object returns a String object
right, ie, even though we give object as input, this is converted
to a String and why should there be a problem with using the
'+' operator on objects(Which are converted to Strings).
System.out.println(int + object) is the same as
System.out.println(int + object.toString());
The second version is fine and working but what is the problem
with the first.
2. when is the pattern change of exam comes into effect. if i
buy a voucher now and wish to take exam on oct 10, will there
be any change in the exam pattern, objectives etc..
thanks for the interest shown.
Kishan Kumar
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kishan Kumar,
System.out.println(int + object) is the NOT same as
System.out.println(int + object.toString());
System.out.println( object) is the same as
System.out.println(object.toString());
The point to remember here is, the output of an expression is string if and only if one of the operands is string. The compiler will give you an error if you use non-string Objects and primitives as operands for the + operator.
In other words, a non-string Object can be a valid operand for the + operator if and only if one of the operands is a String object ie.,
10 + "hello" - returns a String
"h" + 2 + "3" - returns a String
10 + EmployeeObject - gives you compiler error!!.
Here is an excerpt from JLS Section 15.18 - Additive Operators


If the type of either operand of a + operator is String, then the operation is string concatenation. Otherwise, the type of each of the operands of the + operator [i]must be a primitive numeric type,[i] or a compile-time error occurs.
In every case, the type of each of the operands of the binary - operator must be a primitive numeric type, or a compile-time error occurs.


You an expect a few quesions on this topic in the SCJP exam. So it is absolutely necessary that you understand this concept well. The best way to do it is by writing a LOT of code.
Hope this helps.
Ajith
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
Thanks for the explanation. I was just thinking logically rather
than in programmatic terms. I feel it would have been a good feature if concatenation was allowed with objects even though i
cannot think of a example.
Thanks all, you are doing a wonderful job.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic