File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simpe SCJP question(reason?)

 
MInu
Ranch Hand
Posts: 517
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
public static void main(String[] args) {
char a = 'a'; // 'a' = 97
char b = 'b'; // 'b' = 98
System.out.print(a + b + "" + a + b);
}}

The output displayed is '195ab'. Give me an Explanation for the Output.

:
minu
[ January 19, 2005: Message edited by: minu su ]
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java evaluates data type from left to right, and thus, if you perform a+b, the intermediate result is still an interger. However, when you add (a+b) with "", the intermediate result changes to String, and whatever you add with a String, it will be added in String form and thus 'a' and 'b' are added.

Nick
 
Javier Diaz
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This makes you think about overloading common operators (+ in this case)

Perhaps using '+' for String was not a good decision ...
 
MInu
Ranch Hand
Posts: 517
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Nick & Javier.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic