3

Suppose I have an object like this:

Public Class Test{
    int a;
}

At some point in my program I want to check whether attribute a is set. I know that if I used Integer instead of int as the type of the attribute I could do something like:

if(test.a!=null)
    ...;

But what if I keep the int there and instead and use this to check:

if(test.a!=0)
    ...;

One problem is that I wouldn't be able to differentiate between a zero value and an unset value, but in my program those are the same, as valid values are all different from 0. Also, using int simplifies things I need to do later on, like comparisons using == .

So would it be fine to use int here, or Integer is always preferred?

4

4 に答える 4

6

It's totally up to you, either is fine (provided "unset" and 0 really mean the same thing in your program). I realize that's not much of an answer, but it's the truth. :-) If "unset" and 0 didn't mean the same thing, that would argue more strongly for Integer so you could properly differentiate them.

Re your comment below:

I just wanted confirmation that an unset int will always be equal to 0

Yes, int is always initialized to 0, per Section 4.12.5 of the JLS.

于 2012-11-03T13:20:22.137 に答える
3

intプリミティブ型は0、宣言で値が指定されていない場合に初期化されます(上記のコードスニペットのように)。

プログラムにとって(値ではなく)状態「set / unset」が重要な場合は、整数(おっしゃるように)、ブール値のダーティフラグ、または「マジックナンバー」を使用できます。

マジックナンバーと比較したい場合は、デフォルト値と同じくらい一般的なものに対してアドバイスします0。Integer.MIN_VALUEのように、プログラムではほとんど見つけられないものを使用し、セッターでそれを防ぎます。

その場合、次のようなことができます。

public class Test {
   private static final int MAGIC_NR = Integer.MIN_VALUE;
   private int var = MAGIC_NR; //set it to the magic nr at declaration time  

   public boolean isVarSet() {
      return var == MAGIC_NR;
   }

   public void setVar(int value) {
       if (value == MAGIC_NR) throw new IllegalArgumentException("Invalid value for VarX"); // guard against setting this value - otherwise you're in trouble
       var = value;
   }

   public int getVar() {
       return var;
   }
}
于 2012-11-03T13:36:21.650 に答える
2

慣例の観点からは問題ありませんが、未設定の値を表すために定数を使用することをお勧めします。

public static final int UNSET = 0;
...
if(test.a == UNSET) {
....
}
于 2012-11-03T13:23:47.790 に答える
-1

It depends on how you want to access it, I'm not entirely clear on how this works but my understanding is a static variable doesn't need to be instantiated, its already an object so you could try something like this.

    public class Test {
        public static int a;
    }

Then refer to it by referring to the Test class,

    if (Test.a != null) {}

Or of course you could simply create an object of the Test class where ever you want to refer to the variable.

    Test test = new Test();
    if (test.a != null) {}

And finally my third solution would be to pass it into the constructor of the new class.

    public class NewCLass {
         int newInt;  
         public NewClass(int num1) {
            this.newInt = num1;
         }
    }

then when you created an object of that class you'd have to pass an int into the constructor, or use mulitple constructors. As for your last question use what ever you want, I rarely use the Integer class unless I need to use a static method from it or something along those lines. I prefer to use int. Hope that helped.

于 2012-11-03T13:33:14.647 に答える