Java Chapter III Hands-on brain

Source: Internet
Author: User

Hands on the brain

1.

(1) int value=100;

(2) MyClass obj = new MyClass ();

Are these two definitions the same way?

A: the definition is the same, where (1) is a simplified notation (2).

2.

The result is falsebecause the comparison object is the same as the memory address pointed to by the object, and if the object is to be compared, the individual variables of the object should be compared.

3.

Please summarize what is different about this approach:

The difference:

This method is a construction method, so there is no return value, and its method name is the same as the class name.

When an object is created, its construction method is automatically called.

If the class does not have a constructor defined, the Java compiler automatically gives it a "default constructor method" without parameters at compile time.

4.

Why is the following code not compiled? Where did it go wrong?

A: When you define a parameter constructor yourself, the system will not automatically generate an parameterless constructor, and you will get an error.

5.

If a class has both an initialization block and a constructor, and it also sets the initial value of the field, who decides?

When an object is created, no assignment is made, the value is assigned according to the construction method, and when the assignment is made, it is calculated as the value after the assignment.

6.

The execution order of static initialization blocks:

(1) Execute the static initialization block before executing the normal method.

(2) When creating objects of subtypes, the execution of static initialization blocks of the parent type is also caused.

7.

How do I access an instance member of a class in a static method?

A: Use parameters to pass arguments, invoke instances.

8.

A: The integer package is really -2^8-1~2^8 between, that is, between -127~128, if the comparison, you can return the truth, if the value is out of range, this cannot be compared.

Java Chapter III Hands-on 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.