Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why need to convert from Integer to int [closed]

Tags:

I encountered a problem when I use TreeMap.

    Map<Integer, Integer> a = new TreeMap<Integer, Integer>();     a.put(5,1);     a.put(3,2);     a.put(4,3);     int target = 7;     System.out.println(target - a.get(5)); //line 6     for(Map.Entry b : a.entrySet()){         System.out.println(target - b.getValue()); //line 8     } 

The code above gave me an compile error. However, when I change the line 8 to this:

    Map<Integer, Integer> a = new TreeMap<Integer, Integer>();     a.put(5,1);     a.put(3,2);     a.put(4,3);     int target = 7;     System.out.println(target - a.get(5)); //line 6     for(Map.Entry b : a.entrySet()){         System.out.println(target - (int) b.getValue()); //line 8     } 

Then it works. Could anyone give me some ideas why I don't need any change in line 6 but need convert an Integer to int in line 8?

like image 758
yyyyyyyyoung Avatar asked Feb 24 '16 07:02

yyyyyyyyoung


People also ask

Can we assign Integer to int in java?

In java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting. In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value.

How do you convert Integer to int?

Convert Int to Integer Using the Integer. valueOf() Method in Java. This is another that we can use to convert an int to an Integer in Java. Here, we used valueOf() method of the Integer class.

Can we convert long to Integer?

We can convert long to int in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype).

What is difference between int and Integer in java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.


2 Answers

You ignored the "raw type" warning in the for statement. It should be:

    for(Map.Entry<Integer,Integer> b : a.entrySet()) {         ... 

The raw type would cause getValue() to return Object. If you provide the type parameters then the compiler knows it will return Integer, and this will get unboxed automatically.

like image 144
Jim Garrison Avatar answered Oct 04 '22 02:10

Jim Garrison


There are more than one operations that are underneath (int) b.getValue(). First getValue() returns Object and then that is casted to Integer which is then unboxed to int . a.get() in it's own immediately returns Integer since you declared a with Integer in <> (see https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object) it returns the V type).

The target - b.getValue() didn't compile because it was int - Object operation which is not defined for operator - . That is why you have to do cast to (int) .

Following wont work even though b is referring to the object that is Integer.

Integer a = 1; Object  b = a; System.out.println(3 - b); // compile time error "bad operand types for binary operator '-'" 

Following works

Integer a = 1; Object  b = a; System.out.println(3 - a); 

Also works

Integer a = 1; Object  b = a; System.out.println(3 - (int) b); //this is when you say to compiler not to worry since you are sure that object reference refers to the object that is Integer. 

Though if at runtime b doesn't refer to int the cast will fail. Even if it did compile in the first place.

Integer a = 1; String  s = "shouldn't work at runtime"; Object  b = s; System.out.println(3 - (int) b); //this  will compile but fail at runtime 
like image 42
Developer Marius Žilėnas Avatar answered Oct 04 '22 00:10

Developer Marius Žilėnas