JavaSE-第二章

发布于 2022-02-18  72 次阅读


第二章 java基础语法

2.1 纲要

  • 标识符
  • 关键字
  • 数据类型
  • 运算符
  • 控制语句
  • 方法的创建与使用
  • 递归问题

2.1.1 标识符

  • 标识符:就是给类、方法、参数起名字
  1. java标识符的规则
    • 标识符:数字、字母、下划线、美元符号,其他的都不可以
    • 数字不能作为开头使用
  2. 关键字不能作为标识符名称使用
  3. 区分大小写
  4. 理论上没有长度限制

案例:

合法标识符非法标识符
_abc3abc 数字不能开头
HelloWorldhello-world 使用了中划线
public2public 关键字
hello world 使用了空格

标识符命名时最好"见名知意",如果两个单词组成的名称,那么第二个单词的首字母必须大写。这种命名方式称为"驼峰式命名"

2.1.2 关键字

  • 注意:都是小写的
classextendsimplementsinterfaceimport
packagebreakcasecontinuedefault
doifelseforreturn
switchwhilefalsetruenull
booleanbytecharshortint
longfloatdoubletrycatch
throwthrowsfinallyabstactfinal
nativeprivateprotectedpublicstatic
synchronizedthransinetvolativeinstanceofnew
supervoidassertenumgoto保留字
const保留字

2.2 数据类型

2.2.1 分为两类

  • 基本类型(8种):
    • 数字类型
      • 整型:byte、short、int、long
      • 浮点:float、double
    • 字符类型:char
    • 布尔类型:boolean(true|flase)
  • 引用类型(3种):
    • 数组
    • 接口

2.2.2 8种数据类型的范围

  • 一个字节占8位,可以使用0补位,例如:00000001
  • 笔试题:byte i = 127; i+1<1是否成立
  • 答:成立,因为每种类型都有取值范围,超出范围就会取反

2.3 字节编码

字符编码对应的编码方式
ISO-8859-1也称为latin-1;主要用于西欧语言
GB2312/GBK/GB18030用于汉字的编码
unicode全球统一编码utf-8,utf-16,utf-32
  • utf-8是三个字节处理中文的,效率高,节省空间
  • 汉字的转换
  • 每个中文汉字都对应一个ascii码

2.4 创建字符

public class HelloWorld {

    public static void main(String[] args){
		//字符使用单引号
		char param = '中'
        System.out.println(param);
    }
}

2.5 变量

  • 变量就是java中一个最基本的单元;包含4个属性:
    • 变量的类型:基本类型或引用类型都可以
    • 变量名称:符合标识符即可
    • 变量的值:如果是基本类型就是具体的值,如果是引用类型就是内存地址
    • 变量的存储单元:根据不同的类型决定在内存中开辟空间的大小(1byte=8位)
  • 创建一个变量
public class Demo {
    public static void main(String[] args){
        //创建了一个变量
        int i = 10;
    }
}

2.6 数据类型

2.6.1 整型

  • 分为:byte、short、int、long
  • 表示的方式:
    • 十进制:1,2,3...
    • 八进制:以0开头;012
    • 十六进制:以0x开头;0x123
  • 在java中整型的默认类型就是int
  • 如果使用长整型:10L(大小写均可)

2.6.2 浮点

  • 浮点类型:单精度float;双精度double
  • java中默认精度类型是:double
  • 案例:
  • 正确创建单精度
public class Demo {
    public static void main(String[] args){
        //创建单精度;大小写均可
        float f = 2.0f;
        float f1 = 2.0F;
    }
}
  • 笔试题:float和long谁大?
    • 答:float大,因为,数字的大小不是按照字节数比较的

2.6.3 布尔类型

  • boolean类型:true;flase,只能为这两个值,不能取其他的值
  • 案例:
public class Demo {
    public static void main(String[] args){
        //创建布尔类型
        boolean bo = true;
        boolean bo1 = false;
    }
}
  • 错误的

2.6.4 基本类型的转换

  • 在java中基本类型可以转换,但是,boolean类型比较特殊不能与其他类型转换;
  • 类型转换分为:
    • 默认转换:小类型转大类型
    • 强制转换:大类型转小类型
  • 类型由小到大转换:
    • byte->short->int->long->float->double
    • 注意:byte、short、char之间不会相互转换,首先转为int后再转换
    • 注意:只要不超出范围,整型可以直接赋值给byte、short、char
  • 总结:多种类型混合运算的过程中,返回最大的类型
  • 案例:
