Summarize some key and confusing knowledge points in Java

Source: Internet
Author: User

I. Switch
1. it can accept four data types: Char, byte, short, int
2. Default can be placed anywhere in the switch, but it will be executed only when the given condition does not match.
3. If the 'case' and 'default' statement is to be jumped out after execution, break must be used. Otherwise, the statement will continue to be executed (if the 'case' statement is met, it will directly go into execution)
Instance 1:
1. Int I = 1, j = 0
3. Switch (I ){
4. Case 2:
5. J + = 6;
7. Case 4:
8. J + = 1;
10. Default:
11. J + = 2;
13. Case 0:
14. J + = 4;
15 .}

What is the value of J at line 16?
A.0
B .1
C.2
D.4
E.6

Instance 2:
1. Switch (I ){
2. Default:
3. system. Out. printin ("hello ");
4 .}

What is the acceptable type for the variable I?
A. byte
B. Long
C. Float
D. Double
E. Object
F. A and B
G. C and D

Ii. String and stringbuffer
A string defines a String constant, and its struct does not change once defined as follows:
String S = "ABC ";
S = S. substring (2); // A New String object is generated.
After the preceding two statements are executed, "two" strings are generated in the memory, one is "ABC", and the other is "AB" (note that s no longer points to "ABC ")
Stringbuffer defines a string variable, and its variable value can be changed as follows:
Stringbuffer S1 = new stringbuffer ("ABC ");
S1 = s1.substring (2 );
After the preceding two statements are executed, only "one" String object is generated in the memory: "AB" pointed by S ";
Instance 1:
1. Public class Foo {
2. Public static void main (string [] ARGs ){
3. stringbuffer A = new stringbuffer ("");
4. stringbuffer B = new stringbuffer ("B ");
5. Operate (A, B );
6. system. Out. printin {A + "," + B };
7 .}
8. Static void operate (stringbuffer X, stringbuffer y ){
9. x. append {y };
10. Y = X;
11 .)
12 .}
What is the output?
Ans:

Instance 2:
1. Public class test {
2. Public static void stringreplace (string text ){
3. Text = text. Replace ('J', 'I ');
4 .}
5.
6. Public static void bufferreplace (stringbuffer text ){
7. Text = text. append ("C ")
8 .}
9.
10. Public static void main (string ARGs []) {
11. String textstring = new string ("Java ");
12. stringbuffer textbuffer = new stringbuffer ("Java ");
13.
14. stringreplace (textstring );
15. bufferreplace (textbuffer );
16.
17. system. Out. printin (textstring + textbuffer );
18 .}
19 .}

What is the output?
Ans:

3. String S = new string ("XYZ ");
This statement generates two string objects:
One method is generated during compilation and stored in the constant pool.
One is generated at runtime using the new method and stored in heap memory.
However, only one object is generated in the new mode during running.

4. Parameters in Java can only be transferred by worker, and the copy of worker is passed.
If the basic type is used, a copy of the basic type is passed.
If it is a reference type, a copy of the reference itself is passed.
See instance 2

5. Conditions for method overloading and overwriting
Meet the overload condition: 1. In the same class
2. There are multiple methods with the same name,
3. Different method parameters (different numbers of parameters or different types of parameters)
Instance:
1. Public class methodover {
2. Public void setvar (int A, int B, float c ){
3 .}
4 .}

Which two overload the setvar method? (Choose two)

A. Private void setvar (int A, float C, int B ){}
B. Protected void setvar (int A, int B, float c ){}
C. Public int setvar (int A, float C, int B) (return ;)
D. Public int setvar (int A, int B, float C) (return ;)
E. Protected float setvar (int A, int B, float C) (return C ;)

Covered conditions: 1. In inheritance
2. The method name in the subclass is the same as that in the parent class.
3. The method parameters in the subclass are the same as those in the parent class.
4. The return type of the method in the subclass is the same as that of the parent class.
5. The subclass method cannot throw more exceptions than the parent class.
6. The access range of the subclass method is greater than or equal to that of the parent class.
Override it is worth noting that if there is a method name in the subclass that is the same as the parent class, but the parameters are different, it is not called overwrite, so it is not subject to overwriting conditions (note that this method can exist)
Instance:
1. Class baseclass {
2. Private float x = 1.0f;
3. Protected float getvar () (return X ;)
4 .}
5. Class subclass extends baseclass (
6. Private float x = 2.0f;
7. // insert code here
8 .)

Which two are valid examples of method overriding? (Choose two)
A. Float getvar () {return X ;}
B. Public float getvar () {return X ;}
C. Float double getvar () {return X ;}
D. Protected float getvar () {return X ;}
E. Public float getvar (float f) {return F ;}

