Java入门知识梳理02

时间:2021-08-30 09:05:48   收藏:0   阅读:11

Java标识符

所有的标识符都需要用字母(大小写),美元符号($),下划线(-)开始

之后可以是字母,美元符号,下划线,数字

不能使用关键字作为变量名或方法名

标识符是大小写敏感的

标识符可以用中文来命名,但是不推荐,因为不规范,而且很low

数据类型

byte 1 short 2 int 4 (最常用)long 8(在数字后加L) float 4 (在数字后面加F)double 8

整数扩展

进制 二进制 0b 十进制 八进制0 十六进制0x


银行业务怎么表示?float 有限 离散 舍入误差 BigDecimal

字符扩展

强制转换

所有的字符本质还是数字

编码 Unicode 2字节 65536 Excel 2^16 = 65536

a = 97 A = 65 U0000 UFFFF

转义字符

//转义字符

// \t 制表符

// \n 换行

布尔扩展

Blloean flag = true;

if(flag == true){}

if(falg){}

public class HelloWorld {
    public static void main(String[] args) {
        String sa = new String("Helloworld");
        String sb = new String("Helloworld");
        System.out.println(sa == sb);
        String sc = "Helloworld";
        String sd = "Helloworld";
        System.out.println(sc == sd);
    }

}

此程序跑完的两个判断语句 第一句为false 第二句为true 原因涉及到底层

类型转换

int i = 128 ;

Byte b = (byte) i ; 会出错

强制类型转换 高--低

自动类型转换 低--高


不能对布尔值进行转换

不能吧对象类型转换为不相干的类型

在吧高容量类型转换到低容量时,需要强制类型转换

转换的时候会出现内存溢出或者精度问题


public class HelloWorld {
    public static void main(String[] args) {
       int money = 10_0000_0000;
       int years  = 20 ;
       int total = money * years ;
       long total2 = money * years ;
        System.out.println(total);
        System.out.println(total2);
    }

}

可以在数字之间加上下划线进行分割

以上程序在进行计算时会溢出,导致结果出现问题

操作较大数时要注意溢出问题

变量

就是值可以变化的量!变量代表一块空间,空间里的东西是多变的多样的

强类型。一定要明确他的类型

类变量

实例变量

局部变量 (生命周期在语句块中)

public class  Demo08 {
    String name ;
    int age ;
    char i ;

    public static void main(String[] args) {
        Demo08 demo08 = new Demo08();
        System.out.println(demo08 . age);
        System.out.println(demo08 . name);
        


    }

}

Name age 为实例变量 可以只申明而不用初始化 在使用的时候用方法调用

属性 :变量 mian :方法

public class  Demo08 {
    static double salary = 2500 ;
    public static void main(String[] args) {
        System.out.println("===========");
        System.out.println(salary);

    }
}

常量

值不会变的变量

要用final 定义 final作为修饰符 不分先后顺序

public class  Demo08 {
    static final double PI = 3.14 ;
    public static void main(String[] args) {
        System.out.println("===========");
        System.out.println(PI);
        
    }

}
public class  Demo08 {
    final static double PI = 3.14 ;
    public static void main(String[] args) {
        System.out.println("===========");
        System.out.println(PI);

    }

}

以上两段代码改变了final修饰符的位置 不影响程序的执行

变量的命名规范

所有的变量 方法 类名 :见名知意

类变量成员 局部变量 方法名 :小驼峰命名规则run(),runRun()

类名 : 大驼峰命名规则 Man,GoodMan

常量 : 大写字母和下划线MAX_VALUE

原文:https://www.cnblogs.com/xiaohima/p/15201394.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!