关键字和保留字的说明(略)

标志符

  • 命名规则
  • 命名规范

变量

基础语法 - 图1

基本数据类型

整数类型

类型 占用存储空间 范围
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 不是基本数据类型,属于引用数据类型

练习:
试打印下面的字符。

  1. public class test {
  2. public static void main(String[] args){
  3. char c = 'a';
  4. int num = 10;
  5. String str = "hello";
  6. System.out.println(c + num + str);
  7. System.out.println(c + str + num);
  8. System.out.println(c + (num + str));
  9. System.out.println((c + num) + str);
  10. System.out.println(str + num + c);
  11. System.out.println("* *");
  12. System.out.println('*' + '\t' + '*');
  13. System.out.println('*' + "\t" + '*');
  14. System.out.println('*' + '\t' + "*");
  15. System.out.println('*' + ('\t' + "*"));
  16. }
  17. }
  18. 输出:
  19. 107hello
  20. ahello10
  21. a10hello
  22. 107hello
  23. hello10a
  24. * *
  25. 93
  26. * *
  27. 51*
  28. * *

进制

  • 二进制(binary),以 0b 或 0B 开头
  • 十进制(decimal)
  • 八进制(octal),以 0 开头
  • 十六进制(hex),以 0x 开头

    二进制和十进制互相转化

  • 2 转 10

    • Screen Shot 2021-01-11 at 21.41.14.png
    • 即:基础语法 - 图3
  • 10 转 2,除2取余的逆

运算符号

  • 算术运算符
  • 赋值运算符
  • 比较运算符
  • 逻辑运算符
  • 位运算符
  • 三元运算符

算术运算符

练习:
随意给出一个三位的整数,打印显示它的个位数、十位数,百位数的值。

  1. public class test {
  2. public static void main(String[] args){
  3. int num = 187;
  4. int unit = 187 % 10;
  5. int decade = 187 / 10 % 10;
  6. int hundred = 187 / 100;
  7. System.out.println("百位数:" + hundred);
  8. System.out.println("十位数:" + decade);
  9. System.out.println("个位数:" + unit);
  10. }
  11. }
  12. 百位数:1
  13. 十位数:8
  14. 个位数:7

赋值运算符

  • 扩展赋值运算符:+=,-=,*=,/=,%=

    比较运算符

    | 运算符号 | 运算 | | —- | —- | | == | 相等于 | | != | 不等于 | | < | 小于 | | > | 大于 | | <= | 小于等于 | | >= | 大于等于 | | instanceof | 检查是否为类的对象 |

逻辑运算符

  • & 逻辑与
  • && 短路与
  • | 逻辑或
  • || 短路或
  • ! 逻辑非
  • ^ 逻辑异或

    位运算符

    | 运算符 | 运算 | | —- | —- | | << | 左移 | | >> | 右移 | | >>> | 无符号右移 | | & | 与运算 | | | | 或运算 | | ^ | 异或运算 | | ~ | 取反运算 |

三元运算符

  • 条件表达式?表达式1:表达式2
    • 如果条件表达式是true:返回表达式1
    • 如果条件表达式是false:返回表达式2

程序流程控制

  • 顺序结构
  • 分支结构
  • 循环结构

顺序结构

分支结构

If - else 的例题1:
成绩为100分时候,奖励BMW;
成绩为(80,99]时,奖励iphone xs max;
当成绩[60,80]时,奖励一个 iPad;
其他时,什么奖励也没。
请从键盘输入岳小鹏的期末成绩并且加以判断。

  1. import java.util.Scanner;
  2. public class test {
  3. public static void main(String[] args){
  4. Scanner input = new Scanner(System.in);
  5. System.out.println("请输入岳小鹏的期末成绩:");
  6. int score = input.nextInt();
  7. if(score == 100){
  8. System.out.println("奖励BWM");
  9. }else if(score > 80 && score <= 99){
  10. System.out.println("奖励iphone xs max");
  11. }else if(score >= 60 && score <= 80){
  12. System.out.println("奖励一个 iPad");
  13. }else{
  14. System.out.println("什么奖励也没有");
  15. }
  16. }
  17. }

switch-case

循环结构

for 语句例题1:
编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行上打印出“””,在每个5的倍数上打印“biz”,在每个7的倍数行上打印输出“baz”

  1. public class test {
  2. public static void main(String[] args){
  3. for(int i = 1; i < 150 ; i ++ ){
  4. System.out.print(i + " ");
  5. if(i % 3 == 0){
  6. System.out.print("foo ");
  7. }
  8. if(i % 5 == 0){
  9. System.out.print("biz ");
  10. }
  11. if(i % 7 == 0 ){
  12. System.out.print("baz ");
  13. }
  14. System.out.println();
  15. }
  16. }
  17. }

While循环
do-While循环