Using int vs Integer

后端 未结 9 1465
抹茶落季
抹茶落季 2020-12-01 01:28

I came across a class using Integer variables to capture size to be used in a for loop. Is this good practice or should we use the int primitive data type?

I         


        
9条回答
  •  無奈伤痛
    2020-12-01 01:59

    When there is a need of using objects, you have to use the Wrapper classes, like Integer, Double, Float, etc...

    eg:

     int n = Integer.parseInt("10");
    

    Here we are converting the string to an integer (Primitive type) , but method parseInt(String str) works only on Wrapper classes (ie Object), so we used it... you will find many more use of it in java.

提交回复
热议问题