Java Course Assignment 02

Source: Internet
Author: User

---restore content starts---

1. Can there be only one public class in a Java class file?

There can be at most one public class in a Java source file, and when there is a public class, the source file name must be the same, otherwise it cannot be compiled, and if there is no public class in the source file, the file name does not have a consistency requirement in the class.

The main () does not have to be placed in the public class to run the program.

2, the main () method to change the return value from void to int, the program can be compiled through it? Can you run it?
Cannot have to return the result of type int

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

A non-static member function must first 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, and then call Main, this instantiation of code needs to be implemented. If it is also non-static, there is a need to write an instantiation that has been repeated cannot be achieved.
Therefore, the Java language specifies that main must be static.

4. Scope of variables

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.

Description

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.

5. On the accuracy of double type numerical operation and its solving method

Defines two variable number1=0.01;number2=0.05 of type double;

number1+number2=0.06000000000000005;

It is obvious that the result is imprecise.

Workaround: Define the String type Number1,number2 and so on, and accept number with the object of the BigDecimal class,

The subtraction function is called in BigDecimal.

6. What does the following code output?

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 Result: x+y=100200 300=x+y

Why is there such an output?

The + sign in System.out.println (), which is an operator when connecting a numeric type, is a connector when connecting to a string

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.