• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting int as a string with string builder

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the following code work:


But this code won't compile but gives me an "int cannot be converted to String" error.


It isn't clear to me why I can implicitly cast the int in the first code but can't explicitly cast it in the second code. Thanks!
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you aren't actually casting the int to a String in the first example. Instead, you are using a feature of the + operator which is defined in the Java Language Specification; somewhere in there (if you really want to go looking) you'll find some rules which describe how the + operator should be evaluated when one of its arguments is a String.

In the second example, on the other hand, you are actually attempting to do the case. Seems like you already know that shouldn't work, am I right? Seems like your first example is casting doubt on that knowledge, though.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. I thought from using the + operator you could cast an int into a string but I see that is a special case. Details, details!
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called "operator overloading", your first example that is. Operator overloading is when you redefine an operator like +, which normally just adds two numbers together, and give it another meaning when you apply it to other kinds of operands. In Java only the language designers are allowed to do operator overloading, and they have only done it in a very small way. In other languages it's possible for any programmer to do operator overloading, and sometimes when programmers are allowed to do this sort of thing the result is code which is a candidate for The Daily WTF.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:It isn't clear to me why I can implicitly cast the int in the first code but can't explicitly cast it in the second code.

In the 1st code snippet, you don't use a cast, but (as Paul) already mentioned the + concatenation operator. In the 2nd code snippet you try to explicitly cast an int to a String, which results in a compiler error (because you can't cast a primitive to a String).

This + concatenation operator can only be used on a String object. If you would do the same with a StringBuilder, you'll get a compiler error. Illustrated in this code snippet:
But with a String you can concatenate everything, both primitives and other objects. A little example:Output: java rocks! 5java.lang.Object@4979c0a7java.io.IOException: abc

And another little thing, but very important for the OCAJP exam: the + operator is often used in println statements in combination with primitives and strings. You really need a perfect understanding how this operator behaves. Otherwise you could fail the exam. So time for a little test. What's the output of these statements?

Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading about using the + operator with the print statement but I am going to try to answer the quiz without going back to the book. I think the output will be
java35
8java

P.S. I couldn't wait to see if I got it right so I ran the code and I was right! At least I won't get a 0 on the exam!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:I think the output will be
java35
8java

Spot-on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic