Java operator Summary

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators

The following example shows you how to use a primary data type with a specific operator. Fundamentally, it is the same example that executes over and over again, using only a different main data type. The file compiles without an error, because the rows that cause the error have been//! into the annotation content.

 

: Allops.java//Tests All "operators on" all "//primitive data types to show which//ones are accepted by the J

Ava compiler. Class Allops {//To accept the results of a Boolean test:void f (Boolean b) {} void Booltest (Boolean x, Boolean y)
    {//Arithmetic operators://! x = x * y; //!
    x = x/y; //!
    x = x% y; //!
    x = x + y; //!
    x = XY; //!
    x + +; //!
    x--; //!
    x = +y; //!
    x =-Y; Relational and logical://!
    F (x > Y); //!
    F (x >= y); //!
    F (x < y); //!
    F (x <= y);
    F (x = = y);
    F (x!= y);
    f (!y);
    x = x && y; x = x | |
    Y Bitwise operators://!
    x = ~y;
    x = x & y; x = x |
    Y
    x = x ^ y; //!
    x = x << 1; //!
    x = x >> 1; //!
    x = x >>> 1; Compound Assignment://!
    x = = y; //!
    x = y; //!
    x *= y; //!
    x/= y; //!
    x%= y; //!
    x <<= 1; //!
    x >>= 1; //!
    x >>>= 1; X &= y;
    x ^= y;
    x |= y; Casting://!
    char C = (char) x; //!
    byte B = (byte) x; //!
    Short s = (short) x; //!
    int i = (int) x; //!
    Long L = (long) x; //!
    float f = (float) x; //!
  Double d = (double) x;
    } void Chartest (char x, char y) {//Arithmetic operators:x = (char) (x * y);
    x = (char) (x/y);
    x = (char) (x% y);
    x = (char) (x + y);
    x = (char) (XY);
    x + +;
    x--;
    x = (char) +y;
    x = (char)-y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y);
    Bitwise operators:x= (char) ~y;
    x = (char) (x & y);
    x = (char) (x | y);
    x = (char) (x ^ y);
    x = (char) (x << 1);
    x = (char) (x >> 1);
    x = (char) (x >>> 1);
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y;
    x <<= 1; X >>= 1;
    x >>>= 1;
    x &= y;
    x ^= y;
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    byte B = (byte) x;
    Short s = (short) x;
    int i = (int) x;
    Long L = (long) x;
    float f = (float) x;
  Double d = (double) x;
    } void Bytetest (Byte x, byte y) {//Arithmetic operators:x = (byte) (x* y);
    x = (byte) (x/y);
    x = (byte) (x% y);
    x = (Byte) (x + y);
    x = (Byte) (x-y);
    x + +;
    x--;
    x = (byte) + y;
    x = (byte)-y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y);
    Bitwise OPERATORS:X = (byte) ~y;
    x = (Byte) (x & y);
    x = (Byte) (x | y);
    x = (byte) (x ^ y);
    x = (Byte) (x << 1);
    x = (Byte) (x >> 1);
    x = (Byte) (x >>> 1);
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y;
x <<= 1;    x >>= 1;
    x >>>= 1;
    x &= y;
    x ^= y;
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;
    Short s = (short) x;
    int i = (int) x;
    Long L = (long) x;
    float f = (float) x;
  Double d = (double) x;
    } void Shorttest (short x, short y) {//Arithmetic operators:x = (short) (x * y);
    x = (short) (x/y);
    x = (short) (x% y);
    x = (short) (x + y);
    x = (short) (XY);
    x + +;
    x--;
    x = (short) +y;
    x = (short)-Y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y);
    Bitwise OPERATORS:X = (short) ~y;
    x = (short) (X & y);
    x = (short) (x | y);
    x = (short) (x ^ y);
    x = (short) (x << 1);
    x = (short) (x >> 1);
    x = (short) (x >>> 1);
    Compound assignment:x = y;
    x = y;
    x *= y;
