JavaSE---01快捷键及基础知识

**前言**:该内容为作者学习java期间所作笔记,学习参考尚硅谷系列视频。

00 快捷键


0.1 IDEA

 执行 alt+r
 提示补全  alt+/
 单选注释 ctrl + /
 多行注释 ctrl + shift + /
 向下复制一行 ctrl+alt+down
 删除一行或选中行 ctrl+d
 向下移动行 alt+down
 向上移动行 alt+up
 向下开始新的一行 shift+enter
 向上开始新的一行 ctrl+shift+enter
 万能解错/生成返回值变量 alt + enter **
 生成 try-catch 等 alt+shift+z **
 如何查看源码 ctrl + 选中指定的结构 或 ctrl + shift + t **
 生成构造/get/set/toString alt +shift + s
 退回到前一个编辑的页面 alt + left 
 进入到下一个编辑的页面 alt + right
 查看类继承关系 F4
 格式化代码 ctrl+shift+F
 提示方法参数类型 ctrl+alt+/
 复制代码 ctrl + c
 撤销 ctrl + z
 反撤销 ctrl + y
 剪切 ctrl + x 
 粘贴 ctrl + v
 保存 ctrl + s
 全选 ctrl + a
 选中数行,整体往后移动 tab
 选中数行,整体往前移动 shift + tab
 查看类的结构:类似于 eclipse 的 outline ctrl+o
 修改变量名与方法名 alt+shift+r
 大写转小写/小写转大写 ctrl+shift+y
 查看文档说明 F2
 收起所有的方法 alt + shift + c
 打开所有方法 alt+shift+x
 打开代码所在硬盘文件夹 ctrl+shift+x
 局部变量抽取为成员变量 alt+shift+F
 查找/替换(当前) ctrl+f
 查找 ctrl+h
 查找文件 double Shift
 抽取方法 alt+shift+m
 全屏显示代码 ctrl + m
 打开 Module setting 窗口 ctrl+E
 关闭当前打开的代码栏 ctrl + w
 关闭打开的所有代码栏 ctrl + shift + w
 快速搜索类中的错误 ctrl + shift + Q
 选择要粘贴的内容 ctrl+shift+V
 查找方法在哪里被调用 ctrl+shift+H
 查看方法的多层重写结构 ctrl+alt+h

01 基础知识


1.1 Java中的名称规范

  • 包名:多单词组成时所有字母都小写:xxxyyyzzz

  • 类名、接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz

  • 变量名、方法名:多单词组成时,第一个单词首字母小写,第二个单词开始每个单词首字母大写:xxxYyyZzz

  • 常量名:所有字母都大写。多单词时每个单词用下划线连接:XXX_YYY_ZZZ

1.2 数据类型

1.2.1 数据类型分类

基本数据类型

  • 整型: byte(1字节) \ short(2字节) \ int(4字节) \long(8字节)[声明long型变量,必须以"l"或"L"结尾]

  • 浮点型: float(4) \ double(8)[声明float型变量需要以"f"或"F"结尾,通常用double]

  • 字符型: char(1字符=2字节)[用' '括起来,内部只能写一个字符或转义符如'\n']

  • 布尔型: boolean[只能为true或false]

引用数据类型

  • 类(class)

  • 接口(interface)

  • 数组(array)

1.2.2 基本数据类型运算规则

自动类型转换

  • 容量小的类型自动转换为容量大的数据类型。数据类型按容量大小排序为:

    img

  • byte,short,char之间不会相互转换,他们三者在计算时首先转换为int类型。

  • byte+byte也要转换成int。

强制类型转换

  • 将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符:(),但可能造成精度降低或溢出,格外要注意。

1.2.3 字符串类型:String

  • String不是基本数据类型,属于引用数据类型

  • 声明String类型变量时,使用一对""

  • String可以和8种基本数据类型变量做运算,且运算只能是连接运算: +

  • 看+是连接还是相加,就看连接的两部分是否有字符串

练习1

 char a = 'a';
 int num = 10;
 String str = "hello";
 System.out.println(a + num + str);//107hello
 System.out.println(a + str + num);//ahello10
 System.out.println(a + (num + str));//a10hello
 System.out.println(str + num + a);//hello10a

练习2

 //输出*   *
 System.out.println("*   *");
 System.out.println('*' + '\t' + '*');
 System.out.println('*' + "\t" + '*');
 System.out.println('*' + '\t' + "*");
 System.out.println('*' + ('\t' + "*"));

运行结果

 

1.3 运算符

1.3.1 逻辑运算符

  • &—逻辑与 左边无论真假,右边都进行运算

  • |—逻辑或 同理

  • —逻辑非

  • && —短路与 如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算

  • ||—短路或

  • ^ —逻辑异或

1.3.2 赋值运算符

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

  • 用赋值运算符做运算不会改变本身数据类型,如下:

 short s1 = 10;
 //s1 = s1 + 2;  //编译失败,因为2是int,不能将int转成short
 s1 += 2; //结论:不会改变变量本身的数据类型
 System.out.println(s1);  //可以正常运行
 ​
 int n = 10;
 n += (n++) + (++n);
 System.out.println(n);  //32

1.3.3 三元运算符

 

1.4 Scanner获取键盘输入

具体步骤:

  1. 导包:import java.util.Scanner

  2. Scanner的实例化:Scanner scan = new Scanner(System.in)

  3. 调用Scanner类的相关方法,来获取指定的变量(不包含char)。scan.nextXxx() 特别的,字符串输入scan.next()

 Scanner scan = new Scanner(System.in);
 int num = scan.nextInt();

