Android基础Java 基础

Java 基础 01. Java 基础语法

2021-11-29  本文已影响0人  yjtuuige

一、计算机基础

1. 打开 CMD 的方式

  1. 开始 > Windows 系统 > 命令提示符。
  2. Win + R 输入 CMD 打开控制台 (推荐使用)。
  3. 在任意文件夹下,按住 Shift 键 + 鼠标右键 打开命令行窗口。
  4. 在资源管理器的地址栏前加上 CMD 路径。

2.管理员身份运行方式

3.常见的Dos命令

  1. 盘符切换:英文(盘符 + 冒号)

    Microsoft Windows [版本 10.0.19043.1348]
    (c) Microsoft Corporation。保留所有权利。
    C:\Users\xinzh>d:
    D:\>
    
  2. 查看当前盘符下的全部目录:dir

    D:\>dir
     驱动器 D 中的卷是 工作台
     卷的序列号是 1718-20F6
     D:\ 的目录
    2021/05/17  16:15    <DIR>          BaiduNetdiskDownload
    2021/05/15  19:50    <DIR>          CloudMusic
    2021/05/14  21:19    <DIR>          Dev-Cpp
    2021/05/14  16:56    <DIR>          DTL8Folder
    2021/05/14  22:35    <DIR>          HBuilderX
    2021/05/14  22:36    <DIR>          java
    2021/05/14  17:00    <DIR>          MyDrivers
    2021/05/14  22:47    <DIR>          Notepad++
    2021/05/14  22:29    <DIR>          Sublime Text 3
    2021/05/14  18:56    <DIR>          Typora
    2021/05/17  17:53    <DIR>          VCW
    2021/05/14  21:25    <DIR>          VS2017
    2021/05/14  21:47    <DIR>          Windows Kits
                   0 个文件              0 字节
                  13 个目录 663,783,088,128 可用字节
    
  3. 切换目录:cd change directory

    E:\>cd /d d:
    d:\>cd /d d:\leven  // '/d'可实现进入跨盘符目录切换
    d:\LEVEN>cd ..
    d:\>
    
  4. 返回上一级:cd ..

  5. 清理屏幕: cls

  6. 退出终端:exit

  7. 查看电脑IP信息:ipconfig

  8. 打开计算器:calc

  9. 打开画图:mspaint

  10. 打开记事本:notepad

  11. ping 命令:ping 网址

  12. 文件操作

    C:\Users\xinzh\Desktop>md test
    C:\Users\xinzh\Desktop>cd test
    C:\Users\xinzh\Desktop\test>cd>a.txt
    C:\Users\xinzh\Desktop\test>
    
  13. 删除文件

    C:\Users\xinzh\Desktop\test>del a.txt
    C:\Users\xinzh\Desktop\test>cd ..
    C:\Users\xinzh\Desktop>rd test
    C:\Users\xinzh\Desktop>
    

二、Java 基础

1. 简介及安装、配置

2. 编写第一个 Java 程序

  1. 新建 Hello.java 文件,内容如下:

    public class HelloWorld{
        public static void main(String[] args){
            System.out.println("Hello,world!"); // 输出 Hello,wrold!
        }
    }
    
  2. 编译 java 文件:在命令行窗口输入 javac HelloWorld.java ,回车键编译代码,生成 HelloWorld.class 文件

  1. 运行 class 文件:输入 java HelloWorld ,回车键运行程序

可能遇到错误的情况

  1. 单词大小写,Java 区分大小写
  2. 尽量使用英文
  3. 文件名与类名必须保持一致,且首字母要大写
  4. 符号中不能出现中文

3. Java 三种注释方式

  1. 单行注释 // , idea 快捷键 Ctrl + / idea 常用快捷键

    public class HelloWorld {
        public static void main(String[] args) {
            // 单行注释,输出 Hello,wrold!
            System.out.println("Hello,world!");
        }
    }
    
  2. 多行注释 /* */ , idea 快捷键 Ctrl + Shift + /

    public class HelloWorld {
        public static void main(String[] args) {
           /*
            多行注释
             */
            System.out.println("Hello,world!");
        }
    }
    
  3. 文档注释:JavaDoc /** */

/**
 * @Description HelloWorld
 * @Author LIU
*/
public class HelloWorld {
    public static void main(String[] args) {
        // 单行注释,输出 Hello,wrold!
        System.out.println("Hello,world!");
    }
}

