Does Java have mutable types for Integer, Float, Double, Long?

前端 未结 7 1583
野趣味
野趣味 2020-12-01 15:48

I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in?

http://

7条回答
  •  醉话见心
    2020-12-01 15:58

    You can import the org.omg.CORBA package(or just the class you need) and in it you can use the Holder classes.

    For example, it has the "IntHolder" where the field where it stores the integer is public, giving access to modify it.

    public static void triple(IntHolder x){
        x.value = 3 * x.value;
    }
    
    IntHolder mutableInt = new IntHolder(10);
    triple(mutableInt);     
    System.out.println(mutableInt.value);
    

    It also has "LongHolder" and "DoubleHolder" and tons of others that you can use. Use with caution.

    Here is the api for it: https://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/package-summary.html

提交回复
热议问题