Summary of various Java issues and some discussions

Source: Internet
Author: User

1. Can a Java class file really have only one public class?

Test:

Package testing;

public class Test {
public static void Main (string[] args)
{

}
public class Innerclass
{

}

}

This program can run successfully, which means that there can be multiple classes in a program, but only one of them is public, and Java requires the source file name to be consistent with it.

2.main method

Program test:

Results:

Summarize:

Change the return value of the main () method from void to int program sequencing cannot pass, cannot run, can only be strictly public static void main (string[] args)

3. Classroom Exercise 02

4. Why is the Java rule static as the main () method of the entry point of the program?

A static method is a method that uses the static modifier in a class that has been loaded and allocated when the class is defined. A non-static method is a method that is non-static and does not occupy memory when the class is defined, but only when the class is instantiated as an object, the object calls the method to allocate memory. If main is a non-static method, it must also instantiate the class of main before calling it. While main is the entry point for the program, for this reason, the Java language specifies that main is static.

5. Course Exercise 03

6. Scope of variables

Each variable has a valid scope, and the variable is no longer valid when it is scoped out.

Java variables follow the "masking principle of variable with the same name", and the output value is the value of the valid region of the variable.

7.java type Conversion

8. Hands-On Labs

The result of a calculation using a numeric value of type Double is imprecise.

Reason: (from the network)

N-binary can be understood as: the power of the numerical x cardinality, for example, we are familiar with the decimal number 123.4=1x102+2x10+3x (10 of the 0 power) +4x (10-1 power); The same is true of the other binaries, such as binary
Number 11.01=1x2+1x (2 of 0 Power) +0+1x (2 to 2 power) = decimal 3.25.
A value of type double takes 64bit, or 64 binary numbers, except that the highest bit represents the positive and negative sign, and the lowest bit is bound to have an error with the actual data (unless the actual data is exactly 2 of the n-th square)
For example, for example, to use 4bit to represent decimal 3.26, from high to low in turn corresponds to 2 1,0,-1,-2 power, according to the top of the analysis, should be in the binary number 11.01 (corresponding to the decimal 3.25)
and 11.10 (corresponding to the decimal 3.5) select between.
In simple terms, the values we give, in most cases, require more digits than 64bit to be accurately represented (even for infinite bits), and the double type has only 64bit, after
The number of bits in the face is bound to bring an error, and the result of "mathematically accurate" cannot be obtained.

9. Hands-on brain

int x=100;
int y=200;
System.out.println ("x+y=" +x+y);

Results:


Cause: The first line of output is output x and then output y, the two do not have any add operation;
The second result is to calculate the output of the x+y first and then output "=x+y".

Summary of various Java issues and some discussions

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.