Java入门基础

Java基础

1. 注释

  • 注释并不会被执行,是用来给写代码的人看的,书写注释是一个非常好的习惯

1.1 方法:

#单行注释
  //
#多行注释
  /* */
#文档注释
  /** */

1.2 效果示例:

image-20210725165407159

1.3 注释颜色字体的设置:

  • File --> Setting

image-20210725165756213

  • Editor --> Color Scheme --> Java -->找到对应选择进行设置

image-20210725170417889

2. 标识符

  • Java所有的组成部分都需要名字。

  • 类名、变量名以及方法名都被称为标识符。

2.1 关键字:

image-20210725170916397

2.2 注意点:

  • 所有的标识符都应该以字母(A-Z或a-z)、美元符($)、下划线(_)开始
  • 首字符之后可以是字母(A-Z或a-z)、美元符($)、下划线(_)或数字的任何字符组成
  • 不能使用关键字作为变量名或者方法名
  • 标识符是大小写敏感
  • 可以使用中文命名,但是一般不建议使用中文或拼音命名,建议使用相对应的英文命名

3. 数据类型

  • Java是强类型语言:要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用

3.1 基本类型(primitive type):

3.1.1 八大常用数据类型
public class 数据类型 {
    public static void main(String[] args) {
        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//Long类型要在数字后面加个L

        //小数;浮点数
        float num5 = 30.1F; //float类型要在数字后面加个F
        double num6 = 3.14159;

        //字符
        char name = '中'; //占两字节

        //类String不是关键字
        //String name = "中国";

        //布尔值:是非
        boolean flag = true;
        //boolean flag = false;
    }
}

image-20210725173205047

3.2 引用类型(reference type):

  • 接口
  • 数组

3.3 常见问题:

3.3.1 整数拓展:
public class 整数拓展 {
    public static void main(String[] args) {
        //进制
         int i = 10;     //十进制
         int i2 = 010;   //八进制 0
         int i3 = 0x10;  //十六进制 0x  0-9 A-F
         int i4 = 0b10;  //二进制 0b

        System.out.println(i);    //10
        System.out.println(i2);   //8
        System.out.println(i3);   //16
        System.out.println(i4);   //2

    }
}
3.3.2 浮点数拓展:
public class 浮点数拓展 {
    //银行业务怎么表示
    //BigDecimal 数字工具类
    //最好完全避免使用浮点数进行比较
    public static void main(String[] args) {
        float f = 0.1f;    //0.1
        double d = 1.0/10; //0.1

        System.out.println(f==d);  //false

        float d1 = 12345646546f;
        double d2 = d1 + 1;

        System.out.println(d1==d2);  //ture

        //浮点数 有限 离散 舍入误差 大约 接近但是不等于
    }
}
3.3.3 字符拓展:
public class 字符拓展 {
    public static void main(String[] args) {
        char c1 = 'a';
        char c2 = '中';

        System.out.println(c1);

        System.out.println((int)c1); //强制转换

        System.out.println(c2);

        System.out.println((int)c2); //强制转换

        //所有的字符本质还是数字
        //编码 Unicode表   (97 = a  65 = A)  2字节  0-65536
        //U0000-UFFFF

        char c3 = '\u0061';  //61为16进制
        System.out.println(c3);  //a
    }
}
  • 转义字符:
public class 转义字符 {
    public static void main(String[] args) {

        System.out.println("hello,world");    // hello,world

        //  \t 一个制表位,实现对齐功能
        System.out.println("hello,\tworld");  // hello,	world

        //  \n 换行符
        System.out.println("hello,\nworld");  /* hello,
                                                 world    */

        //  \\ 输出一个\
        System.out.println("hello,\\world");  // hello,\world

        //  \" 输出一个"
        System.out.println("hello,\"world");  // hello,"world

        // \' 输出一个'
        System.out.println("hello,\'world");  // hello,'world

        // \r 表示一个回车,使光标回到当前行的行首。如果之前该行有内容,则会被覆盖;
        System.out.println("hello,\rworld"); // world
    }
}
3.3.4 布尔值扩展:
public class 布尔值扩展 {
    public static void main(String[] args) {
        boolean flag = true;
        if (flag == true) { }
        if (flag) { }
        // 二者意义相同
        // less is More 代码要精简易读
    }
}