4. 标识符与关键字

  1. 标识符
    Java 所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。
    • 所有的标识符都应该以字母 A-Z 或者 a-z,美元符 $,或者下划线 _ 开始;
    • 首字符之后可以是字母 A-Z 或者 a-z 美元符 $ 、下划线 _ 或数字的任何字符组合;
    • 不能使用关键字作为变量名或方法名;
    • 标识符是大小写敏感的;
    • 可以使用中文命名,但是一般不建议这样去使用,也不建议使用拼音。
    • 合法标识符举例:age、$salary、_value、__1_value
    • 非法标识符举例:123abc、-salary
/**
 * @author Liu
 * @create 2021-11-26 11:38
 */
public class Demo01 {
    public static void main(String[] args) {
        String Ahello = "Liu";
        String hello = "Liu";
        String $hello = "Liu";
        String _hello = "Liu";
        String _vvhh = "Liu";
        String 变量 = "Liu";      // 不建议这样去使用
    }
}
  1. Java 关键字 点击查看

5. 数据类型

  1. 语言类型分类
    • 强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用。
    • 弱类型语言
  2. Java 数据类型基本分类:基本类型和引用类型
public class Demo02 {
    public static void main(String[] args) {
        // 八大基本数据类型
        // 整数
        int num1 = 10;  // 最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 40L; // Long 类型要在数字后面加 L
        // 小数:浮点数
        float num5 = 50.1F; // float 类型要在数字后面加 F
        double num6 = 3.1415926534546246455;
        // 字符类型
        char name = '中';
        // 字符串,String 不是关键字,是类
        String names = "Liu";
        // 布尔值
        boolean flag = true;
        boolean flag2 = false;
    }
}

数据类型扩展知识

public class Demo03 {
    public static void main(String[] args) {
        // 整数拓展:    进制      二进制0b   十进制     八进制0    十六进制0x
        int i=10;
        int i2 = 010;   // 八进制0
        int i3 = 0x10;  // 十六进制0x
        System.out.println(i);  // 10
        System.out.println(i2); // 8
        System.out.println(i3); // 16
        System.out.println("-------------------------------------");
        //************************************************
        // 浮点数拓展
        // BigDecimal   数学工具类
        // float 有限     离散      舍入误差    大约  接近但不等于
        // 最好完全避免使用浮点数进行比较
        float f = 0.1f; // 0.1
        double d = 1.0/10;  // 0.1
        System.out.println(f==d);   // false
        float d1 = 2425444564215654564f;
        float d2 = d1 + 1;
        System.out.println(d1==d2); // true
        System.out.println("-------------------------------------");
        //************************************************
        // 字符拓展
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1);    // 强制转换
        System.out.println(c2);
        System.out.println((int)c2);    // 强制转换
        // 所有字符的本质还是数字
        // 编码 Unicode   2字节    0 - 65536   Excel
        char c3 = '\u0061';
        System.out.println(c3); // a
        // 转义字符
        // \t   制表符
        // \n   换行
        // ......
        System.out.println("hello\tworld!");
        System.out.println("hello\nworld!");
        System.out.println("-------------------------------------");
        //
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);     // false
        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);     // true
        // 布尔值扩展
        boolean flag = true;
        if(flag==true){}    // 新手
        if(flag){}      // 老油条
        // 代码要精简易读
    }
}

6. 类型转换

常见问题

public class Demo05 {
    public static void main(String[] args) {
        // 操作比较大时,注意溢出
        // JDK7特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);
        int years = 20;
        int total = money*years;    //  -1474836480  计算时溢出
        System.out.println(total);
        long total2 = money*years;
        System.out.println(total2);    // 默认是int,转换之前已经存在问题了!!!
        long total3 = money*(long)years;
        System.out.println(total3);     // 20000000000
    }
}

