Java Grammar basics hands-on brain and after-school homework

Source: Internet
Author: User

Hands on Brain 1:

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

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

Run Result:false false True SMALL MEDIUM LARGE

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

Constants of enumerated types are stored in the order of strings, enumeration types are reference types, enumerations are not of the original data type, and each of its specific values refers to a specific object. The same value refers to the same object. You can use the "= =" and the equals () methods to directly compare the values of the enumeration variables, in other words, for variables of enum type, "= =" and The result of the Equals () method execution is equivalent.

hands on the brain 2: look at this figure, and then look at the number of bits in Java for each data type, and the range of values, what can you conclude?

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 that Java will automatically convert from a low-level type to an advanced type, that is, a data type with a relatively small range of values converted to a data type with a relatively large range of values.

Hands-On Labs: Please run the following code (Testdouble.java)

public class Testdouble {

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

}

}

What kind of output did you see, accidentally?

Output Result:

0.05 + 0.01 = 0.060000000000000005

1.0-0.42 = 0.5800000000000001

4.015 * 100 = 401.49999999999994

123.3/100 = 1.2329999999999999

Experiment:

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.

In fact, floating-point arithmetic is rarely accurate, as long as it is more than the range of precision can be expressed error. Errors are often generated that are not Because of the size of the number, but because of the precision of the number. Therefore, the resulting result is close but not equal to the desired result. Especially in the usefloatand theDoublefor precise Operation be very careful when you count. There are some alternatives that you might consider implementing. such as by usingLongtype to convert. We know that the representation of a floating-point number in a computer is multiplied by an integer (that is, the mantissa) by a radix (the computer typically2) is obtained by the whole power. (similar to the scientific notation, the scientific notation base isTen)floatthe memory structure is: sign bit indicates positive or negative, 1bit decimal point, 8bit trailing digits,bit (sign bit1represents a negative, 0indicates positive) the index is2for the bottom, the range is-128to the127,If you exceed the127,then from-128start the meter.  namely:127+1=-128the mantissa has omitted the first1bit of1,so at the time of restoration, first add the1. 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 the decimal16777215the use of scientific notation, if less than or equal to the direct use of the general binary notation. The notation of scientific notation is the same as that of decimals. The decimal part is the direct use of scientific notation, the form ofX * (2 ^ n). This results in a floating-point type that cannot be accurately represented in the computer's storage.

Hands on Brain 3:

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 output is:x+y=100200

300=x+y

Cause: "+" inSystem.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.

After-school exercise: Read the corresponding textbook, or use the Internet search engine, to find out the anti-code, complement and the original code of the concepts, and then write a sample program, the positive, negative number of various bit operations, observe the output, and the results of the manual calculation, to see the Java The number in the above is expressed by which code.

In the computer, the fixed-point number has 3 kinds of notation: the original code, the inverse code and the complement. Anti-code is a kind of numerical storage, but because the complement is more effective in the representation of the number in the computer form, so most computers generally do not use anti-code representation number.

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.

The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

The original code 10010= Anti-code 11101 (10010,1 is the symbol number, therefore is negative)

(11101) binary =-2 decimal

The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1of the inverse code.

After-school homework: Write a program, the user input two number, to find out its subtraction, and the message box to display the results of the calculation.

Import Javax.swing.JOptionPane;

public class Yusuan {

public static void Main (string[] args) {

TODO auto-generated method stubs

String Firstnumber,secondnumber;

int number1,number2,sum,minus,multiply,divide;

Read in first number from user as a string

Firstnumber =joptionpane.showinputdialog ("Enter first integer");

Read in second number from user as a string

Secondnumber =joptionpane.showinputdialog ("Enter second integer");

Convert numbers from type String to type int

Number1 = Integer.parseint (Firstnumber);

Number2 = Integer.parseint (Secondnumber);

Yunsuan the numbers

sum = number1 + number2;

Minus=number1-number2;

Multiply=number1*number2;

Divide=number1/number2;

Display the results

Joptionpane.showmessagedialog (NULL, "and as:" +sum+ "The difference is:" +minus+ "Product:" +multiply+ "quotient:" +divide, "Results", Joptionpane.plain_message);//Display the results on the window

System.exit (0);//Free space

}

}

Output Result:

Java Grammar basics hands-on brain and after-school homework

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.