Java third week after class homework

Source: Internet
Author: User

Javahomework after class

I. Enumeration type

Code:

Enum Size{small,medium,large};

Public cl

The 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);

}

}

}

:

      

Conclusion: The enumeration type is a reference type, not an original type, and each of his specific values refers to a specific object. The same value refers to the same object. The values of the enumeration variables can be compared directly with "= =" and "Equals ()", and "= =" and "Equals ()" are the same for enumeration types.

Second, what kind of code is the data in Java

The original code: the highest bit is the sign bit, "0" stands for positive, "1" is negative, the remaining bits represent the true size of the value.
Inverse code: The inverse code of a positive integer is the same as the original code, and the inverse code of a negative integer is the symbol bit, and the original code is reversed by bit.

Complement: The complement of a positive integer is the same as the original code, and the complement of the negative integer is the inverse of the code plus 1.

Java uses "complement".

Iii. Scope of variables

The masking principle of a variable of the same name: two or more variables with the same name, when different values are defined, when used, within the range used, the value of the variable is the nearest (preceding) definition from his.

Instance:

public class NN {

private static int CC = 1;

public static void Main (String arga[])

{

int cc=2;

System.out.println ("cc=" +cc);

}

}

Result output:

Iv. Conversion of data types

Code:

public class NN {

private static int CC = 1;

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));

}

}

Result output:

Analysis: The computer can only recognize the binary, all the data will be converted to binary at the end. For example, the 2.4 in the source program is decimal, to be converted to binary, but the 2.4 binary is not the exact 2.4, just close to 2.4 , the actual 2.3 999 999 999 999 999, the floating-point number consists of two parts: exponent and Mantissa, and then the conversion of floating-point number binary and decimal, the floating-point number participates in the calculation, then the conversion process becomes unpredictable, and become irreversible.

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");

Results:

Cause: The two plus signs after the previous output statement are character connectors, the first plus sign in the next output statement is a mathematical plus sign, and the next plus sign is a character connector.

Java third week after class homework

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.