在 java 上将 int 转换为 Object

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24115021/
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 10:14:20  来源:igfitidea点击:

Casting int to Object on java

javacasting

提问by ZoharYosef

I have a question: I work in environment of Eclipse.

我有一个问题:我在 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 Objectto Integer, but why primitive variable - there are times when you can, and there are times you can not do a casting?

我理解为什么你可以在Objectto之间进行转换Integer,但为什么是原始变量 - 有些时候你可以,有些时候你不能做转换?

Thank you

谢谢

I am attaching the code which the compiler not let me make casting between intvariable to object:

我附上了编译器不允许我在int变量和对象之间进行转换的代码:

/** @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);
    }

采纳答案by Isaac

Before Java 1.5, you couldn't even do this:

在 Java 1.5 之前,你甚至不能这样做:

int a;
...
Object x = (Integer) a;

The compiler would complain that ais of a primitive data type, and therefore cannot be cast to an object.

编译器会抱怨它a是原始数据类型,因此不能转换为对象。

Starting with Java 1.5, Java introduced the concept of automatic boxing. So, the following became OK:

从 Java 1.5 开始,Java 引入了自动装箱的概念。因此,以下变得确定:

int a;
...
Object x = (Integer) a;

Because the compiler knows how to convert from a primitive intto the boxed type Integerautomatically; and from Integerto an Objectit's, well, not a problem.

因为编译器知道如何自动从原始int类型转换为装箱类型Integer;从IntegerObject它,嗯,不是问题。

However, what you're trying to do:

但是,您要执行的操作是:

int a;
...
Object x = (int) a;

Is basically telling the compiler to avoidboxing. You explicitly tell the compiler to leave aas an int, and put a reference to that intinto an Object. The compiler isn't designed to deal with such a case.

基本上是告诉编译器避免装箱。您明确告诉编译器将离开a作为int,并把该基准intObject。编译器不是为处理这种情况而设计的。

回答by TheLostMind

An Integeris also an Objectand stays as an Object on the Heap.

AnInteger也是 anObject并且作为一个对象留在堆上。

An intis a primitive type. It is NOT an Object. An Object has its own state and behavioral properties, intdoesn'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

Anint原始类型。它不是Object. 一个对象有它自己的状态和行为属性,int没有那些。因此,在尝试将 Object 转换为基元时会出现编译错误。另一方面,将 int 转换为 Object 是可能的,因为Autoboxing

回答by Kgrover

You should look into the difference between intwhich is a primitive type, and Integer, which is a wrapper class in Java.

您应该研究int之间的区别,它是一种原始类型,而Integer是 Java 中的包装类。

回答by Abdul Fatir

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 Integeris a class and intis 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:

因为Integer是一个类并且int是一种原始数据类型。
如果我假设正确,您想要实现的功能是从 Object x 获取整数的值,可以这样做:

 Object x = (Integer)a;  
 //Do something and somewhere else  
 int z = ((Integer)x).intValue();  

This may through a ClassCastExceptionif it is not of Integerclass.

这可能通过一个ClassCastException如果它不是Integer类。

回答by Tanmay Patil

  • intis a primitive type.

    Way of declaration: int a = 5;

  • Integeris a wrapper class(it extends Object).

    Way of declaration: Integer a = new Integer(5);

  • int是原始类型。

    申报方式:int a = 5;

  • Integer是一个包装类(它扩展了 Object)。

    申报方式: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)

此功能称为自动装箱(自 Java 5.0 起)



  • intcan not be casted to Objectas it is not a referenced data type (object) at all.
  • But it can be casted to other primitive types.
  • On the other hand, Integercan be casted to Object.
  • int不能强制转换,Object因为它根本不是引用的数据类型(对象)。
  • 但它可以转换为其他原始类型。
  • 另一方面,Integer可以强制转换为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 intis successful, but it can not be assigned to an Objectreference.

给出编译器错误,因为强制转换int成功,但不能分配给Object引用。

Hope this helps.
Good luck.

希望这可以帮助。
祝你好运。

回答by raja

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.

但我能够执行以下代码。我正在使用 jdk 1.6 并且以下代码没有向我抛出任何错误或运行时异常。

    int i=5;

    Object test = (int)i;

    System.out.println(test.getClass());

Output: class java.lang.Integer

输出:类 java.lang.Integer