Basic Java Grammar Exercises

Source: Internet
Author: User

1. Read the example: Enumtest.java, and run. Analysis results

Code:

 Public classEnumtest { Public Static voidMain (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 StringSize u=size.valueof ("SMALL"); System.out.println (S==U);//true//list all of its values          for(Size value:Size.values ()) {//TraverseSystem.out.println (value); }    }}enumSize{small,medium,large};

The program created the enumeration type, and the enumeration type is created to use the ENUM keyword enumeration type to put the relevant constants (programming specification: Constant each letter uppercase) into an enumeration type, each value name will be converted to a string. Because the enum itself is a class, it cannot be inherited.

The S and T references in the program are not the same object and are not the original data type.

2. Original code: Directly before the value of a symbol to add a bit of notation. The value range of byte is -27~ 27-1=

Inverse code: Positive number: The inverse code of positive number is the same as the original code. Negative number: The inverse of the negative, the sign bit is "1", the numeric part is reversed by a bit. Example: Symbol bit value bit

Complement: Positive: The complement of the positive is the same as the original code. Negative number: The complement of a negative number is the sign bit "1". And, this "1" is both the sign bit and the value bit. The numeric part is reversed and then the bottom (lowest bit) is added by 1. That is, the number in the "anti-code +1" Java is in the complement. The 3.java variable follows the "masking principle of a variable with the same name", and the value of the variable output is related to the scope.
 Public class Try {        publicstaticint potato=10;      Public Static void Main (String args[]) {        //afteraday        int eaten=2;         int potato=8;        System.out.println ("Eat" +eaten+ "left" +potato);     }

The result of the program operation is:

4. The number of bits per data type and the range it can represent

Short 16-bit -32768~32767-2^15~2^15-1

int 32-bit -2147483648~2147483647-2^31~2^31-1

Long 64-bit -922372036854775808~9223372036854775807-2^63~2^63-1

Float 32-bit 1.4e-45~3.4028235e38

Double 64-bit 4.9e-324~1.7976931348623157e308

5. Run a program to analyze the results

 Public class testdouble {    publicstaticvoid  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 * =" + (4.015 *));        System.out.println ("123.3/100 =" + (123.3/100));}    }

Conclusion: The result of the calculation with a double type is imprecise.

Because this involves a binary and decimal conversion problem. 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 the 0 power) +4x (10-1 power); the other binary is the same, such as the binary number 11.01=1x2+1x (2 0 Power) +0+1x ( 2-2 power) = 3.25 in decimal. 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). For example, for example, to use 4bit to represent decimal 3.26, from high to low to correspond to 2 1,0,-1,-2 power, according to the top of the analysis, should be in the binary number 11.01 (corresponding to the decimal 3.25) and 11.10 (corresponding to the decimal 3.5) between the selection. In short, we give the value, in most cases need more than 64bit more digits to accurately represent (even need infinity), and the double type of the value of only 64bit, the back of the number of bits will definitely bring error, can not get "mathematically accurate" results.

6. Enter a section of code to determine the output:

 Public class Try {        publicstaticvoid  main (String args[]) {        int x= ;         int y=200;        System.out.println ("x+y=" +x+Y);        System.out.println (X+y+ "=x+y");}    }

Results:

Cause, +x+y, which indicates the value of output xy in turn, and x+y represents an operation

7. Use the dialog box to enter numbers and calculate, you need to determine the operator

ImportJavax.swing.JOptionPane; Public classCalcurlate { Public Static voidMain (String args[]) {string firstnumber,secondnumber;        String operator; intNumber1,number2,result; Firstnumber=joptionpane.showinputdialog ("Enter the first integer") ); Secondnumber=joptionpane.showinputdialog ("Enter the second integer"); Operator=joptionpane.showinputdialog ("Enter the Operator"); Number1=Integer.parseint (Firstnumber); Number2=Integer.parseint (Secondnumber); if(Operator.equals ("+") {result=number1+number2; Joptionpane.showinputdialog ("The result is" +result); }                if(Operator.equals ("-") {result=number1-number2; Joptionpane.showinputdialog ("The result is" +result); }                if(Operator.equals ("*") {result=number1*number2; Joptionpane.showinputdialog ("The result is" +result); }                if(Operator.equals ("/") {result=number1/number2; Joptionpane.showinputdialog ("The result is" +result); }    }}

Basic Java Grammar Exercises

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.