basic grammar java and after-school practice

One: Run the code, and analyzing the results

Code 1:

 Package Reserve;
   public  class the Main { 

    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);   // 
        // primitive data type? 
        System.out.println (s.getClass () isPrimitive ().);
         // convert from a string 
        Size Size.valueOf U = ( "the SMALL" ); 
        System.out.println (S == U);   // to true
         // List of all its values 
        for (Size value: Size.values ()) { 
            System.out.println (value);
        }
    }

}
 enum Size{SMALL,MEDIUM,LARGE};

Test results screenshot:

The results and conclusions:

1. For an enumerated type variables, "==" and "the equals ()" method executed results are equivalent.
2. The value of each specific enumeration of all references to a particular object. The same value as the reference the same object.
3. The enumeration is a constant, so it should be in uppercase;
4. Enumeration implicitly inherited java.lang.Enum, it has the properties and methods java.lang.Enum;
5.for (Size value: Size.values ()) {
System.out.println (value); enumeration traversal is output to the SMALL
                                                                         MEDIUM,
                                                                         the LARGE

 6.getDeclaringClass () method returns the enumerated enum constant current corresponding to the type of the object class

Code 2:

 package reserve;
public class Main {
    public static void main(String[] args) {
      int X=100;
      int Y=200;
      System.out.println("X+Y="+X+Y);
      System.out.println(X+Y+"=X+Y");
        }
}

Test results screenshot:

 Analysis: "" in which X + Y is not literal change occurs, "" operators outside the variable X + Y + is connected;

Code: 3:

package reserve;
public class Main {
    private static int value=1;
    
    public static void main(String[] args) {
        int value=2;
        System.out.println(value);
    }
}

Test results screenshot:

package hhj;
public class Hhj{
    public static int value=6;
    public static void main(String[] args) {
        int value=2;
        System.out.println(value);
    }
}

Test results: 2

Conclusion: assignment function inside an outer precedence assignment, assignment function can be assigned to the plurality of external functions class, if there is no repetition assignment function, the function value is a function of the outside.

Code 4:

package reserve;
public class Main {

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

Test results screenshot:

Conclusion: The results of using a double value calculated out is inaccurate.

 Two: the original code, anti-code complement interpretation

Original code: binary fixed-point notation (eight), the highest bit is the sign bit (from left one), "0" indicates positive and "1" represents a negative value represents the magnitude of the remaining bits (binary).

            

Anti-code: Positive anti-code the same as its original code.
            Its inverted negative original code sequence is inverted (0-> 1,1> 0), except for the sign bit.

           
Complement: complement positive number of the same original code and its
          negative is the complement (binary addition, attention carry 1 + 1) at the end thereof a plus radix.


          A positive number of the original code, anti-code and complement the same.

          A positive number, for example:

           

 

       Negative example:

           

 Code:

package reserve;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count[]=new int[8];
        int j=0;//定义一个数量为8的数组用来存储二进制的数;
        System.out.println("请输入整数:");
        int num = sc.nextInt();
        System.out.println("原码:");
        if(num>0) 
             count[0]=0;
        else {
            count[0]=1;
            num=-num;
        }
        System.out.print(count[0]);
        String a = ""; //用字符串拼接
        while(num!=0) { //利用十进制转二进制除2法
            a=num%2+a;
            num=num/2;
            j++;
        }
    
        for(int k=1;k<8-j;k++)
        {
            count[k]=0;
            System.out.print(count[k]);
        }
        System.out.println(a); 
        int math=Integer.parseInt(a);
        // String aS = String.valueOf(a);
          //char[] asC = aS.toCharArray();将字符串转换成整型
         //String result = Integer.toBinaryString(num);//将十进制数转换成二进制数
        
        }
}

 

三:看看下图,再查看Java中的每个数据类型所占的位数,和表示数值的范围,写出所得的结论。

                        

 

 结论:每个数据类型所占的位数和表示数值的范围

            byte:1字节 -128~127

            short:2字节 -2^15~2^15-1
            int :4字节 -2^31~2^31-1
            long:8字节 -2^63~2^63-1
            boolean:1字节 true false(java中不能以0或非0代替)
            float: 4字节 -3.403E38~3.403E38
            double:8字节 -1.798E308~- 4.9E324
            char:2字节  ’\u0000‘~' ’\uffff '(16进制的,换算过来即0~65535)
           (1字节等于8位)

            实数代表无精度损失,虚线代表有精度损失。一般来说在实线两端都是由低精度指向高精度的类型,所占的位数从高到低,范围从小到大,所以可得出,低精度向高精度转化不丢失精度,反之,从高精度转向低精度则会损失。

Guess you like

Origin www.cnblogs.com/hhjing/p/11543497.html