Problem points:100 Replies:149

Show all repliesShow star repliesShow landlord reply

Modify
Delete
Report
Reference
Reply


Add as a friend
Send private message
Online Chat
  • Myjavaroad
  • Focus on nothing
  • Level:
  • Available levels:Poor farmers
  • Total technical points:3
  • Total technical score ranking:294445
Posted on:2009-03-27 14:54:371Floor score:0
After adjusting for half a day, it is still not ideal. If it is adjusted before tomorrow, it will change as soon as it is released.

6. Knowledge about variable initialization in Java:6-1. The initialization sequence is divided into three steps:
1. initialize static variables and static blocks during class loading, first parent class and then subclass
2. When a new object is generated during running, allocate space for the object and initialize the instance variable. The first parent class and then subclass
3. when calling a constructor, first execute the constructor of the parent class, and then execute the constructor of the Child class. The specific process is when the constructor of the Child class is called, in the first line, the constructor of the parent class is called (explicit or implicit)

6-2. Initialization of various types of variables:
Application Type: NULL
Basic Type: Boolean: false
CHAR:/u0000
Byte: 0
Short: 0
INT: 0
Long: 0
Float: 0.0
Double: 0.0

6-3. array Initialization

When we generate an array of storage objects, we actually generate an array that stores references. After the array is created, each reference is automatically set to
. This value is expressed by the keyword null. When Java sees the null value, it regards this reference as "not pointing to any object ". Before using any reference,
You must first assign an object to it. If you use a reference and its value is null, an error will occur during the execution period.
Array initialization starts when space is allocated. Initialization rules are applied. The basic types are initialized according to the 6-2 rules. All application types are initialized to null.
Instance 1:
Int Index = 1;
Int [] Foo = new int [3];
Int bar = Foo [Index];
Int Baz = BAR + index;

What is the result?
A. Baz has the value of 0
B. Baz has the value of 1
C. Baz has the value of 2
D. An exception is thrown
E. The Code will not compile

Instance 2:
1. String Foo = "blue ";
2. boolean [] bar = new Boolean [1];
3. If (BAR [0]) {
4. Foo = "green ";
5 .}

What is the result?

A. Foo has the value of ""
B. Foo has the value of null
C. Foo has the value of "Blue"
D. Foo has the value of "green"
E. An exception is thrown
F. The Code will not compile

6-4. All instance variables in Java are initialized by default. All method variables are initialized by the method itself, and the variables in the method must be initialized before they can be applied.
Example:
Class Parent {
// Static variables
Public static string p_staticfield = "parent class -- static variable ";
// Variable
Public String p_field = "parent class -- variable ";

// Static initialization Block
Static {
System. Out. println (p_staticfield );
System. Out. println ("parent class -- Static initialization block ");
}

// Initialize the block
{
System. Out. println (p_field );
System. Out. println ("parent class -- initialization block ");
}

// Constructor
Public parent (){
System. Out. println ("parent class -- constructor ");
}
}

Public class subclass extends parent {
// Static variables
Public static string s_staticfield = "subclass -- static variable ";
// Variable
Public String s_field = "subclass -- variable ";
// Static initialization Block
Static {
System. Out. println (s_staticfield );
System. Out. println ("subclass -- Static initialization block ");
}
// Initialize the block
{
System. Out. println (s_field );
System. Out. println ("subclass -- initialization block ");
}

// Constructor
Public subclass (){
System. Out. println ("subclass -- constructor ");
}

// Program entry
Public static void main (string [] ARGs ){
New subclass ();
}
}

VII. constructor in Java
1. constructor cannot be inherited
2. Each class has at least one constructor, which is not defined by itself. The Compiler also assigns a default constructor without parameters.
3. the constructor of the subclass will certainly call the constructor of the parent class. The constructor called by Super () or explicitly or implicitly must exist;
If no explicit call is made, the compiler automatically adds the implicit call of super () to the first line of the constructor of the subclass, in this case, the parent class must have constructors without parameters (if the parent class is set
If the constructor has parameters, an error will be reported during compilation)
Example:
Class super1 {
Public int I = 0;
Public super1 (string text ){
I = 1;
}
}
Public class sub1 extends super1 {
Public sub1 (string text ){
// Super (text );
I = 2;
// Implicit superconstruct super1 () is undefined. Another structure must be explicitly cited
}
Public static void main (string ARGs []) {
Sub1 sub2 = new sub1 ("hello ");
System. Out. println (sub2. I );
}
}

8. Exception Handling in Java
1. exception in Java is a sub-run exception or non-runtime exception. The runtime exception is captured and processed by the runtime system (compilation is normal). Non-runtime exceptions must be handled (throw or captured)