4. 类型转换

image-20210725190706225

4.1 强制转换:

public class 类型转换 {
    public static void main(String[] args) {
        //强制转换  (类型)变量名 高 -->低
        int i = 128;
        byte b = (byte)i;

        System.out.println(i); //128
        System.out.println(b); //-128 内存溢出 byte字节范围为 -128-127
    }
}

4.2 自动转换:

public class 类型转换 {
    public static void main(String[] args) {
        //自动转换     低 --> 高
        int i = 128;
        double b = i;

        System.out.println(i);  //128
        System.out.println(b);  //128.0
    }
}

4.3 注意点:

  • 不能多布尔值进行转换
  • 不能把对象类型转换为不相干的类型
  • 在把高容量转换到低容量的时候,强制转换
  • 转换的时候可能存在内存溢出或是精度问题

image-20210725192444214

public class 溢出问题 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;
        System.out.println(total); //-1474836480,计算的时候溢出了
    }
}
===========================================
public class 溢出问题 {
    public static void main(String[] args) {
        int money = 10_0000_0000;
        int years = 20;
        long total = money*years;  //默认是int,转换之前已经存在问题
        System.out.println(total); //-1474836480
    }
}
===========================================
public class 溢出问题 {
    public static void main(String[] args) {
        int money = 10_0000_0000;
        int years = 20;
        long total = money*(long)years;  //先把一个数转换成Long
        System.out.println(total); //20000000000
    }
}
    • L尽量大写,避免在编写代码过程中l1难以区分

5. 变量、变量的作用域、常量

5.1 变量

image-20210725193551181

  • 每个变量都有类型,类型可以是基本类型,也可以是引用类型
  • 变量名必须是合法的标识符
  • 变量声明是一条完整的语句,因此每一个声明都必须以分号结束

5.2变量的作用域

  • 类变量

  • 实例变量

  • 局部变量

    image-20210725194321863

    public class Variable { //变量
        //类变量
        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); // 10
    
            //变量类型 变量名字 = new Variable();
            Variable variable = new Variable();
            System.out.println(variable.age); // 0
            System.out.println(variable.name); // null
    
            //类变量 static
            System.out.println(salary); //2500.0
        }
        //其他方法
        public void add(){
            
        }
    }
    
    

5.3 常量

image-20210725195706890

  • 常量名一般使用大写字符

  • public class Constant {
        //修饰符,不存在先后顺序 final static double PI = 3.14结果相同
        static final double PI = 3.14;
        public static void main(String[] args) {
            System.out.println(PI); //3.14
        }
    }
    

5.4 命名规范

  • 所有变量、方法、类名:见名如意
  • 类成员变量:首字母小写和驼峰原则:monthSalary
  • 局部变量:首字母小写和驼峰原则
  • 常量:大写字母和下划线:MAX_VALUE
  • 类名:首字母大写和驼峰原则:Man,GoodMan
  • 方法名:首字母小写和驼峰原则:run(),runRun()

6. 运算符

#算术运算符: + , - , * , / , %(模运算--取余) , ++ , --
#赋值运算符: =
#关系运算符: > , < , >= , <= , ==(等于) , !=(不等于) , instanceof
#逻辑运算符: &&(与) , ||(或) , !(非)

image-20210725202040065

6.1 运算实例(通过结合代码理解运算符的应用)

package operator;

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); // 30
        System.out.println(a-b); // -10
        System.out.println(a*b); // 200
        System.out.println(a/(double)b); // 0.5 注意除法或产生小数

        System.out.println(c%a); // 5 模运算  取余
    }
}
=======================================================
package operator;

public class Demo02 {
    public static void main(String[] args) {

        long a = 123456456465L;
        int b = 123;
        short c = 10;
        byte d = 8;

        /*
        abcd相加,a是Long类型,容量比其他类型大,
        所以输出结果也是Long类型。同理...
         */
        System.out.println(a+b+c+d); //123456456606 Long类型
        System.out.println(b+c+d); //141 Int类型
        System.out.println(c+d); //18 Int类型
    }
}
=========================================================
package operator;