public class Demo {
    public static void main(String[] args){
        //创建参数
        byte b = 100;
        
        //byte a = 1000; 错误,超出范围
        
        //short b = 1000; 错误,变量不能同名
        
        short d = 1000; //正确
        
        int c = 1000; //正确
        
        long e = c; //正确;因为int比long小,默认就可以转换了

        //int f = e; //错误;因为long比int大,超出范围了,存在精度丢失
        int f = (int)e; //正确;可以进行强制类型转换
    }
}
  • 案例2:
public class HelloWorld {

    public static void main(String[] args){
		int i = 10/3; //正确的
		
		long n = 10; //正确的
		
		//int f = n/3; //错误;返回的是long类型
		
		int f = (int)n/3; //正确,可以把long强制转为int类型
		
		//byte h = (byte)n/3; //错误;返回的是int类型
		
		byte h = (byte)(n/3); //正确
		
    }
}
  • 案例3:
public class HelloWorld{
    
    public static void main(String[] args){
        short k = 10;
        
        short k1 = k+1; //错误,因为需要转为int后在运算
    }
}
  • 案例4:
public class HelloWorld{
    
    public static void main(String[] args){
        //正确:char转为int后在运算
        char f = 'a';
        int n = f+100;
        System.out.println(n);
    }
}

2.7 运算符

算术运算符+,-,*,/,++,--,%
关系运算符<,<=,>,>=,==,!=
布尔运算符&&,||,&,|,!
位运算符(二进制)了解即可&,|,~,^,>>,>>>,<<&,按位与(AND)[真真为真,真假为假]|按位或(OR)[假假为假,其余全真]^按位异[相同为假,不同为真]~按位非(NOT)[真则假,假则真]>>右移>>>右移,左边空出的位以0填充<<左移
赋值类运算符=,+=,-=,*=,/=,%=
字符串连接运算符+
条件运算符?:
其他运算符instanceof,new

2.7.1 算术运算符

  • 案例1:++就是自身加一
public class Demo{
    public static void main(String[] args){
        //创建变量
        int i = 10;
        i++; //自身加一
        System.out.println(i);
    }
}
  • 案例2:
public class Demo{
    public static void main(String[] args){
        //创建变量
        int i = 10;
        //++在后表示先赋值,自身再加一
        int n = i++;
        System.out.println(i);
        System.out.println(n);
    }
}
  • 案例3:
public class Demo{
    public static void main(String[] args){
        //创建变量
        int i = 10;
        //++在前表示自身先加一在赋值
        int n = ++i;
        System.out.println(i);
        System.out.println(n);
    }
}
  • 案例4:
public class Demo{
    public static void main(String[] args){
        //创建变量
        int i = 10;
        //i++相等于i=i+1
        i = i+1;
        System.out.println(i);
    }
}
  • 案例5:
public class Demo {
    public static void main(String[] args){
        //创建变量
        short i = 10;
        //正确;推荐使用 +=、-=,*=,/=、因为,【可以自动转为接收的类型】
        i += 1;
        System.out.println(i);
    }
}
  • 案例6:
public class Demo {
    public static void main(String[] args){
        //创建变量
        int i = 10;
        i /= 3;
        System.out.println(i);
    }
}
  • 案例7:取模
public class Demo {
    public static void main(String[] args){
        //创建变量
        int i = 10;
        int n = 1%3; //就是取余数
        System.out.println(n);
    }
}

使用场景:就是分页的使用

public class Demo {
    public static void main(String[] args){
        //总记录数
        int total = 20;
        //每页显示9条
        int pageSize = 9;
        //总页码
        int pages = 1;
        if (total%pageSize==0){
            pages = total/pageSize;
        }else {
            pages = total/pageSize+1;
        }
        System.out.println("总页码="+pages);
    }
}

