Java Language Foundation Course assignment 02

Source: Internet
Author: User
Tags variable scope

1. Is there really only one common class in a Java class file?

Java programs are executed from the main function of a public class (actually , the main thread), just as the C program starts with the main () function. Only one public class is available to facilitate the class loader. A public class can only be defined in a file with its class name as its file name.

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

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,
then you need to write a function to instantiate the class where main is, and then call Main, where is this instantiation code written? If it is also non-static, would it not be to write a function to instantiate the class it is in?
Therefore,the Java language specifies that main must be static.

3. Change the return value of the main () method from void to int, can the program be compiled and passed? Can you run it?

in the Main method to change void to an int program can be run, can be compiled through, you need to add a return statement in the main method .

4. Running Testdouble.java

Run:

Cause Analysis:

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 ). The values we give, in most cases, require more digits than 64bit to be accurately represented (even in the case of infinite bits), and the double type has 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. Variable Scope issues:

in the In Java, the scope of a variable is divided into four levels: class-level, object-instance-level, method-level, block-level.

class-level variables, also known as global-level variables or static variables, need to be decorated with the static keyword. Class-level variables already exist after the class is defined, occupy memory space, can be accessed through the class name, and do not need to be instantiated. An object instance-level variable is a member variable that is not allocated until the memory space is instantiated for access. A method-level variable is a variable defined inside a method, which is a local variable.

In addition to accessing the method-level variables inside the method, you can access the class-level and instance-level variables.

The inside of a block can access class-level, instance-level variables, and if the block is contained inside a method, it can also access the method-level variables.

Method-level and block-level variables must be initialized to be displayed, otherwise they cannot be accessed.

6. Shielding principle of variable with the same name:

If there is a variable with the same name in the program code, output is based on the nearest principle.

7. What is the conclusion of the type conversion accuracy question:

Byte is 1 bytes, valid bit 8 bit, range-128 ~ +127

Short is 2 bytes, valid bit 16 bit, range-32768 ~ +32767

int accounts for 4 bytes, valid bit 32 bits, range -2^31 ~ +2^31-1

Long is 8 bytes, valid bit 64 bit, range -2^63 ~ +2^63-1

Float occupies 4 bytes, valid bit 32 bit, range -3.403e38~3.403e38

Double is 8 bytes, valid bit 64 bit, range -1.798e308~1.798e308

Char occupies 1 bytes, valid bit 8 bits

if one of the two operands is of type double, the other is converted to a double type;

Otherwise, if one of the operands is float, the other is converted to float;

Otherwise, if one operand is long, the other is converted to long;

Otherwise, the two operands are converted to the int type.

8. Why is the output different?

int x=100;

int y=200;

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

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

The first output statement first output the character, the system to the back + as the connection function directly output the value of two data; the second output statement system defaults to the first + operation symbol, so the output will be different.

Java Language Foundation 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.