Thinking about some problems in Java

Source: Internet
Author: User

1. Thinking

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

In Java, the main () method is the entry method for a Java application, that is, when the program is running, the first method to execute is the main () method, which differs greatly from other methods, such as the name of the method must be main, and the method must be public static void type, the method must be a parameter that accepts a string array, and so on.

Because the main () method is called by the Java Virtual machine, it must be public, and the main () method called by the virtual machine does not need to produce any objects, so the main () method is declared static and does not require a return value, so it must be declared void. The final format is as follows: public void Main (string[] args)

2. Scope of variables

Example

Code:

Package Text;

Public class TEXT1 {

Private Static int value= 1;

Public Static void Main (string[] args)

{

int value=2;

System. out. println (value);

}

}

Results

3. Why is the numeric value of a double type not "mathematically accurate"?

This involves the conversion of binary and decimal.
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 other binary is the same, such as the binary number 11.01=1x2+1x (2 0 Power) +0+1x ( 2-2 power) = 3.25 in decimal.
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 to correspond 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 back of the number of bits will definitely bring error, can not get "mathematically accurate" results.

4. 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");

Output

x+y=100200

300=x+y

This is because the output of Java can be added to the variables, the + number on the first output represents a number of variables sequentially output, the second output code represents two variables added.

Thinking about some problems in Java

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.