public class Demo03 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误  布尔值
        //常与if一起使用

        int a = 10;
        int b = 20;

        System.out.println(a>b); //false
        System.out.println(a<b); //true
        System.out.println(a==b); //false
        System.out.println(a!=b); //true
    }
}
=========================================================
package operator;

public class Demo04 {
    public static void main(String[] args) {
        // ++ 自增  --  自减  一元运算符
        int a = 3;

        int b = a++; //执行完这行代码后,先给b赋值,再自增
        // a++  a = a + 1

        System.out.println(a); // 4
        int c = ++a; //执行完这行代码前,先自增,再给c赋值

        System.out.println(a); // 5
        System.out.println(b); // 3
        System.out.println(c); // 5

        //幂运算 2^3 2*2*2 = 8 很多运算会使用一些工具类来操作!
        double pow = Math.pow(2,3);
        System.out.println(pow); //8.0
    }
}
========================================================
package operator;

// 逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        // 与(and)  或(or)  非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a&&b)); // false  逻辑与运算,两个变量都为真,结果才为true
        System.out.println("a || b:"+(a||b)); // true   逻辑或运算,两个变量都为假,结果才为false
        System.out.println("!(a && b):"+!(a&&b));// true  如果是真,则变为假;反之则反之
    }
}
========================================================
package operator;
//位运算
public class Demo06 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

      A&B = 0000 1100 有0则为0
      A|B = 0011 1101 有1则为1
      A^B = 0011 0001 相同为0,不同为1
       -B = 1111 0010

    2*8怎么运算最快
    2*8 = 16 2*2*2*2
    效率极高
    <<  左移 *2
    >>  右移 /2

    0000 0000    0
    0000 0001    1
    0000 0010    2
    0000 0011    3
    0000 0100    4
    0000 1000    8
    0001 0000    16
     */
        System.out.println(2<<3); //16

    }
}
============================================================
package operator;
//扩展赋值运算符
public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 25;

        a+=b; // a=a+b
        a-=b; // a=a-b
        System.out.println(a);

        //字符串连接符 + , String
        System.out.println(""+a+b);// 1025   ""为字符串类型,会将ab都转换成String类型进行连接

        System.out.println(a+b+"");// 35     字符串在后,a+b会先进行运算
    }
}
==============================================================
package operator;

//三元运算符 必须掌握
public class Demo08 {
    public static void main(String[] args) {
        //  x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 50;
        String type = score < 60 ?"不及格":"及格";
        System.out.println(type); // 不及格
    }
}

6.2 优先级

  • ()的优先级最高,所以学会去利用()辅助进行优先级管理

7. 包机制

  • 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间(包的本质就是文件夹)

  • 包语句的句法格式

image-20210725215819040

  • 一般利用公司域名倒置作为包名:com.baidu.www
  • 解决创建多级包时,不分层显示的问题

image-20210725215254762

将红色框内容去掉勾选即可

  • 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包,使用"import"语句可完成此功能 Alt+Enter

image-20210725215841420

import com.xxx.xxx.*;//导入这个包下所有的类

8. JavaDoc

  • javadoc命令是用来生成自己API文档的

    JDK8帮助文档

  • 参数信息

    • @author 作者名
    • @version 版本号
    • @since 指明需要最早使用的jdk版本
    • @param 参数名
    • @return 返回值情况
    • @throws 异常抛出情况
    package com.kai.base;/** * @author wang * @version 1.0 * @since 1.8 */public class Doc {    /**     *      * @param name     * @return     * @throws Exception     */    public String test(String name) throws Exception{        return name;    }}
    
  • 通过命令行 -->javadoc -encoding UTF -8 -charset UTF-8 Doc.java -->产生javaDoc文档

image-20210725232537741

image-20210725232604015

  • 使用IDEA生成JavaDoc文档

image-20210725233711515

image-20210725233742903

点击OK会自动弹出网页

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值