JAVA enumeration basic operation, understanding of the original code and the anti-code complement for the operation of the in-depth understanding of

layman's understanding of enumerated types

  Look at the piece of code:

enum Size {the SMALL, MEDIUM,, the LARGE};
 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);   // false
         // primitive data type? 
        System.out.println (s.getClass () isPrimitive ().); // to false
         // convert from a string 
        Size Size.valueOf U = ( "the SMALL" ); 
        System.out.println (S == U) ;   // to true
         //It lists all values 
        for (Size value: Size.values ()) { 
            System.out.println (value); // sequentially outputs values of the enumerated type 
        } 
    } 

}

First, we define an enumeration class Size, defines two objects in the main function, were assigned to SMALL and LARGE

First, determine whether they are the same object? The results: FALSE

The second output is that he is a primitive data type it? The results: FALSE

The third through the conversion of a normal into a string enumeration object , he then determined whether the identical object is initially defined enumeration? The results: TRUE

By a fourth foreach statement , and a method of enumerated values () enumerated type member implemented in the array returned form, and then sequentially outputs values of an enumerator.

Through the above program examples, let us know, enumerated type is a reference type , specific values for each reference a particular object, the same values refer to the same object , the same as if s == u.

There is a method of enumerated type: ORDINAL () is used to obtain the index enumeration member , for example, a code of: System.out.println (t.ordinal ()); the result is: 2

Can also compare two objects enumerated order defined when: the compareTo (), for example: Size q = Size.MEDIUM; System.out.println ( q.compareTo (t)); the result is: -1, because the object q a value t smaller than the target value, and use it to subtract two subscripts.

In addition: You can also in adding new methods enumerated type

enum Color {   
    RED ( "red", 1), GREEN ( "green", 2), BLANK ( "White", 3), YELLO ( "yellow", 4 );  
     // member variable   
    Private String name;  
     Private  int index ;  
     // Constructors   
    Private Color (String name, int index) {  
         the this .name = name;  
         the this .index = index;   
    }   
    // find the index name corresponding   
    public  static String getName ( int index) {  
         for (C Color: Color.values ()) {  
             IF (c.getIndex() == index) {  
                return c.name;  
            }  
        }  
        return null;  
    }  
    // get set 方法  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getIndex() {  
        return index;  
    }  
    public void setIndex(int index) {  
        this.index = index;  
    }  
}  
public class Enum {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Color a=Color.RED;
        System.out.println(Color.getName(2));
        System.out.println(a.getName()+" "+a.getIndex());
        
    }

}

Adding private data to an enumerated type: index name and also add a method to find the index corresponding to the name, call the enumeration class in the main function, to achieve a new method.

The output is:

Green
Red 1

the original code, anti-code complement

Original code: is the simplest representation of the number of machines. A represents a sign bit MSB , ' 1' denotes a negative sign, '0' denotes a positive number . Other bits to store the absolute value of the binary number.

Here is a simple chestnut:

0101, which is the most significant bit is '0', that he first is a positive number, followed then the binary method to calculate its SUM =. 1 * 2 2 + 0 * 2 . 1 +. 1 * 2 0 =. 6, so that the binary number is 6

1111, which is the most significant bit to '1', that he is first negative, then followed the same way to calculate = SUM - (1 * 2 2 + 1 2 * 1 + 1 * 2 0 ) = -. 7, so that the binary the number of representatives is -7

The table can help you further understand the original code

 

Anti-Code: positive anti-code is equal to the original code, negative inverted is his original code except the sign bit, the other bit is inverted (for example: if the original is 1-> 0, originally 0-> 1)

For example: -5 corresponding to the binary number is: His 1101 is inverted: 1010

Some anti-code number to be observed

Complement: positive complement equal to his original code, negative complement equal to his +1 inverted, the sign bit unchanged

For example: a binary number is -5: 1101 anti-code: 1010 Complement: 1011

Finished basic concepts, let's look at some of the numbers-bit computing is how it is calculated: & Bitwise and, bitwise or |, bitwise ~ Bitwise XOR ^

  & Bitwise: if the corresponding bits of the two numbers are 1, the result was 1.

public class ma {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=4,b=5;
        System.out.println(a&b);
    }

}

Guess how many results: 4.

If the two numbers is a positive and a negative it?

public class ma {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=2,b=-5;
        System.out.println(a&b);
    }

}

Your answer is not: 0, Congratulations, you got it wrong, the actual output is: 2.

why? Since the number of the original code CKS computer, the inverted, complementary codes are the same, the negative is not the same, in order to enable the subtraction operation, in JAVA, all numbers are used to store complement.

I do not believe we can do the math.

Complement 2: 0010

-5 original code: 1101 anti-code: 1010 Complement: 1011

0010 & 1011 = 0010 The result is 2, and the answer is correct.

  Bitwise or: two operands corresponding bit is 0, the result is 0, and 1 otherwise.

public class ma {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=2,b=-5;
        System.out.println(a|b);
    }

}

I think this number is how much?

The answer is: -5

Complement 2: 0010

-5 original code: 1101 anti-code: 1010 Complement: 1011

0010 | 1011 = 1011 1011 see the sign bit is '1', to prove that he was a minus, but are in the form of a negative complement to the store, so we give him revert back

1011--> 1010--> 1101 results: -5 correct answer

  Bitwise: ~ binary operand is modified to a modified 0,0 1

  Bitwise XOR: ^ when bit operands represented by two phases simultaneously 0, otherwise 1

③ string connected tips

public class Test {
    
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");
    }
}

Output:

X + Y = 100200
300 = X + Y

When the string variable phase +, will form a new string

When the first phase + for variables, will first numerical sum, then the connection string  

Guess you like

Origin www.cnblogs.com/xiaofengzai/p/11544442.html