Java Assignment and initialization __java

Source: Internet
Author: User

Java assignment and initialization includes a number of areas that are easily overlooked, as summarized in the following categories:

1. Associated with assignment:

Basic types of assignment operations need to pay attention to the assignment when the type conversion problem, when it can be automatically converted, when the need to force type conversion needs to be clarified.

First, remember the two conventions:

(1) All literal integers are of type int.

(2) All literal floating-point numbers are double, so declaring a float is a float, you must do a type conversion, or you can add f when declaring. Float ads=3.13f.

First: An expression that occupies an int or a shorter length is always the result of an int, that is, the result of a two byte is added together, and the result is int. For example, the following code will compile incorrectly:

BYTE b=3;

BYTE c=8;

BYTE D=b+c;

The result of the b+c is a value of int, and D is a byte value, the conversion to the range is relatively small conversion, the compiler will error, if the display of coercion type conversion, it will not be a problem.

Conversions from a small to a large range are not required to display casts, including from long to float and double.

When the compiler knows that it is too large to be assigned to a variable by the literal value that the variable saves, a compilation error will be obtained.

BYTE a=128;

Note that there are several operators in the special place of type conversions.

+=,-=,*=,/= These operators all use implicit coercion type conversions.

BYTE b=3;

b+=7;//is right at this time.

2 and initialization Related:

(1) For instance variables, they are initialized to the default values. For an instance variable of an object reference type, its initial value is null.

(2) The array object is special, the array is an object, and the array element is always given a default value, regardless of where the array is declared and instantiated, even if the array is a local variable. If an array element is a reference to an object, the default value is NULL, and if the array element is a base type, the default value is the default value for the base type.

(3) A local variable is not a default value, whether it is a basic type or a reference to an object, so any local variable must be initialized explicitly before it is used, or a compilation error will occur.

(4) The references to objects are assigned to each other, and the object itself is not modified (except for a string object).

(5) For string objects, keep in mind that the value of a string object cannot be changed, so the following example may seem contradictory:
String str= "Zhanglei";

String Str2=str;

Str=str+ ", how to Are you?"

The detailed explanation of the statement is as follows:

First create a String object "Zhanglei" in the heap, and then let the object refer to Str to point to the object, and the second statement assigns the object reference str to the STR2 object reference. The third statement will object "Zhanglei" and objects "how to Are you?" Make a connection operation, generate a new object, and then assign the object's reference to Str. Therefore, if you print the contents of str2 or "Zhanglei" at this time.

(6) Initialization block:
Initialization blocks are divided into two types: static initialization block and instance initialization block;

Static initialization blocks are used to initialize static property members in a class, and instance initialization blocks are used to initialize instance property members.

The first time the class is loaded, a static initialization block is run, and a new instance is run, and the instance initialization block runs once. The instance initialization block runs after all superclass constructors have been run. Static initialization blocks are run before all superclass constructors. When multiple initialization blocks coexist, the order in which they are run is the order in which they appear in the class file.

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.