2.7.2 关系运算符与布尔运算符

  • 与:两个操作数相与,如果都为true则返回true,否则返回false
  • 或:两个操作数相或,只要有一个为true则返回true
  • 非:取反
  • 异或:相异为true,就是两个操作数不同(一个true一个false)
  • 短路与&&和逻辑与&的区别?
    • 它们返回的结果是相同的,只是运算的过程不相同
    • 短路与:如果左边为true那么计算右边,否则直接返回flase
    • 逻辑与:无论左边是否为true都计算右边后再返回结果
  • 短路或||和逻辑或|的区别?
    • 它们返回的结果是相同的,只是运算的过程不相同
  • 注意:短路与要求两侧必须是布尔类型,而逻辑与并不要求。
  • 笔试题:4&7=?
    • 答:返回4,因为转为二进制后再运算;只有1遇到1返回1,其他都返回0

2.7.2.1 案例

public class TestDemo {
    public static void main(String[] args){
        //创建变量
        boolean op1 = 10>5;
        boolean op2 = 10<5;
        
        //输出
        System.out.println("op1=" + op1);
        System.out.println("op2=" + op2);
        
        //短路与与逻辑与
        System.out.println("op1&&op2="+(op1&&op2));
        System.out.println("op1&op2+="+(op1&op2));
        
        //短路或与逻辑或
        System.out.println("op1||op2="+(op1||op2));
        System.out.println("op1|op2="+(op1|op2));
        
        //取反
        System.out.println("!op1="+!op1);
        
        //异或
        System.out.println("op1^op2="+(op1^op2));
        System.out.println("op1^op1="+(op1^op1));

2.7.3 条件运算符

  • 三元运算符
public class TestDemo {
    public static void main(String[] args) {
        //创建变量
        int i = 10;
        //三元运算符
        int n = i>30?1:-1;
        System.out.println(n);
        
        //三元运算符
        boolean bo = i%3==0?true:false;
        System.out.println(bo);
    }
}

2.8 控制语句

2.8.1 判断语句

2.8.1.1 语法

  • if语句
if(布尔类型){
    一行或多行语句
}

如果只有一行语句,那么可以省略大括号

if(布尔类型)
    一行
  • if-else语句
if(布尔类型){
    一行或多行语句
}else{
    一行或多行语句
}
  • if-else-if-else:执行的效率高,因为,遇到一个true其他判断就被忽略不执行了
if(布尔类型){
    一行或多行语句
}else if(布尔类型){
    一行或多行语句
}else if(布尔类型){
    一行或多行语句
}else{
    一行或多行语句
}

2.8.1.2 案例:只使用if,这种判断的效率低

public class TestDemo {
    public static void main(String[] args){
        int Age = 3;
        if (Age>0&&Age<=5){
            System.out.println("儿童");
        }
        
        if (Age>5&&Age<=10){
            System.out.println("小学生");
        }
        
        if (Age>10&&Age<=18){
            System.out.println("中学生");
        }
    }
}

2.8.1.3 案例:使用if-else if-else 效率高

public class TestDemo {
    public static void main(String[] args){
        int Age = 3;
        if (Age>0&&Age<=5){
            System.out.println("儿童");
        }else if (Age>5&&Age<=10){
            System.out.println("小学生");
        }else if (Age>10&&Age<=18){
            System.out.println("中学生");
        }else{
            System.out.println("大学生");
        }
   	}
}

2.8.1.4 案例:两个添加判断if-else

public class TestDemo {
    public static void main(String[] args){
        int Age = 3;
        if (3%2==0){
            System.out.println("偶数");
        }else {
            System.out.println("奇数");
        }
    }
}

2.8.2 switch分支语句

2.8.2.1 格式

switch(表达式){
    case 值:
        语句;
        break;
    case 值:
        语句;
        break;
    case 值:
        语句;
        break;
    default:
        语句;
        break;
}
  • 表达式:byte、short、int、char、String
  • break:推荐使用;如果省略,那么从【符合条件的那一行】穿透
  • 如果所有条件都不满足执行default

2.8.2.2 案例

public class TestDemo {
    public static void main(String[] args){
        int key = 1;
        switch(key){
            case 0:
                System.out.println("0");
                break;
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            default:
                System.out.println("-1");
                break;
        }
    }
}

2.8.2.3 不添加break那么从符合条件的那一行开始穿透

public class TestDemo {
    public static void main(String[] args){
        int key = 1;
        switch(key){
            case 0:
                System.out.println("0");
            case 1:
                System.out.println("1");
            case 2:
                System.out.println("2");
            case 3:
                System.out.println("3");
            default:
                System.out.println("-1");
        }
    }
}
  • 笔试题:
public class TestDemo {
    public static void main(String[] args){
        int key = 1;
        int	i = 10;
        
        switch(key){
            case 0:
                i++;
            case 1:
                i+=10;
            case 2:
                i--;
            case 3:
                i++;
        }
        System.out.println("i="+i);
    }
}
  • 笔试题:输出-1,因为,default与位置无关
public class TestDemo {
    public static void main(String[] args){
        int key = 10;
        switch(key){
            default:
                System.out.println("-1");
                break;
            case 0:
                System.out.println("0");
                break;
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
        }
    }
}

2.8.3 for循环

2.8.3.1 格式

for(初始化;条件;步长){
    语句
}

2.8.3.2 案例

public class TestDemo {
    public static void main(String[] args){
        for(int i=0;i<5;i++){
            System.out.println(i);
        }
    }
}
  • 笔试题:是否可以执行?答:这是一个死循环
public class TestDemo {
    public static void main(String[] args){
        for(;;){
            System.out.println("abc");
        }
    }
}
  • 循环的顺序
public class TestDemo {
    public static void main(String[] args){
        for(int i=0;i<5;i++){
            System.out.println(i);
        }
    }
}

等价于
    
public class TestDemo {
    public static void main(String[] args){
        int i = 0;
        for(;i<5;){
            System.out.println(i);
            i++;
        }
    }
}
  • 笔试题:执行错误,因为,i变量属于for的语句块
public class TestDemo {
    public static void main(String[] args){
        for(int i=0;i<5;i++){
            i+=1;
        }
        System.out.println(i);
    }
}
  • 笔试题:书写是否正确?正确
public class TestDemo {
    public static void main(String[] args){
        for(int i=0;i<5;i++)
			System.out.println(i);
    }
}

2.8.4 while循环

2.8.4.1 格式

while(布尔类型){
    语句;
    防止死循环;
}
public class TestDemo {
    public static void main(String[] args){
        int i=0;
        while(i<5)
			System.out.println(i);
			i++;    
    }
}

2.8.5 do-while循环

  • 特点:至少循环一次
  • 格式:
public class TestDemo {
    public static void main(String[] args){
        int i=0;
        do{
            System.out.println(i);
			i++;
        }while(i<5); //必须注意结束使用分号
    }
}
  • 笔试题:输出5 5
public class TestDemo {
    public static void main(String[] args){
        int i=10,n=0;
        do{
            n++;
			i--;
        }while(n<5);
        
        System.out.println(i);
        System.out.println(n);
    }
}

2.8.6 跳出循环体

  • break:跳出当前循环体
  • continue:跳出本次循环
  • return:跳出所有循环体,因为,方法弹栈

2.8.6.1 break

public class TestDemo {
    public static void main(String[] args) {
        for(int i = 0;i < 5;i++)
            if(i==2){
                break;
            }
        	System.out.println(i);
    	}
    }
}
  • 嵌套循环
public class TestDemo {
    public static void main(String[] args) {
        for(int n = 0;n < 5;n++){
            for(int i = 0; i < 5; i++){
                if(i==2){
                	break;
            	}
                System.out.println(i);
            }
        	System.out.println("-------外层-------");
    	}
    }
}
  • 跳出所有循环
public class TestDemo {
    public static void main(String[] args) {
        //创建变量
        boolean flag = false;
        
        for (int = 0; n < 5; n++) {
            if (flag) {//跳出外层
                break;
            }
            for (int i = 0; i < 5; i++) {
                if (i==2){
                    flag = true;
                    break;
                }
                System.out.println(i);
            }
            System.out.println("-------外层-------");
        }
        
    }
}

2.8.6.2 continue

public class TestDemo {
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++) {
            if (i==2){
                continue;
            }
            System.out.println(i);
        }
    }
}

2.8.6.3 return

public class TestDemo {
    public static void main(String[] args) {
        
        for(int n = 0; n < 5; n++) {
            for (int i = 0; i < 5; i++) {
                if (i==2){
                    return; //方法弹栈
                }
                System.out.println(i);
            }
            System.out.println("-------外层-------");
        }
        
    }
}


我从未觉得繁琐,说浪漫些,我很爱你。