Are static methods offered by wrapper classes ofte

2019-08-27 17:56发布

问题:

I'm curious if static methods of wrapper classes are really helpful.

Which of them are most useful and popularly used? Can you present any must-know tricks involving these methods?

Thanks in advance.

回答1:

The compare methods are useful to handle the primitive counterparts.

static int  compare(primitive p1, primitive p2) 
          Compares the two specified primitive values.

Possible use:

@Override
public int compareTo(MyClass other){
    return Double.compare(this.myDoubleField, other.myDoubleField);
}


回答2:

Integer.parseInt(..) is used a lot. I don't have statistics though. I've used half of them, but of course they are all useful in certain contexts.



回答3:

Integer.toString(...) is used for turning an integer to string without resorting to "" + i.