1.5 switch-case

  • 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,进入相应case结构中,执行相关语句。 当调用完执行语句后,则仍然继续向下执行其他case语句,直到遇到break关键字或末尾结束为止

  • switch结构中的表达式,只能是如下的六种数据类型之一:byte、short、char、int、枚举类型(JDK5.0)、String类型(JDK7.0)

  • case 之后只能声明常量,不能声明范围

 

练习

 /*
 对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
 说明:如果switch-case语句中多个相同语句,可以进行合并。因为没break所以顺着执行.
 */
 //方案一
 switch(score / 10){
     case 0:
     case 1:
     case 2:
     case 3:
     case 4:
     case 5:
         System.out.println("不合格");
         break;
     case 6:
     case 7:
     case 8:
     case 9:
     case 10:
         System.out.println("合格");
         break;
 }
 //方案二
 switch(score / 60){
     case 0:
         System.out.println("不及格");
         break;
     case 1:
         System.out.println("合格");
         break;
 }

练习2

 /*
 键盘分别输入年、月、日,判断这一天是当年的第几天注:判断一年是否是闰年的标准:
 1)可以被4整除,但不可被100整除或
 2)可以被400整除
 (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
 */
 import java.util.Scanner;
 ​
 public class DayTest {
     public static void main(String[] args) {
 ​
         Scanner scan = new Scanner(System.in);
         System.out.println("请输入year:");
         int year = scan.nextInt();
         System.out.println("请输入month:");
         int month = scan.nextInt();
         System.out.println("请输入day:");
         int day = scan.nextInt();
 ​
         int sumDays = 0;
         switch (month){
         case 12:
             sumDays += 30;
         case 11:
             sumDays += 31;
         case 10:
             sumDays += 30;
         case 9:
             sumDays += 31;
         case 8:
             sumDays += 31;
         case 7:
             sumDays += 30;
         case 6:
             sumDays += 31;
         case 5:
             sumDays += 30;
         case 4:
             sumDays += 31;
         case 3:
             if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                 sumDays += 29;
             }else {
                 sumDays += 28;
             }
         case 2:
             sumDays += 31;
         case 1:
             sumDays += day;
     }
         System.out.println(year+ "年" + month + "月" + day + "日是当年的第" + sumDays + "天" );
 ​
     }
 }

1.6 循环结构

1.6.1 for循环

 语法格式
 for(①初始化部分;②循环条件部分;④迭代部分){
             ③循环体部分;
 }
 ​
 执行过程:①-②-③-④-②-③-④-②-③-④-.....-②

练习1

 /*
 编写程序从1循环到150,并在每行打印一个值,
 另外在每个3的倍数行上打印出“foo”,
 在每个5的倍数行上打印“biz”,
 在每个7的倍数行上打印输出“baz”。
 */
 class ForTest1{
     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();
         }
     }
 }

1.6.2 while

 语法格式
 ①初始化部分
 while(②循环条件部分){
     ③循环体部分;
     ④迭代部分;
 }
 执行过程:①-②-③-④-②-③-④-②-③-④-.....-②

1.6.3 do-while

 语法格式
 ①初始化部分
 do{
     ③循环体部分;
     ④迭代部分;
 }while(②循环条件部分);
 ​
 执行过程:① - ③ - ④ - ② - ① - ③ - ④ - ... - ②
  • do-while循环至少执行一次循环体

1.6.4 嵌套循环的一个优化示例

 /*
 100000以内的所有质数
 质数:素数,只能被1和它本身整除的自然数。
 最小的质数是:2
 */
 public class PrimeNumTest {
     public static void main(String[] args) {
         //自己写的
 //        long start = System.currentTimeMillis();
 //        for(int i = 2;i <= 100000;i++){
 //            int m = 0;
 //            for(int j = 1;j < i;j++){
 //                if (i % j == 0){
 //                    m++;
 //                }
 //            }
 //            if (m == 1){
 //                System.out.println(i);
 //            }
 //        }
 ​
         //优化
         long start = System.currentTimeMillis();
 ​
         boolean flag = true;
         for(int i = 2;i <= 100000;i++){
                 for(int j = 2;j < Math.sqrt(i);j++){ //优化二:对本身是质数的自然数是有效的
                     if (i % j == 0){
                         flag = false;
                         break; //优化一:只对本身是非质数的自然数是有效的
                     }
                 }
                 if (flag == true){
                     System.out.println(i);
                 }
                 flag = true;
             }
 ​
         long end = System.currentTimeMillis();
         System.out.println("程序花费时间:" + (end - start));
         //自己:13428 示例原本13515 优化一:1216 优化二:179 优化一+优化二:55
     }
 }

1.7 break、continue和return

1.7.1 区别

适用范围作用相同点
breakswitch-case、循环结构中结束当前循环关键字后不能声明语句
continue循环结构中结束当次循环关键字后不能声明语句
 区别示例:
 for(int i = 1;i < 10;i++){
     if(i % 4 ==0){
         //break;  //123
         continue; //12356789
     }
     system.out.print(i);
 }

1.7.2 带标签的break和continue

  • 增添标签可以跳出指定循环

示例:

 lable:for(int i = 1;i <= 4;i++)
         for(int j = 1;j < 10;j++){
             if(j % 4 ==0){
                 //break;  
                 continue label; //跳出外圈循环
             }
             system.out.print(j);
         }
     }

1.7.3 return

  • 功能是结束一个方法。当一个方法执行到一个return语句时,这个方法将被结束。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值