Java post-class jobs

Source: Internet
Author: User

Hands on the brain 1

Enumeration type Gu Weihua 2015/10/3

Package test2;

public class Enumtest {

public static void Main (string[] args) {

TODO auto-generated method stubs

Size S=size.small;

Converting from a string to an enumeration

Size T=size.large;

Converting from a string to an enumeration

S and T refer to different objects

System.out.println (s==t); Compare the values of the S and T enumeration variables, output True at the same time, not output false at the same time

Not the original data type

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

Convert from String

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

Convert from String

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

List all of its values

Output

For (Size value:Size.values ())

{

System.out.println (value); Output

}

}

Enum Size{small,medium,large};

}

Operation Result:

False

True

SMALL

MEDIUM

LARGE

Conclusion:

An enumeration type is a reference type, and 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.

Binary representation of numeric values

The original code: the so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit,"0" is positive,"1" is negative, the remaining bits represent the size of the value.

Anti-code: The inverse code of positive number is the same as its original code, and the inverse code of negative number is the inverse of its original code, except the sign bit.

Complement: The complement of a positive number is the same as its original code; the complement of a negative number is the minus 1of its inverse code.

The number in Java is represented by the above-mentioned source code.

/* Gu Weihua 2015/10/4

* read the appropriate materials,

* or using an Internet search engine,

* Find out the concepts of anti-code, complement and original code

* then write the sample program,

* A number of positive, negative numbers for various bit operation, observation of the output results,

* compared with the result of manual calculation,

* See if the number in Java is represented by any of these codes.

*/

Package test3;

public class Zfshu {

public static void Main (string[] args) {

TODO Auto-generated method stubs

int num1=-9;

int num2=-10;

int sum;

int Cheng;

sum=num1+num2;

cheng=num1*num2;

SYSTEM.OUT.PRINTLN (sum);

System.out.println (Cheng);

}

}

Output Result:

-19

90

After-school exercises:

The/*java variable follows the "masking principle of a variable of the same name",

* Please read the relevant information after class to clarify the relevant knowledge,

* Then write some test code yourself, as in this example,

* Consciously define variables of the same name in different places,

* See what value the output is.

*/

Package test5;

public class Pb {

private static int value=5;

public static void Main (string[] args) {

TODO auto-generated method stubs

int value = 9;

System.out.println (value);

}

}

Output Result:

9

Hands-on brain:

Look at this diagram, and look at the number of bits in Java for each data type, and the range of values that you can draw. Different storage methods

The char 8-bit binary number range is 2 of 7 square to 2 of 7 square -1,byte 8-bit signed integer-any integer between 128 and 127, short 16-bit unsigned integer-any integer between 32768 and 32767, int 32-bit signed integer-2 of the 31-square to 2-31-by-1 arbitrary integer, a long 64-bit signed integer-2 of 63-square-to-2-63-by-1-any integer, float 32-bit single-precision floating-point number, double 64-bit double-precision floating-point number.

The conclusion is:

Java automates the conversion from a low-level type to a high-level type, where a data type with a relatively small range of values is converted to a data type with a relatively large range of values.

Hands-On Labs:

Output Result:

0.05+0.01=0.060000000000000005

1.0-0.42=0.5800000000000001

4.015*100=401.49999999999994

123.3/100=1.2329999999999999

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

In fact, floating-point arithmetic is rarely accurate, as long as it is more than the range of precision can be expressed error. Often the number of errors is the size, but because of the precision of the number.

Therefore, the resulting result is close but not equal to the desired result. Especially careful when using float and double for precise calculations. There are some alternatives that you might consider implementing. such as by using the long type to convert. We know that the representation of a floating-point number in a computer is obtained by multiplying an integer (that is, the mantissa) by a radix (usually 2 in the computer) to the full power. (like scientific notation, the scientific notation Base is ten) the memory structure of float is: The sign bit represents positive and negative, 1-bit exponent, 8-bit tail bit, 32-bit (sign bit 1 is negative, 0 means positive) the exponent is the base of 2, the range is 128 to 127, if more than 127, then from-128 counts. That is: The 127+1=-128 mantissa is omitted the 1th bit of 1, so in the restore should first in the first bit plus 1. It may contain both integers and decimal fraction, or only some of them, depending on the size of the numbers. For floating-point numbers with integral parts, there are two representations of integers, when integers are greater than 16777215 of the decimal, the scientific notation is used, and if less than or equal, the general binary notation is used directly. The notation of scientific notation is the same as that of decimals. The decimal part is the direct use of the scientific notation, in the form of X * (2 ^ n). This results in a floating-point type that cannot be accurately represented in the computer's storage.

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

The result is:

x+y=100200

300=x+y

Why is there such an output result?

The "+" in System.out.println ("x+y=" +x+y) is the join operator,

The "+" in System.out.println (x+y+ "=x+y") is the addition operator, which is the sum of two numbers.

Java post-class jobs

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.