I'm not habitual to casting a primitive data type to an object. Saw some code like:
public static int CompareAges(Person p1, Person p2) {
Integer age1 = p1.getAge();
return age1.compareTo(p2.getAge());
}
The instantiation of age1 seemed extraneous, so I tried to write the code as:
public static int CompareAges(Person p1, Person p2) {
return p1.getAge().compareTo(p2.getAge());
}
But that raised a compiler error because p1.getAge() is a primitive data type int and not an Integer, which is an object.
Intuitively, I did:
public static int CompareAges(Person p1, Person p2) {
return ((Integer) p1.getAge()).compareTo(p2.getAge());
}
and it worked!
The question: What did I miss? Since when did we start casting primitives as value types?
It's what happens internally:
1. Integer age1 = p1.getAge();
Integer != int
Integer = Integer.valueOf(int)
Integer age1 = Integer.valueOf(p1.getAge());
2. p1.getAge().compareTo(p2.getAge());
int.compareTo(int)
^^^
// it's just a primitive type, as a result - the compile error
3. ((Integer) p1.getAge()).compareTo(p2.getAge())
Integer.compareTo(int)
Integer.compareTo(Integer.valueOf(int))
((Integer) p1.getAge()).compareTo(Integer.valueOf(p2.getAge()))
4. (Integer) p1.getAge() ---> Integer.valueOf(p1.getAge())
// why so? look at callOfCode's answer
But, in my opinion, ((Integer) p1.getAge()).compareTo(p2.getAge()) looks ugly.
I would replace it to
p1.getAge() > p2.getAge() ? 1 : (p1.getAge() < p2.getAge() ? -1 : 0)
or to
Integer.compare(p1.getAge(), p2.getAge()) // java 7+
I wouldn't like to do casting in this case.
More about "autoboxing/unboxing" you may find here.
Each primitive has its own boxing type
The Boxing could be implicit (Autoboxing) or explicit like you did it in your code.
p2.getAge() is an Example of AutoBoxing or Implicit Boxing. The method compareTo takes an Object. As it is a primitive, Java converts it automatically to the correspondent Boxing Object (Integer). You did the explicit Boxing with the cast (Integer) p1.getAge()
Difference between new Integer(primitive); and Integer integer = (Integer)primitive; Is only on bytecode level. In the first case, new object of Integer class is created in memory. In the second case, Integer.valueOf(primitive) is called internally. This is the static method that checks if primitive falls into range of -128 to 127 and if it falls, it returns value from integer cache and no new object is being created. If it not falls into that range, new Integer object is instantiated. It is used for efficiency. But such efficiency is unsignificant novadays.
int z = 1;
Integer a = (Integer)z;
Integer b = (Integer)z;
//Prints true, since Integer object is retrieved from cache (range of -128 to 127)
System.out.println(a == b);
int w = 999;
Integer c = (Integer)w;
Integer d = (Integer)w;
//Prints false, since 999 is not in cache, new Integer objects are instantiated and they points to different places in memory
System.out.println(c == d);
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
May I know how == works here?
public class App {
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
int i1 = new Integer(10);
int i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
When I am comparing references of two different String with the same values, then == says that they are not equal, but when I am comparing references of two integers with the same values, then == says that they are equal. Why is it so?
Ok, a more meaningful compare would be
public class App{
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
int is primitive, and == will just compare the value. Integer is an object, and == for objects is only true if it points to the same place in memory, otherwise it will be false. For primitives (int, boolean, etc...) == will test the value.
int i = Integer(10) is really int i = (Integer(10)).intValue(), java just does it automagically.
Edited to add more explanation:
Java has 2 types of types. Primitive types and Class types. A primitive type is just the value of the type: an int primitive type is just that, and int. There are not methods or anything, it just is an int.
If you say int a = 4, a is a primitive variable that points to a memory address that contains an int. That's it.
A Class type is a java object, so Integer is a class type. It represents an integer value, so Integer a = new Integer(4) would create an integer object, and a would point to a memory location that contains the object that new... created.
What's different?
Good question. In the latter example (new Integer(4)) this is an integer and some other stuff, stuff like a toString() method and some other stuff.
So, a recap:
A primitive is a value stored in memory somewhere.
A class is an object stored in memory somewhere, and in the case of Integer it represents an int but it has some other stuff associated with it (i.e. the `toString() method).
Now, == is a test for equivalency. So int == int checks to see if the primitive value is the same, since that is the only information that a primitive type has.
== for objects does mean something. Since an object points to a more than just a 4 or 8 or -1, the == only returns true if the object variables point to the same place in memory. If it does not, then you get false.
So:
int i = 1;
int j = 1;
Integer k = new Integer(1)
Integer l = new Integer(1)
Integer m = k;
Integer n = new Integer(k)
i == j -> true
k == l -> false
k == k -> true
k == l -> true
n == m -> false
n.intValue() == m.intValue() -> true (intValue returns a primitive type)
Now, .equals is a test to see if two objects are equal, which means different things for different objects.
For an Integer, a.equals(b) is true if the intValue()s are the same for a and b. However, a == b is true if a and b point at the same object in memory
Now, thing about an object that has 2 integers in it (like an x,y coordinate).
I have a question: I work in environment of Eclipse.
Sometimes the computer does not give to the following casting:
int a ...
Object ans = (int) a;
But only this conversion:
int a ...
Object ans = (Integer) a;
I understand why you can do the casting between Object to Integer, but why primitive variable - there are times when you can, and there are times you can not do a casting?
Thank you
I am attaching the code which the compiler not let me make casting between int variable to object:
/** #return minimum element */
public Object minimum(){
return minimum(this.root);
}
public Object minimum(BSTNode node){
if (node.left != null) return minimum(node.left);
return node.data;
}
/** #return maximum element */
public Object maximum(){
return maximum(this.root);
}
public Object maximum(BSTNode node){
if (node.right != null) return maximum(node.right);
return node.data;
}
public Object findNearestSmall(Object elem) {
int diff;
diff = (int)maximum() - (int)minimum();
if (compare(minimum(), elem) == 0) return elem;
else return findNearestSmall(elem, this.root, diff);
}
public Object findNearestSmall(Object elem, BSTNode node, int mindiff){
if(node == null) return (int)elem - mindiff;
int diff = (int)elem - (int)node.data;
if(diff > 0 && mindiff > diff) mindiff = diff;
/* Case 2 : Look for in left subtree */
if(compare(node.data, elem)>-1)
return findNearestSmall(elem, node.left, mindiff);
else
/* Case 3 : Look for in right subtree */
return findNearestSmall(elem, node.right, mindiff);
}
Before Java 1.5, you couldn't even do this:
int a;
...
Object x = (Integer) a;
The compiler would complain that a is of a primitive data type, and therefore cannot be cast to an object.
Starting with Java 1.5, Java introduced the concept of automatic boxing. So, the following became OK:
int a;
...
Object x = (Integer) a;
Because the compiler knows how to convert from a primitive int to the boxed type Integer automatically; and from Integer to an Object it's, well, not a problem.
However, what you're trying to do:
int a;
...
Object x = (int) a;
Is basically telling the compiler to avoid boxing. You explicitly tell the compiler to leave a as an int, and put a reference to that int into an Object. The compiler isn't designed to deal with such a case.
You cannot cast from a referenced data-type to a primitive data-type i.e. you cannot:
Object x = (int)a;
You can however do:
Object x = (Integer)a;
because Integer is a class and int is a primitive data-type.
If I assume it correctly, the functionality you want to achieve is get the integer's value from Object x which can be done as:
Object x = (Integer)a;
//Do something and somewhere else
int z = ((Integer)x).intValue();
This may through a ClassCastException if it is not of Integer class.
You should look into the difference between int which is a primitive type, and Integer, which is a wrapper class in Java.
An Integer is also an Object and stays as an Object on the Heap.
An int is a primitive type. It is NOT an Object. An Object has its own state and behavioral properties, int doesn't have those. So you get a compilation error when trying to convert an Object to a primitive. On the other hand, Converting an int to Object is possible because of Autoboxing
But I am able to execute the following code. I am using jdk 1.6 and the following code is not throwing me any errors or runtime exceptions.
int i=5;
Object test = (int)i;
System.out.println(test.getClass());
Output: class java.lang.Integer
int is a primitive type.
Way of declaration: int a = 5;
Integer is a wrapper class (it extends Object).
Way of declaration: Integer a = new Integer(5);
When you write
Integer a = 5;
compiler automatically converts it to
Integer a = new Integer(5);
This feature is called Autoboxing (since Java 5.0)
int can not be casted to Object as it is not a referenced data type (object) at all.
But it can be casted to other primitive types.
On the other hand, Integer can be casted to Object.
When you write
Object ans = (Integer) a;
compiler does autoboxing and then casts it.
Object ans = (int) a;
gives a compiler error because the cast to int is successful, but it can not be assigned to an Object reference.
Hope this helps.
Good luck.
public class BuildStuff {
public static void main(String[] args) {
Boolean test = new Boolean(true);
Integer x = 343;
Integer y = new BuildStuff().go(test, x);
System.out.println(y);
}
int go(Boolean b, int i) {
if(b)
return (i/7);
return (i/49);
}
}
This is from SCJP , I understand that answer is "49", but I have a doubt. When we create an object and pass a value in that object. Let's say: Dog d = new Dog("tommy"); in this statement d is reference to object Dog and Dog has "name" instance variable set to "tommy". But it doesn't mean d = tommy.
However, in case of Boolean, when we passed true in Boolean Object. It sets reference to true. Why it is in case of Boolean? Is there any other class too?
The passed-in Boolean b can be converted to a primitive boolean with an unboxing conversion, resulting in the expression true for the if statement.
All primitive types have corresponding built-in wrapper classes for them, and Java will unbox/box as necessary.
Boolean <=> boolean
Byte <=> byte
Character <=> char
Short <=> short
Integer <=> int
Long <=> long
Float <=> float
Double <=> double
(An unboxing conversion will fail with a NullPointerException if the corresponding wrapper class instance is null:
Boolean test = null;
// NPE
boolean b = test;
)
Here's Java's tutorial on boxing and unboxing.
From the javadoc: The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.
If you have
Boolean a = new Boolean(true);
Boolean b = new Boolean(true);
a and b are different objects, but a.booleanValue() and b.booleanValue() return the same primitive. This can be verified by testing a == b versus a.equals(b).
There are other classes like this, viz. Integer, Double, etc. As others have already mentioned, you should look into autoboxing and unboxing.
what is wrong with this code??
int amount= (int) amountSpnr.getValue(); // 1
float total = (float) productData[3]*amount; // 2
total2pay+=total;
totalFld.setText(total2pay+"");
model.addRow(new Object[]{productData[0], productData[1],productData[2],productData[3], amount, total});`
says :
inconvertible types
(for the 1st line)- required int found Object
(for the 2nd line)- required float found Object
What can i do?
You cannot cast Objects in Java to primitive (except for their respective wrapper classes).
Try using this:
Object obj1 = amountSpnr.getValue();
Object obj2 = productData[3];
if (obj1 instanceof Integer) {
int amount = (Integer) obj1; // 1
}
if (obj2 instanceof Float) {
float total = (Float) obj2; // 2
total *= amount;
}
In the above case, Object will be down-casted to Integer type, which will then be unboxed to primitive integer. Same is the case with Float.
Note: Be sure to add instanceof check before you perform downcasting just to make sure that you do not end up getting CastCastException.
If you need to tranform your Object into a int primitive you have to follow 2 steps :
First make sure your object has the good type.
Cast it with (Integer) or (Float). You can use instanceof if you're not sure of the type returned by getValue (case 1) or by you array (case 2)
Then use .intValue() or .floatValue().
Example :
int amount=((Integer)amountSpnr.getValue()).intValue();
float total = ((Float) productData[3]*amount).floatValue();
How can I cast an Object to an int in java?
If you're sure that this object is an Integer :
int i = (Integer) object;
Or, starting from Java 7, you can equivalently write:
int i = (int) object;
Beware, it can throw a ClassCastException if your object isn't an Integer and a NullPointerException if your object is null.
This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int.
int is a primitive so it can't be stored as an Object, the only way is to have an int considered/boxed as an Integer then stored as an Object.
If your object is a String, then you can use the Integer.valueOf() method to convert it into a simple int :
int i = Integer.valueOf((String) object);
It can throw a NumberFormatException if your object isn't really a String with an integer as content.
Resources :
Oracle.com - Autoboxing
Oracle.com - Primitive Data types
On the same topic :
Java: What's the difference between autoboxing and casting?
Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);
Convert Object into primitive int
Scenario 1: simple case
If it's guaranteed that your object is an Integer, this is the simple way:
int x = (Integer)yourObject;
Scenario 2: any numerical object
In Java Integer, Long, BigInteger etc. all implement the Number interface which has a method named intValue. Any other custom types with a numerical aspect should also implement Number (for example: Age implements Number). So you can:
int x = ((Number)yourObject).intValue();
Scenario 3: parse numerical text
When you accept user input from command line (or text field etc.) you get it as a String. In this case you can use Integer.parseInt(String string):
String input = someBuffer.readLine();
int x = Integer.parseInt(input);
If you get input as Object, you can use (String)input, or, if it can have an other textual type, input.toString():
int x = Integer.parseInt(input.toString());
Scenario 4: identity hash
In Java there are no pointers. However Object has a pointer-like default implementation for hashCode(), which is directly available via System.identityHashCode(Object o). So you can:
int x = System.identityHashCode(yourObject);
Note that this is not a real pointer value. Objects' memory address can be changed by the JVM while their identity hashes are keeping. Also, two living objects can have the same identity hash.
You can also use object.hashCode(), but it can be type specific.
Scenario 5: unique index
In same cases you need a unique index for each object, like to auto incremented ID values in a database table (and unlike to identity hash which is not unique). A simple sample implementation for this:
class ObjectIndexer {
private int index = 0;
private Map<Object, Integer> map = new WeakHashMap<>();
// or:
// new WeakIdentityHashMap<>();
public int indexFor(Object object) {
if (map.containsKey(object)) {
return map.get(object);
} else {
index++;
map.put(object, index);
return index;
}
}
}
Usage:
ObjectIndexer indexer = new ObjectIndexer();
int x = indexer.indexFor(yourObject); // 1
int y = indexer.indexFor(new Object()); // 2
int z = indexer.indexFor(yourObject); // 1
Scenario 6: enum member
In Java enum members aren't integers but full featured objects (unlike C/C++, for example). Probably there is never a need to convert an enum object to int, however Java automatically associates an index number to each enum member. This index can be accessed via Enum.ordinal(), for example:
enum Foo { BAR, BAZ, QUX }
// ...
Object baz = Foo.BAZ;
int index = ((Enum)baz).ordinal(); // 1
Assuming the object is an Integer object, then you can do this:
int i = ((Integer) obj).intValue();
If the object isn't an Integer object, then you have to detect the type and convert it based on its type.
#Deprecated
public static int toInt(Object obj)
{
if (obj instanceof String)
{
return Integer.parseInt((String) obj);
} else if (obj instanceof Number)
{
return ((Number) obj).intValue();
} else
{
String toString = obj.toString();
if (toString.matches("-?\d+"))
{
return Integer.parseInt(toString);
}
throw new IllegalArgumentException("This Object doesn't represent an int");
}
}
As you can see, this isn't a very efficient way of doing it. You simply have to be sure of what kind of object you have. Then convert it to an int the right way.
You have to cast it to an Integer (int's wrapper class). You can then use Integer's intValue() method to obtain the inner int.
Answer:
int i = ( Integer ) yourObject;
If, your object is an integer already, it will run smoothly. ie:
Object yourObject = 1;
// cast here
or
Object yourObject = new Integer(1);
// cast here
etc.
If your object is anything else, you would need to convert it ( if possible ) to an int first:
String s = "1";
Object yourObject = Integer.parseInt(s);
// cast here
Or
String s = "1";
Object yourObject = Integer.valueOf( s );
// cast here
I use a one-liner when processing data from GSON:
int i = object != null ? Double.valueOf(object.toString()).intValue() : 0;
If the Object was originally been instantiated as an Integer, then you can downcast it to an int using the cast operator (Subtype).
Object object = new Integer(10);
int i = (Integer) object;
Note that this only works when you're using at least Java 1.5 with autoboxing feature, otherwise you have to declare i as Integer instead and then call intValue() on it.
But if it initially wasn't created as an Integer at all, then you can't downcast like that. It would result in a ClassCastException with the original classname in the message. If the object's toString() representation as obtained by String#valueOf() denotes a syntactically valid integer number (e.g. digits only, if necessary with a minus sign in front), then you can use Integer#valueOf() or new Integer() for this.
Object object = "10";
int i = Integer.valueOf(String.valueOf(object));
See also:
Inheritance and casting tutorial
int i = (Integer) object; //Type is Integer.
int i = Integer.parseInt((String)object); //Type is String.
Can't be done. An int is not an object, it's a primitive type. You can cast it to Integer, then get the int.
Integer i = (Integer) o; // throws ClassCastException if o.getClass() != Integer.class
int num = i; //Java 1.5 or higher
You can't. An int is not an Object.
Integer is an Object though, but I doubt that's what you mean.
If you mean cast a String to int, use Integer.valueOf("123").
You can't cast most other Objects to int though, because they wont have an int value. E.g. an XmlDocument has no int value.
I guess you're wondering why C or C++ lets you manipulate an object pointer like a number, but you can't manipulate an object reference in Java the same way.
Object references in Java aren't like pointers in C or C++... Pointers basically are integers and you can manipulate them like any other int. References are intentionally a more concrete abstraction and cannot be manipulated the way pointers can.
int[] getAdminIDList(String tableName, String attributeName, int value) throws SQLException {
ArrayList list = null;
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("SELECT admin_id FROM " + tableName + " WHERE " + attributeName + "='" + value + "'");
while (result.next()) {
list.add(result.getInt(1));
}
statement.close();
int id[] = new int[list.size()];
for (int i = 0; i < id.length; i++) {
try {
id[i] = ((Integer) list.get(i)).intValue();
} catch(NullPointerException ne) {
} catch(ClassCastException ch) {}
}
return id;
}
// enter code here
This code shows why ArrayList is important and why we use it. Simply casting int from Object. May be its helpful.
For Example Object variable; hastaId
Object hastaId = session.getAttribute("hastaID");
For Example Cast an Object to an int,hastaID
int hastaID=Integer.parseInt(String.valueOf(hastaId));
Refer This code:
public class sample
{
public static void main(String[] args)
{
Object obj=new Object();
int a=10,b=0;
obj=a;
b=(int)obj;
System.out.println("Object="+obj+"\nB="+b);
}
}
so divide1=me.getValue()/2;
int divide1 = (Integer) me.getValue()/2;
We could cast an object to Integer in Java using below code.
int value = Integer.parseInt(object.toString());
If you want to convert string-object into integer...
you can simply pass as:
int id = Integer.valueOf((String) object_name);
Hope this will be helpful :-)
Integer x = 11
int y = x.intValue();
System.out.println("int value"+ y);
Finally, the best implementation for your specification was found.
public int tellMyNumber(Object any) {
return 42;
}
first check with instanceof keyword . if true then cast it.