Java Classroom related issues

Source: Internet
Author: User
Tags arithmetic operators what static method

1. question : is there really only one public class in a Java class file?

Answer: A A Java class file can have multiple classes but only one public class.

An error occurs when the following program has two public classes running.

2. question: change thereturn value of the main () method from void to int, can the program be compiled and passed? Can you run it ?

The program will not run after you change void to int as shown above .

3. question: What static method? Why is the Java rule static as the main() method of the entry point of the program ?

A: The Java static method uses public memory space, which means that all objects can be referenced, and you can use the method with the class without creating an object.

Non- Static Members A function must construct and instantiate the class that contains the function before executing it. If the non-static main is allowed, then the class where the main function resides must be instantiated first .

4. Scope of the variable

as shown in the Java variable follows the "variable masking principle with the same name"

5. Conversion of types in Java

Int,short\,byte,long double,float int turn float Double long turn double There will be a loss of precision. In ascii all integers so char ext int no precision loss.

6. Why is the value of double type not "mathematically accurate" result?

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, to use 4bit to represent the 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) between the selection.
in short, we give the value, in most cases need more than 64bit more digits to accurately represent (even need infinity), and the double type of the value of only 64bit, the number of bits behind will definitely bring error, can not get "mathematically accurate" The result.

7. You should use a string instead of a double value when building a BigDecimal object, or it is still possible to raise the problem of computational precision. (why is this?) )

Double does not accurately represent BigDecimal 16-bit valid numbers when you use BigDecimal to create an object. The objects created by BigDecimal cannot use traditional arithmetic operators such as +-*/to perform mathematical operations on their objects directly, and must call the corresponding (F1.add (F2)) methods. Because the parameter in the method must be an BigDecimal object, the definition should be a string.

8.

What is the output of the following code?

int x=100;

int y=200;

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

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

Why is there such an output result?

The result of the operation shows that the first output is processed as a string, while the second output is treated as an integer addition. when "x+y=" is in front, the compiler defaults to converting X,Y to String type, the System.out.println () default parameter is a String type.

Java Classroom related issues

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.