Does the variable declaration have memory allocated?

Source: Internet
Author: User

Q1:
Does the int I have any memory allocated?
What about int *p?
Do you allocate memory in stack space or in a heap space?

?

A1:
int i;
int *p;
Both of these are allocated memory space in the stack area.
But P points to the address is a wild address, not directly to use
That is, the space that P points to is not assigned

?

Q2: The memory must be allocated when defining an array in Java, is this a wrong or a correct statement?

?

A2:
Arrays in Java you can see a special object that is declared when it is placed in the stack, and the allocated space is stored in the heap
Such as:
1. int[] arr = null;
An ARR array is declared in the stack at this point, but it is not allocated space, so it does not point to
2. int[] arr = new INT[6];
An ARR array is declared in the stack, and a space is created in the heap, when ARR has allocated memory
Look at the specific situation, the definition of the array should be a declaration, when needed to open up space can also

?

Q3:
Is there a local variable that is not initialized in Java that allocates memory?
Declaration of 2 Local variables
int i;
int j = 0;
Do both of these situations allocate memory within the stack?
If you are declaring 3 reference variables, what is the difference between them?
Object o;
Object B = null;
Object j = new Object ();
The first is whether the memory is allocated only within the stack, and no memory is allocated within the heap, or the heap and stack are not allocated memory;
The second only allocates memory within the stack? Or is the heap and stack allocated memory, or does null consume memory? Do you use heap or stack memory?

?

A3:
Post-code and compile-generated instructions to re-analyze
public static void Main (string[] args) {
String s;
int i;
}
———-Generated Instruction ——— –
public static void Main (java.lang.string[]);
Code:
0:return
}

——-Code ———
public static void Main (string[] args) {
String S=new string ("");
int i=0;
}

-Generated instruction ———-
public static void Main (java.lang.string[]);
Code:
0:new #16; Class Java/lang/string
3:dup
4:LDC #18; String
6:invokespecial #20; Method java/lang/string. "":( Ljava/lang/str
ng;) V
9:astore_1
10:iconst_0
11:istore_2
12:return
}
As can be seen from the above, although the two variables are not used, but the compiler did not dispose of them, and allocated space for it, if the non-initialized variables will be ignored by the compiler.
Finally, add that the above Li Shouyu said variables are ignored by the compiler, I personally think this is not the case, because the new object, that is, the active use of this class, will trigger the class of some static blocks and static variables initialization, so even if not used will be compiled, Loading the JDBC driver is an example.

Local variables are not initialized when they are compiled and should be placed in the stack if initialized.
O and B, are only allocated memory in the stack, there is no memory in the heap, only new after the memory allocated in the heap.

Does the variable declaration have memory allocated?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.