10-21 hands on the brain

Source: Internet
Author: User

Zi Defining classes

Boxandunbox. Java

Hello. Java

Initializeblockclass. Java

That is, only the definition of the member variable, not the active initialization operation, the compiler assigns a default value to the member variable.

For basic types, there are different default values such as int, char, short, and so on, 0,boolean to false, and null for the object. The default initialization is to ensure that the initialization process will be performed, only on member variables, and there is no such mechanism for local variables.

Mypackageclass. Java

ObjectEquals

Strangeintegerbehavior . Java

for two non- new integer objects, when compared, if the value of two variables is between 128 and 127, the comparison is true, and if the value of the two variable is not in this interval, the result of the comparison is false

The result is false when a non- new generated integer variable is compared to a variable generated by the new Integer (). (Because a non-new integer variable points to an object in a Java constant pool, the new Integer () generates a variable that points to the new object in the heap, and the address in memory is different)

Teststaticinitializeblock. Java

In a class

class content (static variable, static initialization block) = = instance content (variables, initialization blocks, constructors)

In two inheritance classes

parent class (static variable, static initialization block)= = Subclass (static variable, static initialization block)= = parent class (variable, initialization block, constructor)= = subclasses (variables, initialization blocks, constructors)

Test2

Access class strength members in static methods

Package main;

Public class Circle {

Private Double radius = 1.0;

double Getare () {

return radius * radius * Math. PI;

}

Public Static void Main (string[] args) {

Circle mycircle = new circle ();

System. out. println ("radius is" + Mycircle.radius); Radius is not static, static methods can call non-static member variables, non-static methods through class objects

}

}

 Packagemain; Public classCircle {Private DoubleRADIUS = 1.0; DoubleGetare () {returnRADIUS * RADIUS *Math.PI; }     Public Static voidMain (string[] args) {Circle mycircle=NewCircle (); System.out.println ("Radius is" + Mycircle.radius);//radius is not static, static methods can call non-static member variables, non-static methods through class objects  }}

10-21 hands on the brain

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.