x/= y;    x%= y;
    x <<= 1;
    x >>= 1;
    x >>>= 1;
    x &= y;
    x ^= y;
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;
    byte B = (byte) x;
    int i = (int) x;
    Long L = (long) x;
    float f = (float) x;
  Double d = (double) x;
    } void Inttest (int x, int y) {//Arithmetic operators:x = x * y;
    x = x/y;
    x = x% y;
    x = x + y;
    x = XY;
    x + +;
    x--;
    x = +y;
    x =-Y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y);
    bitwise OPERATORS:X = ~y;
    x = x & y; x = x |
    Y
    x = x ^ y;
    x = x << 1;
    x = x >> 1;
    x = x >>> 1;
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y;
    x <<= 1;
    x >>= 1;
    x >>>= 1;
    x &= y; X ^= Y
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;
    byte B = (byte) x;
    Short s = (short) x;
    Long L = (long) x;
    float f = (float) x;
  Double d = (double) x;
    } void Longtest (long x, long y) {//Arithmetic operators:x = x * y;
    x = x/y;
    x = x% y;
    x = x + y;
    x = XY;
    x + +;
    x--;
    x = +y;
    x =-Y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y);
    bitwise OPERATORS:X = ~y;
    x = x & y; x = x |
    Y
    x = x ^ y;
    x = x << 1;
    x = x >> 1;
    x = x >>> 1;
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y;
    x <<= 1;
    x >>= 1;
    x >>>= 1;
    x &= y;
    x ^= y;
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;byte B = (byte) x;
    Short s = (short) x;
    int i = (int) x;
    float f = (float) x;
  Double d = (double) x;
    } void Floattest (float x, float y) {//Arithmetic operators:x = x * y;
    x = x/y;
    x = x% y;
    x = x + y;
    x = XY;
    x + +;
    x--;
    x = +y;
    x =-Y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y); Bitwise operators://!
    x = ~y; //!
    x = x & y; //! x = x |
    Y //!
    x = x ^ y; //!
    x = x << 1; //!
    x = x >> 1; //!
    x = x >>> 1;
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y; //!
    x <<= 1; //!
    x >>= 1; //!
    x >>>= 1; //!
    x &= y; //!
    x ^= y; //!
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;
    byte B = (byte) x; Short S = (Short) x;
    int i = (int) x;
    Long L = (long) x;
  Double d = (double) x;
    } void Doubletest (Double x, double y) {//Arithmetic operators:x = x * y;
    x = x/y;
    x = x% y;
    x = x + y;
    x = XY;
    x + +;
    x--;
    x = +y;
    x =-Y;
    Relational and logical:f (x > Y);
    F (x >= y);
    F (x < y);
    F (x <= y);
    F (x = = y);
    F (x!= y); //!
    f (!x); //!
    F (x && y); //!
    F (x | | y); Bitwise operators://!
    x = ~y; //!
    x = x & y; //! x = x |
    Y //!
    x = x ^ y; //!
    x = x << 1; //!
    x = x >> 1; //!
    x = x >>> 1;
    Compound assignment:x = y;
    x = y;
    x *= y;
    x/= y;
    x%= y; //!
    x <<= 1; //!
    x >>= 1; //!
    x >>>= 1; //!
    x &= y; //!
    x ^= y; //!
    x |= y; Casting://!
    Boolean B = (Boolean) x;
    char C = (char) x;
    byte B = (byte) x;
    Short s = (short) x;
    int i = (int) x; Long L= (long) x;
  float f = (float) x; }
} ///:~


The ability to note a Boolean value (Boolean) is very limited. We can only give it a value of true and false. You can also test whether it is true or false, but you cannot add a Boolean value to them, or perform any other type of operation.
In Char,byte and short, we can see the "transition" effect of arithmetic operators. Arithmetic operations on any of these types will result in an int. It must be explicitly "styling" back to the original type (narrowing the conversion causes the loss of information) to assign the value back to that type. for int values, however, no styling is necessary because all data already belongs to the int type. However, do not relax your vigilance and think everything is safe. If you perform a multiplication operation on two large enough int values, the resulting value overflows. The following example shows you this:

: Overflow.java
//surprise! Java lets you overflow.

public class Overflow {public
  static void Main (string[] args) {
    int big = 0x7fffffff;//MAX int value
    PRT ("b IG = "+ Big");
    int bigger = big * 4;
    PRT ("bigger =" + bigger);
  }
  static void Prt (String s) {
    System.out.println (s);
  }
}///:~

The output results are as follows:
Big = 2147483647
Bigger =-4

It does not receive an error message from the compiler and the runtime does not have an abnormal response. Java coffee is really a good thing, but it is not "so" good!
For Char,byte or short, mixed assignments do not require styling. Even if they perform transformational operations, they will get the same results as direct arithmetic operations. On the other hand, omitting the styling can make the code look more concise.
As you can see, any of the main types can be transformed into other main types, except Boolean. Similarly, when modeling is a smaller type, you must pay attention to the consequences of narrowing conversions. Otherwise, you will unknowingly lose information in the modelling process.

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.