Java 如何将对象转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3661413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:16:42  来源:igfitidea点击:

How to cast an Object to an int

javaobjectcastinginteger

提问by Sheehan Alam

How can I cast an Object to an int in java?

java - 如何在java中将对象转换为int?

采纳答案by Colin Hebert

If you're sure that this object is an Integer:

如果您确定此对象是一个Integer

int i = (Integer) object;

Or, starting from Java 7, you can equivalently write:

或者,从 Java 7 开始,您可以等效地编写:

int i = (int) object;

Beware, it can throw a ClassCastExceptionif your object isn't an Integerand a NullPointerExceptionif your object is null.

当心,它可以抛出一个ClassCastException,如果你的对象是不是IntegerNullPointerException你要的对象是null

This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int.

这样你就假设你的对象是一个整数(包装的整数),然后你把它拆箱成一个整数。

intis a primitive so it can't be stored as an Object, the only way is to have an intconsidered/boxed as an Integerthen stored as an Object.

int是一个原语,所以它不能存储为Object,唯一的方法是将一个int考虑/装箱为 ,Integer然后存储为Object



If your object is a String, then you can use the Integer.valueOf()method to convert it into a simple int :

如果您的对象是 a String,那么您可以使用该Integer.valueOf()方法将其转换为一个简单的 int :

int i = Integer.valueOf((String) object);

It can throw a NumberFormatExceptionif your object isn't really a Stringwith an integer as content.

NumberFormatException如果您的对象不是真正String以整数作为内容的a ,它可能会抛出 a 。



Resources :

资源 :

On the same topic :

在同一主题上:

回答by Etienne de Martel

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.

您必须将其转换为 Integer(int 的包装类)。然后,您可以使用 Integer 的 intValue() 方法来获取内部 int。

回答by Erick Robertson

Assuming the object is an Integerobject, then you can do this:

假设对象是一个Integer对象,那么你可以这样做:

int i = ((Integer) obj).intValue();

If the object isn't an Integerobject, then you have to detect the type and convert it based on its type.

如果对象不是Integer对象,则必须检测类型并根据其类型进行转换。

回答by extraneon

You can't. An intis not an Object.

你不能。Anint不是Object

Integeris an Objectthough, but I doubt that's what you mean.

IntegerObject虽然,但我怀疑这就是你的意思。

回答by Tom

Can't be done. An intis not an object, it's a primitive type. You can cast it to Integer, then get the int.

做不到。Anint不是一个对象,它是一个原始类型。您可以将其转换为 Integer,然后获取 int。

 Integer i = (Integer) o; // throws ClassCastException if o.getClass() != Integer.class

 int num = i; //Java 1.5 or higher

回答by Amy B

If you mean cast a String to int, use Integer.valueOf("123").

如果您的意思是将 String 转换为 int,请使用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.

但是,您不能将大多数其他对象强制转换为 int,因为它们没有 int 值。例如,XmlDocument 没有 int 值。

回答by BalusC

If the Objectwas originally been instantiated as an Integer, then you can downcast it to an intusing the cast operator (Subtype).

如果Object最初被实例化为 an Integer,那么您可以int使用 cast 运算符将其向下转换为 an (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 ias Integerinstead and then call intValue()on it.

请注意,这仅在您至少使用具有自动装箱功能的Java 1.5 时才有效,否则您必须改为声明iasInteger然后调用intValue()

But if it initially wasn't created as an Integerat all, then you can't downcast like that. It would result in a ClassCastExceptionwith 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.

但如果它最初根本不是作为一个创建的Integer,那么你就不能那样沮丧。这将导致消息中ClassCastException带有原始类名。如果由toString()获得的对象表示String#valueOf()表示语法上有效的整数(例如,仅数字,如有必要,前面带有减号),那么您可以使用Integer#valueOf()new Integer()

Object object = "10";
int i = Integer.valueOf(String.valueOf(object));

See also:

也可以看看:

回答by OscarRyz

Answer:

回答:

int i = ( Integer ) yourObject;

If, your object is an integer already, it will run smoothly. ie:

如果您的对象已经是一个整数,它将顺利运行。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:

如果您的对象是其他任何对象,则需要先将其(如果可能)转换为 int:

String s = "1";
Object yourObject = Integer.parseInt(s);
//  cast here

Or

或者

String s = "1";
Object yourObject = Integer.valueOf( s );
//  cast here

回答by Kerem Baydo?an

int i = (Integer) object; //Type is Integer.

int i = Integer.parseInt((String)object); //Type is String.

回答by Martijn Courteaux

@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.

如您所见,这不是一种非常有效的方法。你只需要确定你拥有什么样的对象。然后以正确的方式将其转换为 int 。