• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding cast  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a variable that contains an integer string. Why does this work:

while this does not:

TIA.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a simple answer is - Integer's parseInt method accept a String argument. So, no matter what String you pass to parseInt, the code will compile. But, it will throw a NumberFormatException if the String is not in number format (e.g. "a10" instead of "10" etc.)

However, String and int are not compatible types, so, while doing a casting, compiler itself figures this out (type incompatibility) and gives a compile time error.

I hope this helps.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second case you're trying to get the JVM to convert automatically from a String to an int. That doesn't work, because a String isn't an int.

However, the writers of the Java libraries have provided a method that will do the conversion for you, and that's what you're doing in the first case. In the same way, there's nothing stopping you from writing your own method to convert anything to anything else, and calling that.

Casts are used in two cases:

- For reference types (including String) it allows you to change the type of reference used. It does not change the object itself. For instance, if you have an Object reference, and it's really containing a reference to a String, then you can cast it to a String. An exception will be thrown if it isn't really a String. E.g.

- For numeric primitive types, it lets you convert between them (losing precision if necessary).

But your second case is trying to cast from a reference type (String) to a primitive type.

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I was not clear as to what I was looking for. I expected the parseInt to produce an integer of value 10, which it does. I was expecting (int) to produce an integer value of 18760 rather than a compile error.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting does not do any kind of conversion, so you can't use a cast to convert a string to an int or vice versa.

What a cast means, is that you tell the compiler "look, I have this thing here of type A, but I want you to treat it as type B, don't tell me the types don't match". No more and no less - there is no automatic conversion from A to B.

Note that the type of the object will still be checked when you run the program - if the object you're casting turns out to not be a B, then you'll get a ClassCastException.

Certain casts are not allowed by the compiler - for example, the compiler knows that it's never possible to cast an object to int, so it won't allow that.

Dennis Putnam wrote:Perhaps I was not clear as to what I was looking for. I expected the parseInt to produce an integer of value 10, which it does. I was expecting (int) to produce an integer value of 18760 rather than a compile error.

18760? What's the logic behind that number?

(To make things a little more complicated, there are certain casts between primitive types, for example from float to int, where Java does do an automatic conversion).
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the stored value of the string. "10"=x4948=18760.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some languages support casting from string to decimal, even implicit casting. Java is not one of those languages. You have to use one of the core libraries to accomplish that task -- as you have figured out.

Henry
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:That is the stored value of the string. "10"=x4948=18760.


Couple of thoughts for you, Dennis: First, check your ASCII, since "1" is 0x31 and "0" is 0x30 (which, in decimal, would be 49 and 48, but 0x3130 would be decimal 12592.) Second, while C++ would store "10" as two consecutive bytes, one per character, Java doesn't work that way. "A String represents a string in the UTF-16 format..." say the online docs. (Also, fwiw, even 0x3130 stored sequentially is ambiguous as an int, since its 16-bit value would depend on whether you were on a little-endian machine, as The Creator intended you to be, or on a big-endian machine, if you have gone over to The Dark Side.)

The String data type is pretty opaque. That is, you really don't know how its values are stored, nor are you supposed to be able to find out. So, today it stores them however it stores them, but tomorrow it may store them some other way. If your code relies on the public methods of the String class and Java's own operations (which it pretty much has to, unless you are playing some dangerous game with native code, I suppose), internal changes made to how String does its job will never affect you.

To wax philosophical for a moment: As a C programmer coming to Java late in life (in my 40s), I found this sort of thing made me feel like I was programming with mittens on. That is, good old C let me get right to the bytes, in a defined order, via an address stored in a pointer. I liked that. Then along comes Java which has no pointers and, in what seemed a condescending effort to make sure I didn't hurt myself, replaced all those wonderful pointer-based accesses to memory with method calls on objects. "Do you really mean to say I must call an entire subroutine, just to read a byte out of a string???" I heard myself ask (which was a pretty lonely thing, since I work at home, by myself, and my computer is instransigently inscrutable when it comes to answering my questions like that one). But, the answer was, "Yes, boy, that's how it works now." Felt like writing some kind of shell script or something, with huge, clunky calls replacing all of my clean, efficient, accesses to buffers. Yet, that really it is how it works now. And my computer is so fast I never notice the overhead. I still don't like it, but I have had to get used to it. You will to.

Cheers.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:I heard myself ask (which was a pretty lonely thing, since I work at home, by myself, and my computer is instransigently inscrutable when it comes to answering my questions like that one). But, the answer was, "Yes, boy, that's how it works now."

"There are few things in life more comforting than the mere knowledge that others have felt just as you have felt..." (or something like that; quoting from memory). Sometimes I also want to stab the monitor but that's when I'm doing a code review and I find a particularly inscrutable and convoluted piece of code (of course, I don't call it that).
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Sometimes I also want to stab the monitor but that's when I'm doing a code review and I find a particularly inscrutable and convoluted piece of code (of course, I don't call it that).


Elegant. We call such code, "elegant."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!