-4
static _____ defaultValue;

public static void main(String[] args){

    System.out.println(defaultValue);

}

Options:

A. One

B. Two

C. Three

D. Four

The answer in my book:

C. Since defaultValue is an instance variable, it is automatically initialized to the corresponding value for that type. For double the default value is 0.0. By contrast, it is 0 for int, long, and short. Therefore Option C is correct.

Now my question is defaultValue is a static variable how can it be an instance variable? It should not have any initial value. Please correct me as I know I am wrong somewhere. I am studying for OCA java certification exam and I found this question in a book by Scott Selikoff and Jeanne Boyarsky.

Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31
Arpan Ghoshal
  • 19
  • 1
  • 3
  • 2
    an instance variable as well `static` variables that are declared at the class level will always have a default value set. There is no documentation stating only instance variables should have a default value. – Ousmane D. Dec 30 '17 at 08:37
  • 2
    It is not an instance variable, but that doesn't mean that it doesn't have any initial value. – khelwood Dec 30 '17 at 08:38

4 Answers4

2

Variables that are declared as a class member variable, be static or instance, and not assigned a value will be assigned their default value. Note defaultValue is a static class member variable.

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. Primitive Data Types

The default values that will be assigned are as follows.

Default values

Of the four primitive types given (double, int, long and short), each will be assigned with 0, however, the output depends on how the primitive type is converted to a String. The way in which a String is generated from a primitive is almost identical, just with distinct methods for each primitive type. Let's examine the int primitive type:

By calling System.out.println(int i) you effectively call System.out.print(int i) with a line.separator used to terminate the line, as described in the PrintStream documentation. The System.out.print(int i) method takes the string produced by String.valueOf(int i) which is translated into bytes according to the platform's default character encoding. Finally, the String documentation shows that String.valueOf(int i) acts as a wrapper for Integer.toString(int i)

Hence, the String value for a primitive type is generated found by invoking the toString() method with the primitive value as a parameter on the wrapper Class corresponding to the primitive type (Integer for int, Short for short, etc).

How do these methods convert their default value to a String? All of the primitive values in the question (double, int, long and short) are assigned a default value of 0 (0.0d for double). The toString(primitive_type value) for each of the wrapper classes state that:

Double :

if m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0". toString(double d)

Integer :

Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method. toString(int i)

If the magnitude is zero, it is represented by a single zero character '0' ('\u0030') toString(int i, int radix)

Long :

Returns a String object representing the specified long. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and the radix 10 were given as arguments to the toString(long, int) method. toString(long i)

If the magnitude is zero, it is represented by a single zero character '0' ('\u0030') toString(long i, int radix)

Short :

Returns a new String object representing the specified short. The radix is assumed to be 10. toString(short s) Note that inspecting the code shows that this method is simply a wrapper for return Integer.toString((int)s, 10);

Hence, int, long and short will all output the character '0' when the value is 0 whereas the double value will print the characters '0.0'.

Community
  • 1
  • 1
Zachary
  • 1,693
  • 1
  • 9
  • 13
0

The answer in your book is incorrectly motivated, as you suspected.

Since defaultValue is an instance variable

It is a static variable. If it was an instance variable, this code wouldn't even compile.

it is automatically initialized to the corresponding value for that type.

Correct, but this applies to both instance and static variables.

For double the default value is 0.0. By contrast, it is 0 for int, long, and short.

There is no 'by contrast' about it. The default values are really zero in all these cases. System.out.println() will print 0.0 in the case of double and float, but what's in the variable is indistuinguishable from zero.

Therefore Option C is correct.

There is no 'therefore' about it. There are several non sequiturs here, as well as several errors of fact.

I found this question in a book by Scott Selikoff and Jeanne Boyarsky.

Good to know, so we can avoid it.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

What is written in the book is correct except the line where it is says "Since defaultValue is an instance variable". As defaultValue is a static variable it cannot be a instance variable but it is off course a class variable. As it is a class variable it is automatically initialized to the corresponding value for that type.

Arpan Ghoshal
  • 19
  • 1
  • 3
-1

C. Since defaultValue is an instance variable - this statement is wrong, because defaultValue is not an instance variable - it's a class variable as its declaration contains static keyword. Static variables are associated with class enclosing them. Another worth mentioning fact is that the static variables are initialized before instance variables (instance variables doesn't contain static as their modifier - every instance has its own variable and any changes in that variable doesn't affect other instances' variables).

Now my question is defaultValue is a static variable how can it be an instance variable? It should not have any initial value. - this statement is wrong. It is not an instance variable, it's a class variable. Anyway it will have initial value, because only local variables are not initialized to their default value (local variables are those, whose scope is method they're declared in. For example, if you declared defaultValue in the scope of main() method or any other method, it wouldn't be initialized to its default value.

When program starts execution and you haven't assigned any specific value to your variables, variables containing primitive types are initialized to their default values (you can see the list of these default values at the link below) and those containing objects are initialized to null.

Here, at "Default Values" paragraph, is a list of values that are assigned to primitive types: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
  • "If it is static it just means that this member will be initialized to its initial value before the instance variables." No, that's *far* from the only meaning of static. – Jon Skeet Dec 30 '17 at 08:48
  • @JonSkeet Of course you're right. It was not what I meant, thank you for pointing it out. – Przemysław Moskal Dec 30 '17 at 08:51
  • Could you give me a reason of a downvote here? – Przemysław Moskal Dec 30 '17 at 09:42
  • 1
    Well I didn't downvote, but I still don't think it's actually a good answer. – Jon Skeet Dec 30 '17 at 09:43
  • I didn't point my comment exactly to you, but it's good to know I still need to improve my answer. – Przemysław Moskal Dec 30 '17 at 10:05
  • @JonSkeet Could you have a look at my answer as I tried to improve it a bit? I would like to know if I understand that topic correctly and if my answer is correct now. I know that this question is closed now, but it would be really helpful if you provide some feedback anyway. – Przemysław Moskal Dec 30 '17 at 19:16
  • Well I don't like referring to static fields as "shared between instances" - it's more that they're associated with the type rather than any instance of the type. (The field can be used even if there are *no* instances.) Looks okay apart from that. – Jon Skeet Dec 30 '17 at 19:35