Basic Java language Issues

Source: Internet
Author: User

1. There can be several public classes in a Java class file

The error that appears when testing code in Eclipse, where only final or abstract is allowed to decorate the class, that is, only one public-decorated class in a class file, makes a little analysis of the problem.

Package packages are mainly used to store classes, there can be countless public classes, there is no number limit, but a Java source file can only have one public class.

Java programs are executed from the main function of a public class, just as there is only one main function in a project when the C language was first studied. Only one public class is available to facilitate the class loader. If a class is declared public, it must also have the same source file name as the class name and is strictly case-sensitive.

Each compilation unit can have only one public class. Because each compilation unit can have only one public interface, it behaves as a common class. The interface can include many classes that support package access, as required. If there is more than one public class, the compiler will make an error. Of course, there can be no public class within a compilation unit.
In a source file. There can be many classes in a Java file, only one public class. Because in the public class to find the entrance to the program, if there are many public classes, the computer can not find the portal, then the program will not be able to execute.

2.Java variable follows "Masking principle of variable with the same name "

By comparing the two graphs, we can see that when the int number=1 in the main function is commented out, the result is 0.

Because in this program, the first number is a global variable and the second number is a local variable. In Java, a local variable can override the value of a global variable, and when a variable is locally used, the JVM takes precedence over the definition of the variable near the current location.

Type conversions in 3.Java

①byte (bytes): 8bits, Value range is -128-127

②short (Short integer): 16bits, Value range is-32768-32768

③int (integral type): 32bits, Value range is-2147483648-2147483648

④long (Long Integer): 64bits, Value range is-9233372036854477808-9233372036854477808

⑤float (floating point type): 32bits, value range is -3.40292347e+38-3.40292347e+38

⑥double (double): 64bits with a value range of -1.7976931486231570e+308-1.7976931486231570e+308

⑦char (character type): 16bits, value range of ' \u0000-u\ffff '

⑧boolean (Boolean): 1bits, value range is True/false

The arrows in int that point to double should be solid, and there should also be a dashed arrow pointing from long to float, which is a bit of an error in the diagram.

There are two types of data type conversions in the Java language: ① automatic type conversion: The compiler automates type conversion and does not need to write code in the program. ② coercion type conversion: Forces the compiler to make type conversions and must write code in the program.

The conversion rules for automatic type conversions are from a type with a small storage range to a type with a large storage range, and no loss of precision occurs. The conversion rule of coercion type conversion is the loss of precision from a type that has a large storage range to a small storage range.

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

float and double types are primarily designed for scientific calculations and engineering calculations, and they perform binary floating-point operations, which are divided into three parts: The sign bit, the exponent, and the tail part of the computer. When data is stored as binary in the computer, a series of wireless loops appears, and errors occur during data conversion. Decimal cannot accurately represent 1/3, and the same binary cannot accurately represent 1/10.

The value of double type occupies 64bit, that is, 64 binary numbers, minus the highest bit represents the sign of the bit, there are 63 bits, and we give the value, in most cases need more than 64bit of the number of bits to accurately represent (even need infinite number of bits), The double type has a value of only 64bit, and the number of digits behind it is bound to bring an error, and the result of "mathematically accurate" cannot be obtained.

5. Hands-on brain

In the statement one output, "+" joins X and Y, and the result is that x and y connect the two data.

The "+" in the two outputs of the statement is an operator that sums X and Y.

Basic Java language Issues

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.