Java programming BASICS (1)

Source: Internet
Author: User

I. Local variables and member variables

1. Local variables generally refer to variables defined within the method body, and their scopes are valid within the method block. Local variables must be initialized before they can be used. Otherwise, the program cannot be compiled.

2. A member variable is a variable defined in the class, that is, an attribute. Its scope is valid throughout the class. When defining a member variable, you can choose not to specify the initial value. The system can initialize the variable based on the default principle. The initialization principles are as follows:

Default initial values of member variables
Member variable type Default Value
Byte 0
Short 0
Int 0
Long 0l
Boolean False
Float 0.0f
Double 0.0d
Char '\ U000000'
Other types (reference type) Null
   

3. Differences between the two:

(1) Public, protected, Private, static, and other modifiers can be used to modify member variables, but not local variables. Both can be modified using final.

(2) member variables are stored in heap memory, and local variables are stored in stack memory.

(3) The member variables are valid in the entire class and the local variables are valid in the method body. They are invisible outside the method body.

(4) member variables can be initialized by default, and local variables must be displayed as initialization.

2. How does the "+" operator implement string connection in Java.

Java does not support Operator overloading, but some special operators are provided in Java, which have the feature of overloading. For example, when "+" is used to connect strings, the operand can be two strings, or one string and one other type. See the following example:

String A = "A"; string B = "B"; string c = a + B; string d = C + 1; // compile the above Code before decompiling, the result is as follows: string a = "A"; string B = "B"; string c = (New stringbuilder (string. valueof ())). append (B ). tostring (); string d = (New stringbuilder (string. valueof (c ))). append (1 ). tostring ();

From the code above, it is not difficult to find that "+" actually uses the stringbuilder class and its append () method to implement string connection.

The string class represents a fixed-size string. Once declared and defined, the content and size cannot be changed. All string operation methods provided in the string class Create a New String object and return the operation results.

To consider efficiency, the stringbuilder class is used at the underlying Java layer. This class represents a variable-Size String. The append () and insert () Methods of the stringbuilder class can be modified on the basis of the original string. The append () and insert () methods are overloaded multiple times and can receive multiple types of parameters.

3. What are the differences between values transmitted between variables.

1. storage mechanism.

Simple type variables are stored directly in the stack memory to open up the storage space.

A reference type variable consists of a reference space and a bucket. The reference space is in the stack memory, the bucket is in the heap memory, and the bucket is responsible for storing variable values, the reference space is used to store the first address of the bucket. The reference variable stores the address value. The address value can be used to define the storage location and modify the storage information.

2. variable transmission.

When a value is assigned between a variable and a variable, both the referenced type variable and the simple variable belong to value transfer. The difference is that the simple variable passes the content itself, while the referenced variable passes the reference address.

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.