Some small knowledge about Java (Course Assignment 02)

Source: Internet
Author: User
Tags float double

1, adding a space comment in the Java source code will not affect the program's compilation speed.

2, is there really only one public class in a Java class file?

If the entity is defined only in the first public class, or if two are not defined and no error is available, the program can run. Each compilation unit (file) can have only one public class, and each compilation unit can have only one exposed interface, and this interface is represented by its private class. If you can have more than one public class, the program will not be able to identify where to import the program from. Above this program appears the main class and the general public class, the second class is a general public class. It mainly distinguishes between public and main classes, main classes and main functions. There is not only one public class in the Ava class file, there can be more than one inner class, it can be common.

The return value of the 3,main method has void changed to int program can compile pass? Can you run it?

Compilation error

Run error

After the change, the compilation is correct.

Run error

4, why does Java stipulate that the main () method as the entry point of a program is static?

The main function is actually a method of the same class, such as a class is Test, then the main of the class is actually Test.main (string[] args), it is well known that if a method is not static, you should first instantiate the class, such as to test t=new Test (); Before you can call Test.main (); This is impractical for running a program's main function, so for example, define the main function as static so that Test.main () can be called directly.

5,

Global vs. local variables

6,

The int short long byte represents an integer whose value is accurate, and the float double is floating point, which represents an approximation, so that when an integer is converted to a floating-point type, the approximate value will be lost in precision. While ascll are integers, there is no precision loss in converting Char to integral type

7,

Because there is a problem in the decimal and binary conversion in Java, double type of the value of 64bit, that is, 64 binary number, minus the highest bit to indicate the positive and negative signs of the bit, in the lowest bit will be with the actual data error, in simple terms, we give the value, in In most cases it takes more than 64bit of digits to be accurately represented (even in the case of an infinite number of bits), whereas a double type has a value of only 64bit, and the number of digits behind it will definitely bring an error, and the result of "mathematically accurate" cannot be obtained.

8,

A: When using the BigDecimal (double) constructor, accurate errors occur when used improperly, and the limitations of binary floating-point numbers produce similar problems.

9,

Code:

Public class Test {

Public Static void Main (string[] args) {

int x=100;

int y=200;

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

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

}

}

When "x+y=" is in front, the default is to convert int x, y to String, and "x+y=" causes the compiler to think that x, Y is a string type, System. the parameters of Out. println () itself are string by default, and in this case the conversion is defaulted, resulting in the result, and the second is a simple output that does not cause the calculator to "misunderstand".

Some small knowledge about Java (Course Assignment 02)

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.