糖逗 发表于 2020-8-2 17:08:52

JAVA学习Day2【运算符】

学习视频:https://www.bilibili.com/video/BV17J411G72L?p=20

20 - 40

/*
* (1)浮点类型不精确 因为十进制的小数部分转二进制需要舍去
* (2)float类型的4个字节能表示的数字范围比long类型的8个字节还要大 因为float底层存的是指数
*/
class test01{
        public static void main(String[] args) {
                System.out.println(10);
                System.out.println(0b10);//二进制在数字前面加0B或0b
                System.out.println(010);//八进制在数字前面加0
                System.out.println(0x10);//十六进制在数字前面加0X或0x
        }
}

/*
* 基本数据类型之间的转换
* (1)自动类型转换(小容量存到大容量)boolean不参与、byte、short、char如果进行数学运算会自动升级为int
* byte < short < int < long < float < double
* (2)强制类型转换(大容量存到小容量中)强制类型转化是有风险的,可能会出现溢出
*/

class test02{
        public static void main(String[] args) {
                double d1 = 10;
                System.out.println(d1);
               
                int d2 = (int)10.0;
                System.out.println(d2);
               
                byte b1 = 1;
                byte b2 = 2;
                byte b3 = (byte)(b1 + b2);
                System.out.println(b3);
               
                char c1 = '1';
                char c2 = '2';
                char c3 = (char)(c1 + c2);
                System.out.println(c3);
               
                short s1 = 1;
                short s2 = 2;
                short s3 = (short)(s1 + s2);
                System.out.println(s3);
               
                int x= 1;
                int y = 2;
                System.out.println(x/y);//整数/整数 结果还是整数
                System.out.println((double)x/y);
               
               
        }
}


/*
* 数据类型转换的特例:字符串类型
*/

class test03{
        public static void main(String[] args) {
                char c1 = '0';
                char c2 = '1';
                System.out.println(c1 + c2 + "");//97
                System.out.println("" + c1 + c2);//01
                System.out.println(c1 + "" + c2);//01
        }
}


/*
*运算符
*取余% 只看被模数(符号前面)的正负号被模数%模数
*/
class test04{
        public static void main(String[] args) {
                int x= 10;
                int y = 3;
                System.out.println();
               
                int m = 1;
                int n = ++m;
                System.out.println(m);//2
                System.out.println(n);//2
               
                m = 1;
                n = m++;
                System.out.println(m);//2
                System.out.println(n);//1
        }
}


class test05{
        public static void main(String[] args) {
                int i = 1;
                int j = 5;
                j *= i++ + j++;
                //j*=i++ + j 等价于 j = j * (i++ + j)
                System.out.println(j);
        }
}

/*
* 位运算
* (1)左移<< 右移 >> 无符号右移 >>> 与& 或|异或^ 取反~
*/

class test06{
        public static void main(String[] args) {
                System.out.println(4 << 3);//等价于4乘以2的3次方 为32!!
                //<< 几位就乘以2的几次方!!
                System.out.println(32 >> 3);//等价于32除以2的四次方!!
                // >> 几位就除以2的几次方!!
                System.out.println(32 >>> 3);
                System.out.println(-32 >>> 3);
                }
}


页: [1]
查看完整版本: JAVA学习Day2【运算符】