7. 变量与常量

  1. 变量:可以变化的量。
    • Java 是一种强类型语言,每个变量都必须声明其类型。
    • Java 变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
    • 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
    • 变量名必须是合法的标识符。
    • 变量声明是一条完整的语句,因此每一个声明都必须以分号结束。
     public class Demo06 {
         static int allClicks = 0;   // 类变量
         String str = "hello world"; // 实例变量
         public void method(){
             int i = 0;  // 局部变量
         }
         public static void main(String[] args) {
             // int a,b,c;
             // int a=1,b=2,c=3;
             String name = "Liu";
             char x = 'X';
             double pi = 3.14;
         }
     }
    
    public class Demo07 {
        // 类变量 static
        static double salary = 2500;
        // 属性:变量
        // 实例变量:从属于对象:实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
        // 布尔值:默认是 faLse
        // 除了基本类型,其余的都是null
        String name;
        int age;
        // main 方法
        public static void main(String[] args) {
            // 局部变量:必须声明和初始化值
            int i = 10;
            System.out.println(i);
            // 变量类型 变量名字 = new Demo08();
            Demo07 demo07 = new Demo07();
            System.out.println(demo07.age);
            System.out.println(demo07.name);
            // 类变量 static
            System.out.println(salary);
        }
        // 其他方法
        public void add(int i){
            System.out.println(i);
        }
    }
    
  2. 常量( Constant):初始化( initialize)后不能再改变值!不会变动的值;
    • 所谓常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
    • 常量名一般使用大写字符。
    public class Demo08 {
        // 修饰符,不存在先后顺序
        static final double PI = 3.14;
        public static void main(String[] args) {
            System.out.println(PI);
        }
    }
    
  3. 变量的命名规范
    • 所有变量、方法、类名:见名知意;
    • 类成员变量:首字母小写和驼峰原则:monthSalary;
    • 局部变量:首字母小写和驼峰原则;
    • 常量:大写字母和下划线:MAX_VALUE;
    • 类名:首字母大写和驼峰原则:Man, GoodMan;
    • 方法名:首字母小写和驼峰原则:run(),runRun()。

8. 基本运算符

Java 语言支持如下运算符:

package github.demo01;

public class Demo01 {
    public static void main(String[] args) {
        // 二元运算符
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);
        System.out.println(a/(double)b);
    }
}
package github.demo01;

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);
        System.out.println(b+c+d);
        System.out.println(c+d);
    }
}
package github.demo01;

public class Demo03 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d);
        System.out.println(b+c+d);
        System.out.println(c+d);
        System.out.println((double)c+d);
    }
}
package github.demo01;

public class Demo04 {
    public static void main(String[] args) {
        // 关系运算符返回的结果
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(c%a);    // c / a 的余数
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

9. 自增自减运算符、初识 Math 类

package github.demo01;

public class Demo05 {
    public static void main(String[] args) {
        // ++ -- 自增,自减  一元运算符
        int a = 3;
        int b = a++;    // 执行完这一行代码后,先给b赋值,再自增
        System.out.println(a);
        int c = ++a;    // 执行完这一行代码前,先自增,再给c赋值
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        // 幂运算 2^3
        double pow = Math.pow(2,3);
        System.out.println(pow);
    }
}

10. 逻辑运算符、位运算符

package github.demo01;

public class Demo06 {
    // 逻辑运算符
    public static void main(String[] args) {
        // 与(and)一假则假   或(or)一真则真   非(取反)
        boolean a = true;
        boolean b = false;
        System.out.println("a && b:" + (a && b));   // 逻辑与:两个变量都为真,结果才为true
        System.out.println("a || b:" + (a || b));   // 逻辑或:两个变量有一个为真,则结果才为true
        System.out.println("!(a && b):" + !(a && b));   // 取反:如果是真,则变为假;如果是假,则变为真
        // 短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}
package github.demo01;

public class Demo07 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101
----------------------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B = 1111 0010
        2*8 = 16
        << 左移一位等同于 *2
        >> 右移一位等同于 /2
         */
        System.out.println(2<<3);
    }
}

11. 三元运算符

package github.demo01;

public class Demo08 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a += b; //  a = a+b
        a -= b; //  a = a-b
        System.out.println(a);
        // 字符串连接      + String
        System.out.println(""+a+b); // 1020   直接转换为字符串
        System.out.println(a+b+""); // 30     先计算 a+b 再转成字符串
    }
}
package github.demo01;

public class Demo08 {
    public static void main(String[] args) {
        // 三元运算符
        // x ? y : z
        // 如果x==true,则结果为y,否则结果为z
        int score = 80;
        String type = score < 60 ? "不及格" : "及格";
        System.out.println(type);
    }
}

12. 包机制

13. JavaDoc

package github.demo01;

public class Doc {
    String name;
    /**
     * @author 
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
}
javadoc -encoding UTF-8 -charset UTF-8 Doc.java    // 命令行生成 java 文档
上一篇下一篇

猜你喜欢

热点阅读