Basic work of Java grammar--hands-on brain and experimental problems after class

Source: Internet
Author: User
Tags mul

Hands on one's head:

Read the example carefully : Enumtest.java, run it, analyze the running results?

What conclusions can you get? Have you mastered the basic usage of enum types?

1. Source code:

public class Enumtest {

public static void Main (string[] args) {

Size S=size.small;

Size T=size.large;

S and T refer to the same object?

System.out.println (s==t); //

is the original data type?

System.out.println (S.getclass (). isprimitive ());

Convert from String

Size u=size.valueof ("SMALL");

System.out.println (S==u); True

list all of its values

For (Size value:Size.values ()) {

System.out.println (value);

}

}

}

Enum Size{small,medium,large};

2. Output Result:

False

False

True

SMALL

MEDIUM

LARGE

3. Analysis and summary usage

Enumeration types are used with an enum class, which is a common base class for JAVA enumeration types. The purpose of an enumeration is to have a variable take only one of several fixed values.

1, Size S=size.small;

Assigning a value to an S variable can only be given to a constant that has already been defined (that is, one of the small,medium,large ).

2, System.out.println (s==t);

Determine If S is the same as T .

3, System.out.println (S.getclass (). isprimitive ());

Invokes the method in the enum.

4, Size u=size.valueof ("SMALL");

Assigning a value to u, similar to 1 , uses a different assignment statement.

5,System.out.println (s==u);

Determine If S is the same as u .

6.for (Size value:Size.values ())

{

System.out.println (value);

}

Iterate over the enumeration and find the largest.

7,enum Size{small,medium,large};

Indicates the use of Class enum, named Size, which has element small,medium,large, which can be considered as objects in a class.

7, System.out.println ()

System.out.println (s); Run Result:SMALL(enumeration directly provides the toString method)

System.out.println (S.name ());// Run Result:SMALL ( print name )

System.out.println (S.ordinal ());/// Get the position of the enumeration in the sequence

System.out.println (S.getclass ());// get enum class

System.out.println (S.getdeclaringclass ());// return to the declaration of this class

Hands on the brain two:

What kind of output did you see, accidentally?

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

Please find information through the Internet, or read the relevant computer textbooks to solve this problem.

Tips:

This problem is related to how floating points are represented within a computer. You can use this knowledge to find relevant information in search engines.

1. Source Code

An example of an inexact double

Mao 2015.9.29

public class Testdouble {

public static void Main (string[] args) {

TODO auto-generated Method Stub

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. Results

3. Conclusion

The result of a calculation using a numeric value of type Double is imprecise.

A value of type double takes 64bit, or up to four 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 happens to be 2 of the n - th square).

Workaround, use the BigDecimal class. You should use a string instead of a double value when building a BigDecimal object , or it is still possible to raise the problem of computational precision.

Hands on the brain three:

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?

1. Operation result

1. Conclusion

System.out.println ("x+y=" +x+y);// splicing, string connection

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

After-school practice one:

Java variables Follow the "masking principle of the same name variable", please read the relevant information after class to find out the relevant knowledge, and then write some test code, as in this example, consciously defined in different places with the same name variable, to see exactly what the output is.

1. Source Code

shielding principle of variable with the same name

Mao 2015.9.29

public class Test {

private static int value = 1;

same name, go to comment, do not use

public static void Main (string[] args) {

TODO auto-generated Method Stub

int value = 2;

System.out.println (value);

}

}

2. Operation Result

3. Conclusion

Notice the problem with the same name masking.

After-school Practice II:

Write a program, the user input two number, to find out its subtraction, and the message box to display the calculation results.

1. Source code:

mao2015.10.7

Two-digit operation in the form of a dialog box

Import Javax.swing.JOptionPane;

public class Twonums {

public static void Main (string[] args) {

TODO auto-generated Method Stub

String num1,num2;

int number1,number2;

int add,minus,mul,div;

NUM1 = Joptionpane.showinputdialog

("Input the first integer please");

num2 = Joptionpane.showinputdialog

("Input the second integer please");

Number1 = Integer.parseint (NUM1);

Number2 = Integer.parseint (num2);

Add = Number1 + number2;

minus = Number1-number2;

Mul = Number1 * NUMBER2;

div = number1/number2;

Joptionpane.showmessagedialog (NULL, "Number1 + number2 is"

+add+ "\nnumber1-number2 is" +minus+ "\nnumber1 *"

+ "Number2 is" +mul+ "\nnumber1/number2 is" +div

, "Results", joptionpane.plain_message);

}

}

2. Operation result

Basic work of Java grammar--hands-on brain and experimental problems after class

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.