2

I have scoured the Internet and have not found satisfying answers to my question. In a localized instance, if I have the code:

public static void main (String args[])
{
    int x;
    System.out.println(x);
}

...it will not compile, because the Java compiler will warn me that x should be initialized. I understand that.

My question is, will Java actually set aside 4 bytes of memory for the integer x? And if so, does it wipe out that block of memory? Does it define it as NULL (I don't think so because NULL and an int are incompatible types)? Or does the memory address of int x still retain the value last stored in memory, or does x just simply have "no value?"

I know that the above works in C, and that in C, it would just print out whatever is in that memory block. I want to know how Java handles this.

===EDIT===

Okay, okay. For some reason, my basic knowledge of programming had entirely exited my mind, as when a program does not compile, the question of memory allocation is irrelevant, as pointed out by both of the members who posted the answers. (Cue facepalm)

So what if I had the code:

public static void main (String args[])
{
    int x;
}

How is the integer x resolved in memory then?

3
  • if you put the x declaration outside the main (and make it static), it will compile and x will still be uninitialized. it should print 0. Commented Oct 16, 2014 at 19:06
  • 1
    How would you understand how a program would allocate a variable in memory if the program does not compile in the first place? Commented Oct 16, 2014 at 19:07
  • You must initialize it if it's a local variable if it's not a local variable then it will be automatically initialized to 0 if you don't specify. Commented Oct 16, 2014 at 19:07

2 Answers 2

4

It's not a warning; it's a compiler error. That code won't compile, so no bytecode is generated. The question of whether memory is set aside is irrelevant, because there is no program to run.

However, if the x variable is not local, then Java will assign a default value of 0.

Section 4.12.5 of the JLS states:

Every variable in a program must have a value before its value is used:

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

    • For type byte, the default value is zero, that is, the value of (byte)0.

    • For type short, the default value is zero, that is, the value of (short)0.

    • For type int, the default value is zero, that is, 0.

    • For type long, the default value is zero, that is, 0L.

    • For type float, the default value is positive zero, that is, 0.0f.

    • For type double, the default value is positive zero, that is, 0.0d.

    • For type char, the default value is the null character, that is, '\u0000'.

    • For type boolean, the default value is false.

    • For all reference types (§4.3), the default value is null.

  • Each method parameter (§8.4.1) is initialized to the corresponding argument value provided by the invoker of the method (§15.12).

  • Each constructor parameter (§8.8.1) is initialized to the corresponding argument value provided by a class instance creation expression (§15.9) or explicit constructor invocation (§8.8.7).

  • An exception parameter (§14.20) is initialized to the thrown object representing the exception (§11.3, §14.18).

  • A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16 (Definite Assignment)).

Sign up to request clarification or add additional context in comments.

3 Comments

But the question of memory being set aside is relevant, since if later in the method you do assign a value to that variable before accessing it, it will pass compilation.
It's worth pointing out: even if the code compiles, the JVM is not required to allocate memory. If a primitive variable is always local and there is a free register, an efficient JVM won't allocate anything at all.
@Eran Sure, the question about allocating memory for the int would be relevant if the program compiled.
2

Given that your questions are:

  1. [W]ill Java actually set aside 4 bytes of memory for the integer x?
  2. And if so, does it wipe out that block of memory?

The answer you seek is in JLS 4.12.3.8:

A local variable declaration statement may contain an expression which initializes the variable. The local variable with an initializing expression is not initialized, however, until the local variable declaration statement that declares it is executed. (The rules of definite assignment (§16 (Definite Assignment)) prevent the value of a local variable from being used before it has been initialized or otherwise assigned a value.) The local variable effectively ceases to exist when the execution of the block or for statement is complete.

So the answers are:

  1. Maybe. The value will be stored somewhere.
  2. Yes. No uninitialized values are allowed by the JLS.

Nothing happens to the local variable until it is assigned. It is impossible to use the value until it has been assigned, however, so you can never use an uninitialized value. At the point of initialization, some sort of storage location must be used (obviously). However, nothing about the JLS requires the JVM to allocate memory.

Whether memory is allocated would be determined at runtime based on the execution context, whether the value needed to be pushed onto a stack frame to make a method call, whether an unused CPU register was available, etc. A naive JVM implementation might just always allocate memory, but that would be slow, and as such I would expect most real-world JVMs to try something more optimized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.