02-java Grammar Basics

Source: Internet
Author: User

"Hands on the brain."

Read the example carefully:

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");//Assign Value to u
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}; run it to analyze the running results. What conclusions can you get? Have you mastered the basic usage of enum types?

Answer: The result is false false true SMALL MEDIUM LARGE.

1, S and T refer to the same pair of images, and return the Fasle.

2, the original data type has 8 kinds, byte (byte) short (shorter integer) int (shaping) long (Long integer) float (floating point) double (double) char (String) Boolean (Boolean ) The S.getclass (). Isprimitive () returns FASLE because this is the enum type,and the value for S and U is small, so it returns true.

3. Enum Size{small,medium,large} is an enum class with a name of Size, where elements have small,medium,large that can be considered as objects of the class.

Conclusion:

1. Enum type is reference type

2 . The enumeration does not belong to the original data type, and each of its specific values refers to a specific object. The same value refers to the same object.

3, you can use the "= =" and the Equals () method directly compared to the value of the enumeration variable, in other words, for the enumeration type of variables, "= =" and the Equals () method to perform the result is equivalent.

Basic usage of enumerations:

1. for (Size value:Size.values ()) {
System.out.println (value);

The enumeration is traversed and the largest is found.

2. Size S=size.small Conversion from string to enumeration

3, System.out.println (s==u) to determine whether S and U are the same

4. 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 experiment"

Please run the following code (TESTDOUBLE.JAVA)

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. Tip: This problem is related to how floating-point numbers are represented inside a computer. You can use this knowledge to find relevant information in search engines.

Answer: the output is

0.05 + 0.01 = 0.060000000000000005
1.0-0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3/100 = 1.2329999999999999

It is inaccurate to operate with a numeric value of type Double. The reason 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=1x10²+2x10+3x (10 of 0 Power) +4x (10-1 power); the other binary is the same, such as the binary number 11.01=1x2+1x ( 2 of the power of 0) +0+1x (2-2 power) = decimal 3.25. 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).

"Hands on the 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?

Answer: The result is

x+y=100200
300=x+y

Cause: If the string string is followed by a + and variable, the variable is automatically converted to a string type, then the plus sign joins, then the two strings are concatenated into a new string output, and if there is a string for the addition and subtraction of the variable, the addition and subtraction of the variable is calculated from left to right. It then combines a new string with the string that follows it. The first one is stitching, and the second one is addition.

02-java Grammar Basics

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.