Data type conversion operator &

The basic data type Notes

  • Integer type: long> int> short> byte
    • java default type is int type
      • long a = 123
        shows a value of type int assign a larger type long, than when the data type long long variable must be declared, and L must be added, modified data is declared as type long
      • long a = 2147483648L
  • Floating-point types: double> float
    • java default type is double type
    • Declare a float in java
      • The integer to a float without F.
        Float. 1 = B;
      • Assign a decimal number float need to add F.
        Float B = 1.0;
  • boolean: true 、false

A type automatically upgrade, int type byte, short, char type of operation

  • int = byte+byte
  • int = short+short
  • int = char - int
    • int char with the object when the first operation into byte code, such as: a ----> 97

      public class Test1 {

        public static void main(String[] args) {
      
            // 定义两个 byte类型变量
            byte b1 = 10;
            byte b2 = 20;
            // 定义int 类型变量 b3
            int b3 = b1 + b2;
            System.out.println("byte类型b1和b2的和为:");
            System.out.println(b3);
      
            // 定义两个 short类型变量
            short s1 = 1000;
            short s2 = 2000;
            // 定义int 类型变量 b3
            int s3 = s1 + s2;
            System.out.println("short类型s1和s2的和为:");
            System.out.println(s3);
      
            // 定义char类型变量
            char ch1 = 'a';
            // 定义int类型变量
            int i1 = 30;
            // 定义int 类型变量 ch3
            int ch3 = ch1 - i1;
            System.out.println("char类型ch1和int类型i1的差为:");
            System.out.println(ch3);
          }
          }   

Second, the automatic lift type, floating point type float, double operation

  • float = long + int
  • long = long +int
  • double = double + int
  • double = float +double

    class Test2 {public
    public static void main (String [] args) {
    // define variables of type int and long type variable i1 l1
    int i1 = 100;
    long l1 = 200 is;
    // define variables add, and save i1 l1 and, and outputs.
    long the Add = I1 + L1;
    System.out.println ( "the Add values:" + the Add);
    // l2 and define variables of type long float type variables F2
    long l2 = 1000000;
    float F2 = 0.45F;

          // 定义变量add2,保存l1和f1的和,并输出.
          float add2 = l2 + f2;
          System.out.println("add2的值:"+add2);
          // 定义 int类型变量i3 和 double类型变量d3
          int i3 = 1000000;
          double d3 = 0.45;
          // 定义变量add3,保存i2和d1的和,并输出.
          double add3 = i3 + d3;
          System.out.println("add3的值:"+add3);
          //定义 float类型变量f2 和 double类型变量d2
          float f4 = 1000000.789F;
          double d4 = 0.45;
          //定义变量add4,保存f2和d2的和,并输出.
          double add4 = f4 + d4;
          System.out.println("add4的值:"+add4);
      }

    }

Third, casts, a small -----> Large

  • The type int into char type
    char = (char) int
  • The double type into int type
    int = (int) double

    the Test3 {class public
    public static void main (String [] args) {
    // char type variable is defined ch, assigned to 'J'
    char ch = 'J';
    // ch lowercase variable 'j', and outputs
    ch = (char) (CH + 32);
    System.out.println (CH);

              // 定义char类型变量ch2,赋值为'a'
              char ch2 = 'a';
              // 将变量ch2转换为大写'A',并输出
              ch2 -= 32;
              System.out.println(ch2);
    
    
              // 定义double类型变量d3,int类型变量i3
              double d3 = 3.5;
              int i3 = 100;
    
              // 定义变量sum3,保存d3和i3的和,将sum3去除小数部分并输出.
              double sum3 = d3 + i3;
              System.out.println("sum3的的值:" + sum3);
              System.out.println("sum3的整数部分的值:" + (int) sum3);
    
    
              // 定义double类型变量d4,int类型变量i4
              double d4 = 5.8;
              int i4 = 300;
              // 定义int变量mul4,保存d4和i4乘积的整数部分,并输出
              int mul4 = (int) (d4 * i4);
              System.out.println("mul4的整数部分值:"+mul4);
    
          }

    }

Fourth,%: Remainder Symbol

public static void main(String[] args) {
          // 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果
          int a1 = 10;
          int a2 = 11;
          boolean b1 = a1 % 2 == 0;
          boolean b2 = a2 % 2 == 0;
          System.out.println("10是偶数?" + b1);
          System.out.println("11是偶数?" + b2);
          // 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果
          int a3 = 12;
          int a4 = 13;
          boolean b3 = a3 % 2 == 1;
          boolean b4 = a4 % 2 == 1;
          System.out.println("12是奇数?" + b3);
          System.out.println("13是奇数?" + b4);
        }

Fifth, the difference of a ++ and ++ a

  • ++ a, first add operation do
    A = 20 is
    ++. 3% A
    two-step operation:
    1. a = a+1 = 21
    2. a % 3 = 21 % 3 =0
  • a ++, Houjia Jia first computing
    A = 20 is
    A ++. 7%
    1. a % 7 = 20 % 7 ≠ 0
    2. a = a + 1 =21
    static void main public (String [] args) {
    // define a variable of type int a, assigned 20. A
    int = 20 is A;
    // define a boolean variable BO, it determines whether ++ a divisible by 3, and whether a ++ divisible by 7, the results assigned to bo
    Boolean a bo = ++ == 0 &&%. 3 a 7% ++ == 0;
    // output value of a, the value of bo.
    System.out.println ( "bo values:" BO +);
    System.out.println ( "a value:" + a);
    System.out.println ( "-------------");
    // define a variable of type int B, assigned the value 20. a
    int = 20 is B;
    // define a boolean variable BO2, determines whether or b ++ divisible by 3, and whether ++ b divisible by 7, the results assigned to BO2
    boolean BO2 = b ++ == 0 && 3% ++ b% 7 == 0;
    value // of the output b, bo2's.
    System.out.println ( "BO2 values:" + BO2);
    System.out.println ( "b values:" + b );
    }

VI ternary operator a> b true:? False

  • Ternary operator

      表达式 ?表达式成立返回值:表达式不成立返回值
    
       例: a > b ? true : false
    
       如果a > b成立,则返回true, 否则,返回false

    {class Test6 public
    public static void main (String [] args) {
    // printNum method call in the main method
    printNum ();
    }

              private static void printNum() {
    
          //        printNum方法中,定义int变量a赋值为9,b也赋值为9
                  int a = 9;
                  int b = 9;
    
          //        printNum方法中,定义int变量num赋值为++a.
                  int num = ++a;
          //        printNum方法中,定义boolean变量bo,使用三元运算符赋值,当num>=10,赋值为true,否则为false,打印bo的值
                  boolean bo = (num >= 10) ? true : false;
                  System.out.println("bo的值"+bo);
    
          //        printNum方法中,定义int变量num2赋值为b++.
                  int num2 = b++;
          //        printNum方法中,定义boolean变量bo2,使用三元运算符赋值,当num2>=10,赋值为true,否则为false,打印bo2的值
                  boolean bo2 = (num2 >= 10) ? true : false;
                  System.out.println("bo2的值"+bo2);
              }
          }

Guess you like

Origin www.cnblogs.com/Auge/p/11609894.html