将int强制转换为Java上的Object [英] Casting int to Object on java

查看:95
本文介绍了将int强制转换为Java上的Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:我在Eclipse的环境中工作。

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;

但只有此转换:

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



我理解为什么你可以在 Object Integer ,但为什么原始变量 - 有时候你可以,有时候你不能做一个铸造?

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?

谢谢

我附上编译器不允许我在 int 变量到对象:

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);
    }


推荐答案

甚至可以这样做:

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

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

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

从Java 1.5开始,Java引入了自动 boxing 的概念。所以,以下变得OK:

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

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

因为编译器知道如何从原语 int 到框格类型 Integer 自动;和从 Integer 对象,这是,没有问题。

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.

但是,你想要做什么:

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

基本上告诉编译器避免你明确告诉编译器将 a 作为 int ,并引用 int 转换为对象。编译器不是为了处理这种情况。

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.

这篇关于将int强制转换为Java上的Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