1

let have an example.

public class test { public static void main(String args[]) { int a=5,b=4; int c=a+b; int d=9; System.out.println("ANSWER PLEASE.."); } } 

Now when we execute this code what os does?

It first create a variable named a and alocates a memory address similar things for b and c.

now what happen to d. os creates a new memory address or it just reffer to the address of c as the value is same.

1
  • 1
    Comment rather than an answer because I'm just guessing: In this specific example the compiler (javac?, version?) can reason that all variables will remain unread and therefore erase them. The resulting bytecode would be exactly as if you never declared any ints at all. I look forward to the answer. Commented Aug 1, 2012 at 6:50

2 Answers 2

3

First of all, the compiler doesn't do much. It basically translates it into class-files / bytecode. In the bytecode there is a number called "max locals" which tells how many local variables are required to run the method.

The JVM on the other hand, which reads this information and runs the code makes sure memory is allocated on the stack frame to fit the required variables. How much it asks for is highly implementation dependent, and it may very well optimize the whole thing, and allocate fewer bytes than what's indicated by the code.

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

7 Comments

If we write it in C then does the c compiler allocates some memory for each int?
Depends on the compiler (and probably the optimization level you set). That's quite easy to find out using a disassembler though.
Isn't the stack space for threads pre-allocated (at the latest when the stack is created), and does not grow after that?
@Thilo, that's probably true actually... was a while ago I read up on this. Updated.
@Thilo if we print the value of all variables what happens to d it will create a new memory address or it just refer to c?
|
0

what happen to d. os creates a new memory address or it just reffer to the address of c as the value is same.

  • This is tagged Java. So let's keep the OS out of it. Everything happens in memory managed by the JVM, which is allocated in big chunks in advance.
  • You are talking about primitive data types. Those are easy, as they are stored by value, not as references to objects living elsewhere.
  • You are talking about local variables. Those are allocated on the running thread's call stack (not in heap memory). Since they are primitive here, too, this does not involve the heap at all.
  • In your case, there is memory allocated (on the stack), for the four integers. Each of them contains the value assigned to it, not a reference. Even if the same value is assigned to all of them, they exist separately. The memory is "freed" (not really freed, but no longer used by the thread) when the method returns.
  • If those were not integers, but Objects, you could "share pointers" (to objects on the heap), but integers are stored by value (four bytes each).

2 Comments

thank you. now i can understand. primitive data type are stored by value and objets such as String types are stored as object reference. right?
Yes. For a String, you would have an object on the heap in addition to the variable storing the pointer to that object. You can have all four String variables pointing to the same object on the heap.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.