1.数据类型转换
Java程序中要求参与的计算的数据,必须要保证数据类型的一致性,如果数据类型不一致将发生类型的转换。
分为:自动类型转换 和 强制类型转换
自动类型转换:将 取值范围小的类型 自动提升为 取值范围大的类型 。与字节数无关
long num1 = 100;
此时就发生了自动类型转换 因为100所属int类型 小于 long类型的取值范围 所以100变为了long类型
float num2 = 200L; System.out.println(num2);
此时200L所属的长整形取值范围小于float类型 所以发生了自动类型转换 输出值为 200.0
强制类型转换:格式 - - > 范围小的类型 范围小的变量名 = (范围小的类型) 原本范围大的数据
int num3 = (int) 300L; System.out.println(num3);
此时300L的取值范围是大于int 的 但可以通过强制类型转换来实现
注意事项:
1.强制类型转换一般不推荐使用,因为有可能造成数据损失精度(double 转 int)、数据溢出 如:int num2 = (int) 3000000000L;
2.byte/short/char这三种类型都可以发生数学运算,例如加法‘+’
3.byte/short/char三种类型来说,如果右侧赋值的数值没有超过左侧的数据类型的范围,那么javac编译器会为我们自动加上类型转换 如: char str = 65; 此时输出值为A
char zifu = 'A'; System.out.println(zifu + 1);
此时输出结果为66 因为A在二进制码里表示为65
3.byte/short/char这三种类型在运算的时候,都会被首先提升成为int类型,然后再计算
byte a = 30; byte b = 40; byte result1 = a + b; System.out.println(result1)
此时会报错 因为 a和b会被首先提升为int类型 所以result接收时 要用int类型接收
byte b = 40; short c = 60; short result2 = (short)(c + b) //右边的实际范围不要超过左边数据类型的范围!!!否则会溢出
ASCII码
‘0’ - - - 48
‘A’ - - - 65
‘a’ - - - 97
自增自减运算符: ++ - -
在混合运算时 前加加是先加后用 后加加是先用后加 如:
public class DemoType{ public static void main(String[] args){ int num = 10; System.out.println(++num); int num2 = 20; System.out.println(num2++); } }
此时输出值为11和20
public class DemoType{ public static void main(String[] args){ int num1 = 50; int result1 = --num1; //先减后用于赋值运算 System.out.println(result1); int num2 = 30; int result2 = num2--; //先用于赋值运算再减 System.out.println(result2); } }
此时输出结果为49和30
常量不可使用自增或自减:如: int num1 = 50++; 常量是不可变的!!
赋值运算符中, += -= *= 等内部会进行强制类型转换 如 int num = 5; num += 2,5F; 此时 输出的数据类型依旧是int类型!!
2.逻辑运算符
与(并且) && 两者都会发生短路 不执行后面的运算
或(或者) | | 两者都会发生短路
非(取反)!
对于 1< x <3 应该这么写 x>1 && x > 3;
3.三元运算符
一元运算符: ! ++ - -
二元运算符: + - * / =
三元运算符:
格式: 数据类型 变量名称 = 条件判断 ? 表达式A:表达式B;
流程:
首先判断条件是否成立:如果成立为true,那么将表达式A的值赋值给左侧的变量;如果不成立为false,将表达式B的值赋给左侧变量
public class DemoType{ public static void main(String[] args){ int i = 5; int a = 7; int max = i < a ? a : i ; System.out.println(max); } }
此时 i < a 所以取a max的值为7
注意事项:1.必须 同时保证表达式A和表达式B都符合左侧数据类型的要求!!!
2.三元运算符的结果必须被使用 要么交给变量 要么直接打印
4.方法
方法:就是将一个功能抽取出来,把代码单独定义在一个大括号内,形成一个单独的功能。
当我们需要这个功能的时候,就可以去调用。这样即实现了代码的复用性,也解决了代码冗余的现象。
格式:public static void 方法名称 (){
方法体
}
定义格式解释:
修饰符: 目前固定写法 public static 。
返回值类型: 目前固定写法 void ,其他返回值类型在后面的课程讲解。
方法名:为我们定义的方法起名,满足标识符的规范,用来调用方法。
参数列表: 目前无参数, 带有参数的方法在后面的课程讲解。
return:方法结束。因为返回值类型是void,方法大括号内的return可以不写。
public class DemoType{ public static void main(String[] args){ farmer(); buyer(); } public static void farmer(){ System.out.println("种草"); System.out.println("收割"); } public static void buyer(){ System.out.println("买东西"); System.out.println("吃东西"); } }
5.JShell脚本工具是JDK9的新特性
什么时候会用到 JShell 工具呢,当我们编写的代码非常少的时候,而又不愿意编写类,main方法,也不愿意去编译和运
行,这个时候可以使用JShell工具。
启动JShell工具,在DOS命令行直接输入JShell命令。
退出方法: /exit
拓展:
public static void main(String[] args){ byte b1=1; byte b2=2; byte b3=1 + 2; byte b4=b1 + b2; System.out.println(b3); System.out.println(b4); }
分析: b3 = 1 + 2 , 1 和 2 是常量,为固定不变的数据,在编译的时候(编译器javac),已经确定了 1+2 的结果并没
有超过byte类型的取值范围,可以赋值给变量 b3 ,因此 b3=1 + 2 是正确的。
反之, b4 = b2 + b3 , b2 和 b3 是变量,变量的值是可能变化的,在编译的时候,编译器javac不确定b2+b3的结果是什
么,因此会将结果以int类型进行处理,所以int类型不能赋值给byte类型,因此编译失败。
6.流程控制
if语句第一种格式:if...
if(关系表达式){
语句体;
}
if语句第二种格式: if...else
if(关系表达式) {
语句体1;
}
else {
语句体2;
}
if语句第三种格式: if...else if ...else
if (判断条件1) { 执行语句1; } else if (判断条件2) { 执行语句2; } ... }else if (判断条件n) { 执行语句n; } else { 执行语句n+1; }
选择语句--switch
switch语句格式:
switch(表达式) { case 常量值1: 语句体1; break; case 常量值2: 语句体2; break; ... default: 语句体n+1; break;
}
执行流程:
1.首先计算出表达式的值,其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束。
2.最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。
注意事项:
switch后面的小括号只能是以下数据类型:
1.基本数据类型:byteshortcharint
2.引用数据类型:String字符串、enum枚举
穿透现象:
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运
行,直到遇到break,或者整体switch结束。
public static void main(String[] args) { int i = 5; switch (i){ case 0: System.out.println("执行case0"); break; case 5: System.out.println("执行case5"); case 10: System.out.println("执行case10"); default: System.out.println("执行default"); } }
上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接运行完整体switch。
7.for循环
for循环语句格式:
for(初始化表达式①; 布尔表达式②; 步进表达式④){ 循环体③ }
public class DemoType{ public static void main(String[] args){ for (int i = 1; i <= 10; i++){ System.out.println("我是农民" + i); } } }
8.while循环
初始化表达式① while(布尔表达式②){ 循环体③ 步进表达式④ }
public class DemoType{ public static void main(String[] args){ int i = 1; while (i <= 10){ System.out.println("我是农民" ); i++; } } }
9.do-while循环
public class DemoType{ public static void main(String[] args){ int i = 1; do{ System.out.println("我是农民" ); i++; }while (i <= 10); } }
用循环写出1-100以内的所有偶数:
三种方法: for循环: public class DemoType{ public static void main(String[] args){ int sum = 0; for (int i =1;i <= 100; i++){ if (i % 2 == 0){ sum += i; } } System.out.println(sum); } } while循环: public class DemoType{ public static void main(String[] args){ int sum = 0; int i = 1; while (i <= 100){ if (i % 2 == 0){ sum += i; } i++; } System.out.println(sum); } } do while循环: public class DemoType{ public static void main(String[] args){ int sum = 0; int i = 1;do{ if (i % 2 == 0){ sum += i; } i++; }while(i <= 100); System.out.println(sum); } }
break和continue:
public class DemoType{ public static void main(String[] args){ for (int i = 1; i <= 10; i++){ if (i == 4){ continue; } System.out.println(i + "楼到了"); } } }
循环嵌套:
public class DemoType{ public static void main(String[] args){ for (int i = 1; i <= 30; i++){ for (int j = 1; j <= 24;j++){ System.out.println("第" + i + "天" + "第" + j + "小时" ); } } } }