Executing int in Object in java

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

Sometimes the computer does not give 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 between casting Objectin Integer, but why primitive variable - there are times when you can, and have the time, you can not do the casting?

thank

I am attaching code that the compiler does not allow me to cast between a intvariable in an 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);
    }
+4
source share
6 answers

Prior to Java 1.5, you couldn't even do this:

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

The compiler will complain that it ahas a primitive data type and therefore cannot be passed to the object.

Java 1.5, Java . , :

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

, int Integer; Integer Object , , .

, :

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

, . a int int Object. .

+11

, .. :

 Object x = (int)a;  

:

 Object x = (Integer)a;  

Integer - , int - .
, , , - Object x, :

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

ClassCastException, Integer.

+3

An Integer Object .

An int . Object. , int . , . , int Object - Autoboxing

0

int, , Integer, - ​​Java.

0

. jdk 1.6, .

    int i=5;

    Object test = (int)i;

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

: java.lang.Integer

0
  • int - .

    : int a = 5;

  • Integer - - ( ).

    : Integer a = new Integer(5);


Integer a = 5;

Integer a = new Integer(5);

Autoboxing ( Java 5.0)


  • int Object, () .
  • .
  • , Integer Object.

Object ans = (Integer) a;

autoboxing, .

Object ans = (int) a;

gives a compiler error, because the cast intis successful, but the link cannot be assigned Object.

Hope this helps. Good luck.

-1
source

Source: https://habr.com/ru/post/1543823/


All Articles