Speed ​​up and down in java

I can understand what an increase is, but Downcasting is a bit confusing. My question is why should we play down? Can you help me in real time? Is it really that important?

+3
source share
8 answers

Downcasting is a necessary evil, for example, when dealing with legacy APIs that return non-generic collections. Another classic example is the equals method:

public class Phleem{

    public Phleem(final String phloom){
        if(phloom == null){
            throw new NullPointerException();
        }
        this.phloom = phloom;
    }

    private final String phloom;

    public String getPhloom(){
        return phloom;
    }

    @Override
    public boolean equals(final Object obj){
        if(obj instanceof Phleem){
            // downcast here
            final Phleem other = (Phleem) obj;
            return other.phloom.equals(phloom);
        }
        return false;
    }

    // ...

}

I can’t come up with an example where I need to use Upcasting. Well, good practice is to return the least specific Object from the method, but this can be done without casting:

public Collection<String> doStuff(){
    // no casting needed
    return new LinkedHashSet<String>();
}
+8
source

ServletResponse , HttpServletResponse.

, , " ". , , , .

+2

. , , ...
- " Java- 2- "

0

Downcasting , , , , ( ), .

: - , , String, String. , ClassCastException:

Object receivedObject = receiveFromSomewhere();
if(receivedObject instanceof String){
    receivedString = (String) receivedObject;
}
0

Downcasting

//: RTTI.java
// Downcasting & Run-Time Type
// Identification (RTTI)
import java.util.*;

class Useful {
  public void f() {}
  public void g() {}
}

class MoreUseful extends Useful {
  public void f() {}
  public void g() {}
  public void u() {}
  public void v() {}
  public void w() {}
}

public class RTTI {
  public static void main(String[] args) {
    Useful[] x = {
      new Useful(),
      new MoreUseful()
    };
    x[0].f();
    x[1].g();
    // Compile-time: method not found in Useful:
    //! x[1].u();
    ((MoreUseful)x[1]).u(); // Downcast/RTTI
    ((MoreUseful)x[0]).u(); // Exception thrown
  }
} ///:~ 

.

0

, , , .

, ObjectInputStream.

0

, , downcasting , , .

:

Object o = doStaff();
String s = (String) o; 

:

Object o = new Object();
String s = (String) s;

Q1: downcast?

, downcast. , , .

Q2: ?

, , .

0

, equals, Object. , , Object, , , . , . , equals(), .

class MySuperClass
{
    private int num1;

    public MySuperClass() {}

    public MySuperClass(int num1)
    {
        this.num1 = num1;
    }
}

class MySubClass extends MySuperClass
{
    private int num;

    public MySubClass(int num)
    {
        this.num = num;
    }

    public boolean equals(Object o)
    {
        if (!(o instanceof MySubClass))
            return false;

        MySubClass mySub = (MySubClass)o;

        return(mySub.num == num);
    }

    public static void main(String[] args)
    {
        Object mySub = new MySubClass(1);
        MySuperClass mySup = new MySubClass(1);

        System.out.println(mySub.equals(mySup));
    }

}
0

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


All Articles