java-01-Problem Solving

Source: Internet
Author: User
Tags variable scope what parameter

Question one: Can a Java class file have only one public class?

1 test Code:

Letter 1603 Li Dun-yue 20163520

Test if Java can have only one public class

2017.10.2

Public class Test {

first public class

Public Static void Main (string[] args) {

Output Statement

System. out. println ("Hello Word");

}

a second public class

Public class Innerclass

{

System.out.println ("Hello Word");

}

}

2 test results:

3 Conclusion: There is not only one public class in a Java class file, there can be more than one inner class, it can be common.

question two: the return value of the main () method is changed from void to int , can the program be compiled and passed? Can you run it?

1 test Code:

Letter 1603 Li Dun-yue 20163520

The test changes the return value of the main () method from void to int , does the program compile and pass? Can you run it?

2017.10.2

Package Helloword;

Public class Helloword {

Public Static int main (string[] args) {

TODO auto-generated Method stub

System. out. println ("Hello Word");

return 0;

}

}

2 test:

3 Conclusion: In the main method to change void to int program can be run, can be compiled through, need in main Add return 0 to the method.

question three: Why is the Java rule static as the main () method of the entry point of the program ?
1 static methods can directly use the class name + method name, do not need to instantiate the object, non-static methods need to instantiate the object, call the method through the object

2 The program is packaged as a jar file, and the only interface to the outside world is the main method. The user double-clicks the jar file, is actually lets the virtual machine executes the main method

The 3main method is provided to the customer, the customer does not know how to instantiate the object, more does not know that the instantiated object needs to enter what parameter, can only use static method

Question four: variable scope

1 test Code:

Letter 1603 Li Dun-yue 20163520

Test Variable Scope

2017.9.26

Public class Test1 {

Private Static int value= 1;

Public Static void Main (string[] args) {

int value=2;

System. out. println (value);

}

}

2 test:

(1):

(2):

3. Conclusion:

The scope of a variable of a class is the entire class, the variable scope of the function is the entire function, and if a variable x is defined in the class , the function of theclass defines an x, which uses the X in the function by default . If the outermost layer in the function defines an x, the function has a loop, and an xis defined in the loop, there is aconflict.

question five: check What conclusions can you draw about the number of bits per data type in Java, and the range of values that are represented?

for character and integer types such as:char,int,long,byte, Short, the value range of small conversion to a large range of values, no precision loss, but the integer type to float type, such as:float,double Conversion is the loss of precision, floating point range of small conversion to a large range of values, no precision loss.

Question six: Please run the following code, what kind of output did you see, unexpectedly?

1. Code:

public class Testdouble {

public static void Main (String args[]) {

System.out.println ("0.05 + 0.01 =" + (0.05 + 0.01));

System.out.println ("1.0-0.42 =" + (1.0-0.42));

SYSTEM.OUT.PRINTLN ("4.015 * 100 =" + (4.015 * 100));

System.out.println ("123.3/100 =" + (123.3/100));

}

}

2.:

3. Conclusion: The results are inaccurate and inaccurate. A computer can only recognize binary codes, regardless of what compiler is used to convert programs into binary machine code. The computer carries out the subtraction operation process, the binary conversion of the digit with the decimal point is not necessarily accurate

, floating-point type can be used for scientific calculation, but accurate calculation has error.

question seven: in building The BigDecimal object should use a string instead of a double value, or it might still cause a computational precision problem.

1. Related code:

Package ;

import java.math.BigDecimal;

Public class Testbigdecimal

{

Public Static void Main (string[] args)

{

BigDecimal f1 = new BigDecimal ("0.05");

BigDecimal F2 = BigDecimal. valueOf (0.01);

BigDecimal F3 = new BigDecimal (0.05);

System. out. println (" use String as the calculation result of the BigDecimal constructor parameter: " );

System. out. println ("0.05 + 0.01 =" + F1.add (F2));

System. out. println ("0.05-0.01 =" + f1.subtract (F2));

System. out. println ("0.05 * 0.01 =" + f1.multiply (F2));

System. out. println ("0.05/0.01 =" + f1.divide (F2));

System. out. println (" use double as the calculation result of the BigDecimal constructor parameter:" );

System. out. println ("0.05 + 0.01 =" + F3.add (F2));

System. out. println ("0.05-0.01 =" + f3.subtract (F2));

System. out. println ("0.05 * 0.01 =" + f3.multiply (F2));

System. out. println ("0.05/0.01 =" + f3.divide (F2));

}

}

2:

3 Conclusion: When using the BigDecimal (double) constructor, there is a precise error in using improper use, and the limitations of binary floating-point numbers produce types above-mentioned problems

Question eight: What is the output of the following code? Why is there such an output junction

1 related to code:

Public class Test {

Public Static void Main (string[] args) {

int x=100;

int y=200;

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

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

}

}

2 Results:

3Conclusion: The output result isx+y=100200,300=x+y. when"X+y="in the previous time, the defaultInttypeX,Ytranslates toStringtype,"X+y="makeThe compiler thinksX,Yis aStringtype of,System. out. println()the parameter itself defaults toStringIn this case, the conversion is defaulted, resulting inand the second one is the simple output, which does not allow the calculator to produce"Misunderstanding".

java-01-Problem Solving

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.