Course Assignment 02 (a few discussions on Java)

Source: Internet
Author: User
Tags arithmetic operators variable scope

Discussion one:

The file name must match the public class name

class Source File name = public class name +. JavaNote: If a class is declared public, it must also have the same source file name as the class name, even if the case is not wrong! This is not to say that a Java source file can only write a class, can write more than one class, but only one of the classes is "public", and Java requires the source file name to be consistent with it. (The interface conclusion is also applicable). suspect: Is there really only one common class in a Java class file?
Public
class test{public static void Main (string[] args) {}public class innerclass{}}
After testing this code is error-free, you can draw the conclusion that: multiple classes can be written in a Java source file, but only one of them is "public" (without inner-class functions), and the source file name must match the public class name, otherwise it cannot be compiled.

Discussion two:

Main method

The methods in Java are used to complete a task and return the result information, a Java application can contain one or more methods Java applications (Java application) from the main method to start executionPublic static void Main (String args[])The main method must be declared exactly as above, void indicating that the main method does not return any results, and that main () does not have to be placed in the public class to run.To change the return value of the main () method from void to int, can the program be compiled and passed? Can you run it?

It is not enforceable, whether or not to add return 0 .

Discussion three:

what is a static method? Methods that are decorated with static are called class methods or static methods. A class method is a method that does not depend on a particular object, so it can be referenced by the class name. A method that you can use without creating an object. Because the system allocates space for the methods in the class when the class is loaded, the static and static methods of the class can be accessed directly in the static method, but the instance fields and instance methods cannot be accessed directly in the static method, nor can the this keyword and the Super keyword be used.
class circle{static float pi=3.14f;  int R; static float Getarea () {return pi*r*r;//compilation error}
thinking: Why is the main () method of the entry point for Java rules static?

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)

Discussion three:Scope of the variableVariable scope: Each variable has a "valid" area (called a "scope"), out of which the variable will no longer be valid.

Java variables Follow the "masking principle of variable with the same name",Please read the relevant information after class to find out the relevant knowledge, and then write some of your own test code, as in this example, consciously defined in different places with the same name variable, to see what the output is the value.The value of the output is the value of the valid region of the variable. Discussion four:type conversions between variablesAutomatic type conversion is safe:
int intvalue=100; Long Longvalue=intvalue;
Coercion of type conversions can cause loss of information.
double doublevalue=1234567890;float floatvalue= (float) doublevalue;  System.out.println (Floatvalue); 1.23456794E9
In addition to using the coercion type conversion method of C, you can also complete type conversions with the wrapper class of the original type: scenario: The same data needs to be converted to multiple types, and this data needs to be used more long-term. In most cases, it is recommended to use the method of forcing type conversions directly. The real arrow is the loss of precision without precision.
Type Length and description Range
Char 16-bit, Unicode characters \u000~\uffff or 0~65535
Byte 8-bit, signed, byte -128~127
Short 16-bit, signed, short-integer -32768~32767
Int 32-bit, signed, integral type -2147483648~2147483647
Long 64-bit, signed, long Integer -9.223e18~9.223e18
Float 32-bit, floating-point, IEEE754 format About -3.4E+38~3.4E+38
Double 64-bit, floating-point, IEEE754 format About -1.7e+308~1.7e+308

The length of the large direction of small assignment, there will be a loss of precision

Hands-On Labs:

Run this program, what kind of output did you see, unexpectedly?

Conclusion: The result of the calculation using a double type of numerical value is imprecise.

Why is the numeric value of a double type not "mathematically accurate" result? 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.

How to handle precision loss? Workaround: Use the BigDecimal class

Note: You should use a string instead of a double value when constructing a BigDecimal object, or it is still possible to raise the computational precision issue. (why is this?) )

Double, which does not accurately represent 16-bit valid numbers, it makes sense to apply the BigDecimal (String) constructor to create objects when using BigDecimal. In addition, BigDecimal creates objects, and we cannot use traditional + 、-、 *,/et arithmetic operators to perform mathematical operations on their objects directly, but must call their corresponding methods. The arguments in the method must also be BigDecimal objects.

Hands-on brain:

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");
Why is there such an output result?

The first output is the output X and then output Y, and the two do not have any addition, the second result is to calculate the output of the x+y and then output "=x+y"

(Note: Some of the content is from the network)

Course Assignment 02 (a few discussions on 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.