javaday3

package operator;

public class demo01 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//-128 强制转换 内存溢出 byte:-128~127
double c = i;//128.0 低容量转化为高容量时不用强制转换
System.out.println(b);
System.out.println(c);

    System.out.println("=======================");
    System.out.println((int)23.7);//23
    System.out.println((int)-45.89f);//-45

    System.out.println("==================");

}

}
package operator;

public class demo02 {
public static void main(String[] args) {
char c = 'a';
int d = c + 1;
System.out.println(d);//98
System.out.println((char)d);//b

    System.out.println("===================");
    int money = 10_0000_0000;
    int years = 20;
    int total = money*years;
    long total2 = money*years;
    System.out.println(total);//-1474836480 计算的时候溢出了
    System.out.println(total2);//-1474836480 默认是int,先计算后转换


}

}
package operator;

public class demo03 {
// 类变量 static
static double salary=2500;
String name;
int age;

//main方法
public static void main(String[] args) {
    //局部变量:必须声明和初始化值
    int i = 10;
    System.out.println(i);//10

    //变量类型 变量名字 = new Demo03();
    demo03 demo03 = new demo03();
    System.out.println(demo03.age);//0 基本类型默认值是0
    System.out.println(demo03.name);//null 除了基本类型,其余默认值都是null

    //类变量static
    System.out.println(salary);//2500.0

    System.out.println("====================================");
    //final 常量名 = 值
    final double PI = 3.14;
    System.out.println(PI);


}

}
package operator;

public class Demo04 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;

    System.out.println(a+b);
    System.out.println(a-b);
    System.out.println(a*b);
    System.out.println(a/(double)b);
}

}
package operator;

public class Demo05 {
public static void main(String[] args) {
long a = 123123123;
int b = 123;
short c = 10;
byte d = 8;
//没有Long时,计算时所有的非int类型转化为int类型
System.out.println(a+b+c+d);//Long
System.out.println(b+c+d);//Int
System.out.println(c+d);//Int

}

}
package operator;

public class Demo06 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);//false
System.out.println(a==b);//false
System.out.println(a!=b);//true
System.out.println(c%a);//取余运算 模运算 c/a=2...1
}
}
package operator;

public class Demo07 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后先给b赋值,然后自增
//a = a + 1;
System.out.println(a);//4
//a = a + 1;
int c = ++a;// 执行完这行代码前,先自增,再给c赋值

    System.out.println(a);//5
    System.out.println(b);//3
    System.out.println(c);//5
}

}
package operator;

public class Demo08 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;

    System.out.println("a&&b;"+(b&&a));//与运算 false
    System.out.println("a||b:"+(a||b));//或运算 true
    System.out.println("!(a&&b):"+!(a&&b));//非 true

    //短路运算
    int c = 5;
    boolean d = (c<4)&&(c++<4);
    System.out.println(c);//4
}

}
package operator;

public class Demo09 {
public static void main(String[] args) {
System.out.println(2<<3);//16

    int a =10;
    int b = 20;
    a+=b;//a= a + b;30
    a-=b;//a= a - b;10
    //字符连接符+
    System.out.println(""+a+b);//1020
    System.out.println(a+b+"");//30

    System.out.println("===================================");
    //三元运算符
    //x ? y : z;
    //如果x=true,则结果为y,否则为z
    int score = 50;
    String type = score < 60 ?"不及格": "及格";
    System.out.println(type);
}

}

posted @ 2021-07-22 23:11  風船  阅读(32)  评论(0)    收藏  举报