Day04 关键字、标识符、变量及运算符

关键字

在这里插入图片描述

标识符注意点

在这里插入图片描述

整数拓展

  1. 二进制以0b开头,比如:0b10;
  2. 八进制以0开头,比如:010;
  3. 十六进制以0x开头,比如:0x10。

浮点数扩展

  • 涉及银行业务计算钱的时候,不用float或者double类型(存在舍入误差,容易出错);
  • 最好完全避免使用浮点数进行比较;
  • 银行业务使用BigDecimal类计算。

字符拓展

  • 可以将字符类型强制转换为数字类型;
  • 所有的字符本质还是数字。
  • Unicode编码(U0000-UFFFF): 97=a,65=A
char c = '\u0061';

转义字符

  1. “\t”:制表符(字符之间留出空格);
  2. “\n”:换行。

布尔值扩展

Boolean flag = true;

if(true){ //新手经常写为“if(flag==true)”
    
} 
//代码要精简易读

类型转换

低------------------------------------------------------------高

byte,short,char --> int --> long --> float --> double

  • 高 --> 低,强制转换,”(类型)变量名“;
  • 低 --> 高,自动转换。

注意点

  1. 不能对布尔值进行转换;
  2. 不能把对象类型转换为不相干的类型;
  3. 在把高容量转换为低容量的时候,强制转换;
  4. 转换的时候可能存在内存溢出,或者精度问题。

(操作比较大的数的时候,注意溢出问题)

[ JDK7的新特性,数字之间可以用下划线分割 ]

​ 例如:100_0000_0000,且下划线不会被输出。

  • 溢出
int money = 1000000000;
int year = 20;
int total = money*year; //-1474836480
  • 转换出错
int money = 1000000000;
int year = 20;
long total = money*year; //-1474836480,默认值为int,转换之前已经存在问题
  • 正确的类型转换
int money = 1000000000;
int year = 20;
long total = money*((long)year); //20000000000,先把一个数转换为Long

变量

在这里插入图片描述

变量的作用域

在这里插入图片描述

public class Demo08 {

    //类变量 static
    static double salary = 8000;

    //属性:变量

    //实例变量(在方法外,类中):从属于对象,如果不自行初始化,变量的值为该类型的默认值。
    /**数值的初始值一般为 0 或者 0.0;
     *字符串变量的初始值一般为 u0000;
     *布尔值的默认值为 false;
     *除了基本类型,其余的默认值均为 null。
     * */
    String name;
    int age;

    //主程序方法,main方法
    public static void main(String[] args) {

        //局部变量(在方法里):必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型 变量名字 = new Demo08();
        Demo08 demo08 = new Demo08();
        System.out.println(demo08.age);

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

    //其他方法
    public void add(){

    }
}

变量的命名规范

在这里插入图片描述

常量

在这里插入图片描述

public class Demo09 {

    //static、final、public为修饰符,不存在先后顺序
    static final double PI = 3.14;

    public static void main(String[] args) {

        System.out.println(PI);

    }
}

运算符

在这里插入图片描述

重点掌握算术运算符、赋值运算符、关系运算符和逻辑运算符。

算术运算符

package operator;

public class Demo01 {

    public static void main(String[] args) {

        //二元运算符
        //快捷键:Ctrl + D,复制当前行到下一行
        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 Demo02 {

    public static void main(String[] args) {

        long a = 12765399L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//Long 当多个操作中,有一个数值为long类型,那么结果的类型也为long
        System.out.println(b+c+d);//Int 当操作的数值中没有long类型,那么结果的类型为Int,无论操作的数值中有没有Int类型的数值
        System.out.println(c+d);//Int
        //当多个操作中,有一个数值为double类型,那么结果的类型也为double
    }
}

关系运算符

package operator;

public class Demo03 {

    public static void main(String[] args) {

        //关系运算符返回的结果:true 或者 false
        //关系运算符通常搭配 if 使用

        int a = 10;
        int b = 20;
        int c = 21;
        
        //取余,模运算
        System.out.println(c%a);// c / a  21/10=2...1

        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);

    }
}

逻辑运算符

package operator;

    //逻辑运算符

public class Demo05 {

    public static void main(String[] args) {
        //与(and)、或(or)、非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,结果就为true
        System.out.println(!(a&&b));//如果是真,则变为假;如果是假,则变为真。

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

位运算符

package operator;

public class Demo06 {
    public static void main(String[] args) {

        /*
        A = 0011 1100
        B = 0000 1101
        ---------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B = 1111 0010

        计算2*8=2*2*2*2=16
        位运算效率极高!!!
        << 左移,*2
        >> 右移,/2

        0000 0000 0
        0000 0001 1
        0000 0010 2
        0000 0011 3
        0000 0100 4
        0000 1000 8
        0001 0000 16
         */

        System.out.println(2<<3);
    }
}

扩展赋值运算符

package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        //a+=b; a=a+b
        //a-=b; a=a-b

        System.out.println(a);

        //字符串连接符:”+“
        // 在+运算符两侧,只要有一方出现String类型(字符串类型),它就会把另一个操作数或者其他操作数都转换成String再进行连接。
        System.out.println(""+a+b);//1020,字符串在前,直接拼接后面的数字。
        System.out.println(a+b+"");//30,字符串在后面,前面的数字先进行运算。

    }
}

三元运算符

package operator;
//三元运算符
public class Demo08 {
    public static void main(String[] args) {
        // x ? y : z
        // 如果 x = true,则结果为 y;否则,结果为 z。

        int score = 9;
        String type = score < 60 ? "不及格" : "及格";//必须掌握
        // if
        System.out.println(type);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值