tags:

views:

134

answers:

5

Is it null for Object type?

class C {
int i;
String s;
public C() {}
}

will "s" be always null ?

What about simple types as int? What will that be? Zero or an arbitrary value?

What about local variables in methods?

public void meth() {
int i;
}

what is the unitialized value of i ?


<Relying on such default values, however, is generally considered bad programming style>

Ok, what do you suggest we do?

class A { String s=""; int i=0; }

OR

class A { String s; int i; public A() { // default constr s=""; i=0; } }

Which is better and why?

+9  A: 

From suns java tutorial

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type Default Value (for fields) byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char '\u0000' String (or any object) null boolean false

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

stimpie
+3  A: 

For member variables: The default value for String is null. The default value for primitives is 0 (or 0.0 for floating point values).

For local variables: You must explicitly initialise a local variable before using it.

As to the second part of your question: You can always say String s = ""; in the member variable definition, or s = ""; in the constructor. Then you know it will have a non-null value. (Also, in your setter you'd need to ensure that someone doesn't try and set it back to null.)

Ash
or false for booleans.
Thilo
or null for objects
Ash
<<Also, in your setter you'd need to ensure that someone doesn't try and set it back to null>>Tons and tons of stupid work. Of course I can do that. But that's not what I'm expecting of programming style. Suppose you have to do that for 1000 fields.
EugeneP
What's with the attitude? That's all you've got in Java without relying on either a third-party library (as per nd's Google Collections answer) or writing a "check for null" method yourself (or using AOP or byte code manipulation or some other nefarious manipulation).
Ash
EugeneP
+2  A: 

Fields: Objects default to null; ints, longs and shorts to 0; Strings to null; booleans to false. It's all here.

The compiler will force you to initialise variables declared in methods, local variables, yourself.

Joel
+4  A: 

Primitive fields are initialized to 0 / false. Objects are initialized to null . But frankly, you could have tried that one..

Bozho
.. although trying only makes sense if you're sure, field initialization is defined and reliable. Sure, we know, it is, and that the compiler won't let you go where it isn't (local variables). But if you're not sure and don't know where to look, then it's worth both a question and a good answer (BTW, +1).
Andreas_D
->frankly, you could have tried that one.It may be A NON REPEATABLE TEST. You can get 0 a thousand times. And then, get 4 billions as memory got refreshed.
EugeneP
+1  A: 

As for the setter-method question: The whole point of setters is that they can check if the object passed conforms to the requirements of the class. e.g.

public void setS(String s) {
  if (s == null)
     throw new IllegalArgumentException("S must not be null");
  this.s = s;
}

Or, with Google Collections/Google Guava:

public void setS(String s) {
  this.s = Preconditions.checkNotNull(s, "S must not be null");
}

Of course, you can define arbitrary constraints, e.g.:

/**
 * Sets the foo. Legal foo strings must have a length of exactly 3 characters.
 */
public void setFoo(String foo) {
  if (foo == null)
     throw new IllegalArgumentException("Foo must not be null");
  if (foo.length() != 3)
     throw new IllegalArgumentException("Foo must have exactly 3 characters");
  ...

Of course in such a case you should always state the correct range of values for your properties in the JavaDoc of the setter and/or of the class.

nd
well, preconditions.checknotnull looks very Pretty !!why is it not in standart java library? that's what I'm expecting from Java.
EugeneP