Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why fields in the Java class can not be assigned an initial value

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces why the field in the Java class can not be given the initial value, the article is very detailed, has a certain reference value, interested friends must read it!

The reason why fields in the Java class can not be assigned an initial value

The Java virtual machine allocates memory to instance objects of the class, and after allocating memory, initializes all memory space (except the object header) to zero. This ensures that during the definition of the class, no initial value is assigned to the field, and the instance object can also have an initial value.

The following is the initial value field of each field in the Java class default initial value object null numeric (byte,short,int,long,float,double) 0 or 0.0booleanfalsechar\ u0000 (the output is null) class Solution {private static char c; private static int i; private static long l; private static float f; private static double d; private static String s; private static boolean bl; private static byte b Public static void main (String [] args) {System.out.println (c = ='\ u0000'); System.out.print (c); System.out.println (I); System.out.println (l); System.out.println (f); System.out.println (d); System.out.println (s); System.out.println (bl) System.out.println (b);}} / * initialization of class attributes in the output true00000.00.0nullfalse*/Java

We know that a class (class) must go through the process of loading and connection initialization in order to be used. The following is a brief description of these three phases, followed by a simple example to illustrate the initialization process of classes in java.

In the loading phase, the class loader (Bootstrap ClassLoader or user-defined ClassLoader) loads the compiled class file into memory and creates a class-related Class object that encapsulates the type information of the class we want to use.

The connection phase can be divided into three sub-steps: verification, preparation, and parsing.

Validation is to ensure that the java type data format is correct and suitable for JVM use.

In the preparation phase, JVM allocates memory space for static variables and sets default values. Note that default values are set here, for example, int variables are assigned a default value of 0. At this stage, JVM may also allocate memory for some data structures to improve the performance of running programs, such as method tables.

The parsing process is to find symbolic references to classes, interfaces, fields, and methods in the constant pool of types, and replace these symbolic references with direct references. This phase can be deferred until after initialization, when a symbol reference is actually used during the running of the program.

The class performs initialization when it is "actively used" for the first time, giving the class (static) variable the correct initial value. In Java code, a correct initial value is given through a class variable initialization statement or a static initialization block.

And the active use we are talking about here includes

1. Create an instance of the class

two。 Call the static method of the class

3. Use the non-constant static fields of the class

4. Call some reflection methods in Java API

5. Initialize a subclass of a class

6. When a class containing the main () method starts

Initializing a class involves two steps

1. If there is a direct parent class in the class, and the direct parent class has not been initialized, initialize its direct parent class first

2. If there is an initialization method in the class, execute this method

Note: initializing an interface does not require initializing its parent interface.

Why the final variable in Java must be initialized before it is used

The variable modified by final means that the assignment cannot be changed after the assignment, and the default value assigned by the system is also an assignment, so the system does not assign the default value.

If the final variable is not assigned at definition time or in the constructor, the value of the final variable in the generated object is unknown (the compiler will also report an error directly), so it must be initialized.

If you use static final to modify variables at the same time, the variables must be initialized at the time of definition. Because the static variable belongs to the class, it is assigned a default value by the system before the constructor is called.

If it is not initialized at the time of definition, it cannot be initialized in the constructor and the system will not assign a default value. It is meaningless for the variable to be defined.

The above is all the content of the article "Why fields in the Java class can not be assigned initial values". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report