关键字和保留字的说明(略)
标志符
- 命名规则
- 命名规范
变量
基本数据类型
整数类型
| 类型 | 占用存储空间 | 范围 |
|---|---|---|
| byte | 1字节 | -128~127 |
| short | 2字节 | -2^15~2^15-1 |
| int | 4字节 | -2^31~2^31-1 |
| long | 8字节 | -2^63~2^63-1 |
浮点类型
| 类型 | 占用存储空间 | 范围 |
|---|---|---|
| float | 4字节 | -3.403E38~3.403E38 |
| double | 8字节 | -1.798E308~1.798E308 |
字符型
| 类型 | 占用存储空间 |
|---|---|
| char | 2字节 |
布尔型
只取:true | false
类型的转换
- 自动类型转换
- byte,short,char -> int -> long -> float -> double
- 强制类型转换
字符串类型
- String 不是基本数据类型,属于引用数据类型
练习:
试打印下面的字符。
public class test {public static void main(String[] args){char c = 'a';int num = 10;String str = "hello";System.out.println(c + num + str);System.out.println(c + str + num);System.out.println(c + (num + str));System.out.println((c + num) + str);System.out.println(str + num + c);System.out.println("* *");System.out.println('*' + '\t' + '*');System.out.println('*' + "\t" + '*');System.out.println('*' + '\t' + "*");System.out.println('*' + ('\t' + "*"));}}输出:107helloahello10a10hello107hellohello10a* *93* *51** *
进制
运算符号
- 算术运算符
- 赋值运算符
- 比较运算符
- 逻辑运算符
- 位运算符
- 三元运算符
算术运算符
练习:
随意给出一个三位的整数,打印显示它的个位数、十位数,百位数的值。
public class test {public static void main(String[] args){int num = 187;int unit = 187 % 10;int decade = 187 / 10 % 10;int hundred = 187 / 100;System.out.println("百位数:" + hundred);System.out.println("十位数:" + decade);System.out.println("个位数:" + unit);}}百位数:1十位数:8个位数:7
赋值运算符
- 扩展赋值运算符:+=,-=,*=,/=,%=
比较运算符
| 运算符号 | 运算 | | —- | —- | | == | 相等于 | | != | 不等于 | | < | 小于 | | > | 大于 | | <= | 小于等于 | | >= | 大于等于 | | instanceof | 检查是否为类的对象 |
逻辑运算符
- & 逻辑与
- && 短路与
- | 逻辑或
- || 短路或
- ! 逻辑非
- ^ 逻辑异或
位运算符
| 运算符 | 运算 | | —- | —- | | << | 左移 | | >> | 右移 | | >>> | 无符号右移 | | & | 与运算 | | | | 或运算 | | ^ | 异或运算 | | ~ | 取反运算 |
三元运算符
- 条件表达式?表达式1:表达式2
- 如果条件表达式是true:返回表达式1
- 如果条件表达式是false:返回表达式2
程序流程控制
- 顺序结构
- 分支结构
- 循环结构
顺序结构
分支结构
If - else 的例题1:
成绩为100分时候,奖励BMW;
成绩为(80,99]时,奖励iphone xs max;
当成绩[60,80]时,奖励一个 iPad;
其他时,什么奖励也没。
请从键盘输入岳小鹏的期末成绩并且加以判断。
import java.util.Scanner;public class test {public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("请输入岳小鹏的期末成绩:");int score = input.nextInt();if(score == 100){System.out.println("奖励BWM");}else if(score > 80 && score <= 99){System.out.println("奖励iphone xs max");}else if(score >= 60 && score <= 80){System.out.println("奖励一个 iPad");}else{System.out.println("什么奖励也没有");}}}
switch-case
循环结构
for 语句例题1:
编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行上打印出“””,在每个5的倍数上打印“biz”,在每个7的倍数行上打印输出“baz”
public class test {public static void main(String[] args){for(int i = 1; i < 150 ; i ++ ){System.out.print(i + " ");if(i % 3 == 0){System.out.print("foo ");}if(i % 5 == 0){System.out.print("biz ");}if(i % 7 == 0 ){System.out.print("baz ");}System.out.println();}}}
While循环
do-While循环


