Java programmer interview book-Summary of fragmented knowledge 1

Source: Internet
Author: User

1. In the classloader method, the loadclass method and forname method have similar functions and are used to load the class, but their functions are different.

The loadclass method does not interpret the class during loading, so the class is not initialized. The forname method interprets and initializes the class.

 

2. in Java, characters only exist in one form, that is, Unicode (using numbers in character sets without any specific encoding is the only unified method ).

Therefore, the characters in Java are divided into two parts: JVM internal and OS file system. In the JVM, Unicode is used in a unified manner. When it is migrated to the outside, encoding conversion is required. During byte and character conversion, for example, the inputstreamreader and outputstreamwriter classes perform encoding conversion tasks.

3. the difference between a common initialization block and a static initialization block: A common initialization block is used to initialize Java class objects. When a Java object is created, the system always calls the initialization block defined in this class first.

Static initialization blocks are class-related, not object-related. They are executed in the class initialization phase, rather than during object creation. Therefore, static initialization blocks are always executed first than normal initialization blocks. The static initialization block of a class is executed only once.

During execution of common initialization blocks and static initialization blocks, not only will the initialization blocks of this class be executed, but will be traced back to Java. lang. object Class (if it contains static initialization blocks), first execute Java. lang. the initialization block of the object class, then executes the initialization block of its parent class, and then executes the initialization block of the class at a level. After this process, the initialization process of this class is completed.

 

Summary:

1. Normal initialization blocks are called only when class objects are created, while static initialization blocks are executed during class initialization.

2. Static initialization blocks are executed before normal initialization blocks. Static members are independent of any objects generated by classes.

3. The static initialization block is executed only once, and the normal initialization block is executed every time the object of this class is generated.

4. The execution of the initialization block starts from the highest level and goes down layer by layer.

5. The initialization block is similar to the constructor's function, which is executed before the constructor executes. They are used to initialize objects.

6. The value of the initialization block is that it is used to store the same code in the constructor of multiple different parameters, thus improving code reusability and application maintainability.

 

The test code is as follows:

 

1 package classmate;
2
3 Public class team {
4 public static void main (string [] ARGs ){
5
6 person P = new person ();
7 system. Out. println ("------------------------------");
8 person p1 = new person ();
9}
10}
11
12 class person {
13 // Constructor
14 public person (){
15 system. Out. println ("person constructor ...");
16}
17
18 // initialize the code block
19 {
20 system. Out. println ("person initialization block ...");
21}
22
23 // static code block
24 static {
25 system. Out. println ("person static block ...");
26}
27}

 

Output result:

 

Person static block...
Person initialization block...
Person constructor...
------------------------------
Person initialization block...
Person constructor...

 

 

4. in Java, j = J ++ and J = ++ J

Java uses the intermediate cache variable mechanism, and J = J ++ is equivalent

 

Temp = J;
J = J + 1;
J = temp;

 

Similarly, j = ++ J is equivalent

 

J = J + 1;
Temp = J;
J = temp;

 

 

Test code:

 

1 import java. util .*;
2
3 Public class test {
4 public static void main (string [] ARGs ){
5 Int J = 0;
6 For (INT I = 0; I <100; I ++ ){
7 j = J ++;
8}
9 system. Out. println (j );
10}
11}

 

 

5. tostring () method

Integer, float, double, Boolean, and other classes all have the tostring () method. The usage is as follows:

 

1 public class test {
2 public static void main (string [] ARGs ){
3 int I1 = 10;
4 float F1 = 3.14f;
5 double d1 = 3.1415926;
6 integer I1 = new INTEGER (I1 );
7 float F1 = new float (F1 );
8 double d1 = new double (D1 );
9 string Si1 = i1.tostring ();
10 string sf1 = f1.tostring ();
11 string sd1 = d1.tostring ();
12 system. Out. println ("Si1" + Si1 );
13 system. Out. println ("sf1" + sf1 );
14 system. Out. println ("sd1" + sd1 );
15}
16}

 

 

In addition, there are two mappings when converting a numeric variable to a numeric variable: one is the corresponding ASCII code and the other is the conversion relationship. The getnumericvalue (char ch) method of character is used. For example, '1' is converted to 1.

6. Meaning and usage of assert statements

Theoretically, the assertion method can be used to prove the correctness of the program. However, this is a very complicated task, and there is not much practical significance yet.

In implementation, assertion is a statement in the program. It checks a Boolean expression. A correct program must ensure that the value of this Boolean expression is true. If this value is false, if the program is in an incorrect state, the system will give a warning or exit.

Generally, assertion is used to ensure the most basic and critical correctness of the program. The assertion check is usually enabled during development and testing. To improve performance, the assertion check is usually disabled after the software is released.

The syntax of the assert keyword is simple and has two usage methods:

1. Assert <Boolean expression>

If <Boolean expression> is true, the program continues to execute.

If it is false, the program throws an assertionerror and terminates the execution.

2. Assert <Boolean expression >:< error message expression>

If <Boolean expression> is true, the program continues to execute.

If it is false, the program throws a java. Lang. assertionerror and enters the <error message expression>.

Compile: javac-source 1.4 test. Java

Run: Java-ea Test

 

7. The reason why main () must be of the public static void type

When a Java program is running, the main () function is first run. The main () function is executed by the vm jvm, so the main () function must be of the Public type.

During the call, the virtual machine does not instantiate the object of this class, but directly calls the object through the class name, so the limit is static.

In addition, the JVM restricts that the main () method cannot return values. Therefore, the return value type is void.

In addition, the main () method of the Java program must contain a string parameter. Otherwise, nosuchmethoderror is thrown.

 

8 .(? :) Involved type conversion

False? 1.0: 2

The output result of this statement is 2.0 instead of 2.

Char x = 'X ';

False? 10: x

At this time, the output result is X, instead of the int value 120. When one of the last two expressions is a constant expression, and this constant expression can be expressed by another type T, the output result is of the T type. If it cannot be expressed as t, the output result must be automatically converted.

For example, if the above formula is false? 10.0: X, the output result is 120.0.

9. Shift Operator '> ''<'

The parameter on the right of the shift operator must first perform the modulo 32 operation and then shift its binary value. For example

Int num = 32;

Num> 32;

That is, 0 digits are shifted to the right.

Num> 33;

Shift 1 to the right

10. Difference Between throw and throws keywords

Throw is used to throw an exception in the method body. Syntax format: Throw exception object.

Throws is used to declare what exceptions A method may throw. After the method name, the syntax format is: throws exception Type 1, exception type 2... exception type N.

 

Related Article

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.