Java Programming Thought learning notes

Source: Internet
Author: User

1. Type default value

The default value of the object is null. The default value of Boolean is false. The default value for char is ' \u0000 ' (null).

The default value of byte (byte) 0. Short default value (short) 0. The int default value is 0. The long default value is 0L. The float default value is 0.0f. Double the default value of 0,0d.

Therefore, there is a default initialization for the member fields, but for local variables, if they are not initialized, they are random values of memory.

2. The data in Java is a signed number.

3, static modified fields and methods belong to the class, not the Object! Non-static fields and methods are associated with specific objects. Static gets space in the class load initialization, so the Statics method can only access static Fields!!! (no objects have fields that correspond to objects) but non-static methods can access static fields. (static fields are created before non-static fields). a static field of a class is shared by all objects of that class !

Test Case:

public class Test {

private Static Class mytest{

private int i=10;
private static final int j=1000;

public void GetOne () {

SYSTEM.OUT.PRINTLN ("Non-static method accesses non-static fields" +i);
SYSTEM.OUT.PRINTLN ("Non-static method accesses static field" +J);
}
public static void Gettwo () {

SYSTEM.OUT.PRINTLN ("Non-static method accesses non-static fields" +i);
System.out.println ("Static method Access static field" +j);
}

}

public static void Main (string[] args) {
Mytest.gettwo ();
New MyTest (). GetOne ();
MyTest t1= new MyTest ();
MyTest t2= new MyTest ();
MyTest t3= new MyTest ();
SYSTEM.OUT.PRINTLN ("Object calls static method");
T1.gettwo ();//Object Call static method
T2.gettwo ();//Object Call static method
T3.gettwo ();//Object Call static method
}


}

Results:

Static methods access static fields 1000
Non-static methods access non-static fields 10
Non-static method access to static field 1000
Object calls a static method
Static methods access static fields 1000
Static methods access static fields 1000
Static methods access static fields 1000

4, = = Compare the address, the Eques method compares the value of the address.

Test Case:

public static void Main (string[] args) {
Integer n1 = new Integer (47);
Integer n2 = new Integer (47);
SYSTEM.OUT.PRINTLN (n1 = = N2);
SYSTEM.OUT.PRINTLN (n1! = n2);
System.out.println (N1.equals (N2));
}


Results:

False
True
True

5. The left shift operator (<<) moves the operand to the left of the operator to the left by the number of digits specified to the right of the operator (in the low 0). The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The "signed" right shift operator uses "symbol extension": If the value is positive, insert 0 at the high, and if the value is negative, insert 1 at the high. Java has also added an "unsigned" right shift operator (>>>) that uses the "0 extension": either positive or negative, it inserts 0 in the high position.

If you shift the char, byte, or short, they are automatically converted to an int before the shift is made.

6. String operator +

7, pay attention to the automatic type conversion in arithmetic operation. Use mandatory types to ensure accurate results.

8, (1) A simple continue will return the beginning of the most inner loop (top) and continue execution.
(2) A continue with a label will reach the position of the label and re-enter the loop immediately following the label.
(3) Break breaks the current loop and moves away from the end of the current label.
(4) A tagged break breaks the current loop and moves away from the end of the loop indicated by that tag.

Test Case:

public static void Main (string[] args) {
int i = 0;
Outer:while (True) {
PRT ("Outer while Loop");
while (true) {
i++;
PRT ("i =" + i);
if (i = = 1) {
PRT ("continue");
Continue
}
if (i = = 3) {
PRT ("continue outer");
Continue outer;
}
if (i = = 5) {
PRT ("break");
Break
}
if (i = = 7) {
PRT ("Break outer");


Break outer;
}
}
}
}


static void Prt (String s) {
System.out.println (s);
}

Test results:

Outer while loop
i = 1
Continue
i = 2
i = 3
Continue outer
Outer while loop
i = 4
i = 5
Break
Outer while loop
i = 6
i = 7
Break outer

9. The garbage collector only knows to release the memory allocated by new, so it does not know how to release the object's "special" memory. To solve this problem, Java provides a method called Finalize (), which defines it for our class. Ideally, it should work like this: Once the garbage collector is ready to release the storage space occupied by the object, it calls Finalize () first, and the memory of the object is actually reclaimed only during the next garbage collection process. So if you use Finalize (), you can do some important cleanup or cleaning work during garbage collection.

10. Final keyword

Decorated class: The class cannot be inherited.

modifier field or variable: Indicates that the variable cannot be modified like const, the inner class accesses the local variable, the local variable is declared final and the variable is stored on the heap.

Modification method: The method cannot be replicated. At the same time, if the method does not have a loop, the switch statement applies the inline to the compiler.


11. Static Inner class

To correctly understand the meaning of static when applied to an inner class, you must remember that the object of the inner class holds a pair of the package class that created it by default
The handle of the elephant. However, if we say that an inner class is static, this argument is not tenable. The static inner class means:
(1) To create an object of the static inner class, we do not need an external class object.
(2) An external class object cannot be accessed from an object in the static inner class.


Java Programming Thought learning notes

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.