2. Must catch be followed after try {} in the exception mechanism?
* Not necessarily, but it must be followed by finally. That is, catch and finally.
* Must catch be followed after try {} in the exception mechanism?
* Not necessarily, but it must be followed by finally. That is, catch and finally.
* Try {
*} Finally {}
* This is okay, and it doesn't make sense, because it ensures that the code in finally will be executed even if an exception occurs.
* Sometimes, this is very useful.
* For example, it can be used to release some resources occupied by the caller and then let the caller handle exceptions.
3. Finally in the exception will be executed. Even if a return statement exists in a method, it will be returned only after Exception Processing.
4. if an exception is thrown, the Child class can be first added to the parent class. If the child class is captured, the parent class will not be captured;
However, the parent class cannot be used as a subclass, which may cause compilation errors.
5. After exception handling, the program continues to execute
Instance:
/*
* If an exception is thrown during non-running, the catch block is used to capture and process the exception.
*/
Import java. Io. ioexception;
Public class exceptiontest {
// Public static void methoda (){
Public static void methoda () throws ioexception {
// Throw new nullpointerexception ();
// Try {
Throw new ioexception ();
// System. Out. println ("method exit ");
//} Catch (ioexception e ){}
// Finally {}
}
Public static void main (string [] ARGs ){
Try {
Methoda ();
// Throw new ioexception ();
} Catch (ioexception e) {system. Out. println ("caught1 ioexception ");
} Catch (nullpointerexception e ){
System. Out. println ("caught1 nullpointerexception ");
} Catch (exception e ){
System. Out. println ("caught exception ");
}

System. Out. println ("main exit ");
}
}

What is the output?
Ans:

9. bitwise AND logical operations
Bitwise operators (&, |) must be calculated on both sides.
If the left side of the operator (&, |) is true, it is not on the right side of the calculation.

Instance:
1. Public class test {
2. Private Static Int J = 0;
4. Private Static Boolean methodb (int K ){
5. J + = K;
6. Return true;
7 .}
9. Public static void methoda (int I ){
10. boolean B:
11. B = I <10 | methodb (4 );
12. B = I <10 | methodb (8 );
15. Public static void main (string ARGs []) {
16. methoda (0 );
17. system. Out. println (j );
18 .}
19}

What is the result?
A. The program prints "0"
B. The program prints "4"
C. The program prints "8"
D. The program prints "12"
E. The Code does not complete


Modify
Delete
Report
Reference
Reply


Add as a friend
Send private message
Online Chat
  • Myjavaroad
  • Focus on nothing
  • Level:
  • Available levels:Poor farmers
  • Total technical points:3
  • Total technical score ranking:294445
Posted on:2009-03-27 15:45:032Floor score:0
10. For (;) Meaning
It is equivalent to while (true). I don't know why Java is going to create this strange and confusing thing?

11. Equals, =
Equals checks whether the content of two objects is equal.
= Compare whether two references direct to the storage feature of the same object string will affect the above judgment rules:

The objects generated by string through "" are stored in the constant pool. A very important feature of the constant pool is that they can be shared, such as string S = "X ";
Before placing "X" in the constant pool, the JVM checks whether the same object exists in the constant pool. If yes, the JVM directly points the reference to an existing object, no space is allocated for "X". The advantage is that space is saved.
Instance:
String S1 = "ABC ";
String S2 = "ABC ";
The following results are true or false:
S1 = S2; // true, determined by the string feature
S1.equals (S2); // true

After jdk1.5, the automatic packaging and automatic unpacking function is introduced, which will also affect the above Determination Rules: for example, the following is a correct definition.
Double D = 1.0; // the Java compiler automatically packs 1.0 into a new double (1.0 );
Instance:
Integer I = new INTEGER (42 );
Long L = new long (42 );
Double D = new double (42.0 );

Which one expressions evaluate to true?
A. (I = L)
B. (I = D)
C. (D = L)
D. (I. Equals (d ))
E. (D. Equals (I ))
F. (I. Equals (42) // 42 is automatically packaged as new INTEGER (42)

12. Assign the initial sequence to the variable of the basic type
The byte range is-128 ~ 127
When we give an integer without the l sign, the compiler automatically treats it as an int type, such
Int I = 1; is true
When we give a decimal number without the f flag, the compiler automatically treats it as a double type, such
Double D = 1.0; is true

13. Conversion of Basic Types
Rules: small ones can be automatically converted to large ones. Large ones must be mandatory to be converted to small ones. For example:
Double D = 1.0f; // correct, small to large, automatic
Float F = 1.0d (or 1.0); // error, large to small, need to force float F = (float) 1.0d;

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.