0

I know of this

Integer.parseInt(args[0]);

though in some other code I noticed this counts[(int) B.get(file,rank)]; where the (int) is supposedly converting the returned value from B.get into a Integer.

I can understand the validity of the Integer.parseInt statement, but where does the second one come from and is it proper Java code?

0

3 Answers 3

3

The second one is a cast; it doesn't turn one kind of object into another kind, but it allows you to change the form in which you are using the object.

Say I have an object of type Mammal but I happen to know that this particular instance is a Cat. I can do (Cat)myMammal and refer to the methods and properties that a Cat would have.

Needless to say this isn't a way to convert a string to an int. In the example you gave, you're taking an object of undetermined type (the output of get) and asserting that it's an int. This will only work if it really is an int.

If you want to convert a string to an int, you have to parse.

Sign up to request clarification or add additional context in comments.

3 Comments

Am I right to say that Integer.parseInt doesn't convert anything as well, but it just returns an integer?
No, Integer.parseInt() does do actual conversion (parsing to use the more correct term). It takes an object of type string and then "converts" it into an object of type int. If the string can't be converted to an int (because it's out of range or contains illegal characters) then a NumberFormatException will be thrown.
Integer.parseInt is absolutely converting (as berry says); a string that has numbers in it is a totally different thing (a sequence of encoded characters that, based on the proper encoding, are encoded as integer characters) from an actual number. The number has to be decoded or parsed from the string. Also, plenty of strings aren't parseable into an integer.
1

Integer.parseInt() takes a string and converts that to a int.

Using (int) is just a cast, which won't work with a string. Casting works by telling the compiler that you know the object you've got is already of that particular type. The compiler trusts, you - if you get this wrong you'll face a ClassCastException (though the compiler will moan at you if it works out that the cast can never work if the objects aren't part of the same inheritance hierarchy.)

In the case of primitives casting can be used to demote primitives from one type to another, so casting a double to an int works by dropping the decimal component.

Comments

1

IMHO, this is a non-sense, casting to Integer together with autoboxing would be fine, but I know no single case when this compiles without warning and without error.

final Map<String, Integer> m1 = new HashMap<String, Integer>();
m1.put("a", 42); // autoboxing
final int n = (int) m1.get(42); // WARNING: Unnecessary cast from Integer to int


final Map<String, Long> m2 = new HashMap<String, Long>();
m2.put("a", 43L); // autoboxing
final int n2 = (int) (long) m2.get(42); // fine
final int n2a = (int) m2.get(42); // ERROR: Cannot cast from Long to int

final Map<String, Object> m3 = new HashMap<String, Object>();
m3.put("a", 43); // autoboxing
final int n3 = (Integer) m3.get(42); // fine
final int n3a = (int) m3.get(42); // ERROR: Cannot cast from Object to int

Maybe it was a cast to Integer?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.