Java基础(一)

Java基础语法

1.注释

  1. 单行注释:// (用于注释单行文字)
  2. 多行注释:/* */ (可以注释一段文字)
  3. 文档注释:/** */ (可包含@标签)

2.标识符

  • 所有标识符都应该以字母(A-Z或者a-z),美元符($)、或者下划线(_)开始

  • 首字母之后可以是字母(A-Z或者a-z)、美元符($)、下划线(_)或数字的任何字符组合

  • 不能舒勇关键字作为变量名或方法名。

  • 标识符是大小写敏感的(很重要)

    ​ Java中的关键字:


    )

  • 可以使用中文命名,但是一般不建议这用去使用,也不建议使用拼音。

3.数据类型

  • 强类型语言:要求变量的使用哟啊严格符合规定,所有变量都必须先定义后才能使用
  • 弱类型语言:没有强类型语言这么严格

Java的数据类型分为两大类:

1. 基本类型
2. 引用类型 
public class Dome02 {
    public static void main(String[] args) {
        //八大基本数据类型

        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L; //Long 类型要在数字后面加个L

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

        //字符
        char name = 'A';

        //字符串,String 不是关键字,类
        //String name = "秦疆";

        //布尔值
        boolean flag = true;
        //boolean flag = false;
    }
}

数据类型的扩展以及面试题

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

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);

        System.out.println("==========================================");
        //===================================================================
        //浮点数拓展?   银行业务怎么表示?钱
        //BigDecimal  数学工具类
        //===================================================================
        //float  有限  离散   舍入误差   大约   接近但不等于
        //double

        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较

        float f = 0.1f;//0.1
        double d = 1.0/10;//0.1

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

        float d1 = 23131312312312313f;
        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 表:(97 = a  65 = A)  2字节 0 - 65536  Excel 2^16=65536

        //U000 UFFFF
        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);

        String sc = "Hello World";
        String sd = "Hello World";
        System.out.println(sc==sd);
        //对象  从内存分析

        //布尔值扩展
        boolean flag = true;
        if (flag==true) {}//新手
        if (flag) {}//老手
        //Less is More! 代码要精简易读
    }

}

结果:

10
8
16
==========================================
false
true
==========================================
a
97
中
20013
a
Hello	World
Hello
World
==========================================
false
true

Process finished with exit code 0

4.类型转换

  • 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换
  • 运算中,不同类型的数据先转化为同一类型,然后进行运算。

小数的优先级一定大于整数


)

public class Dome04 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i; //内存溢出

        double c = (double) i; //内存溢出

        //强制转换 (类型)变量名  高-->低
        //自动转换  低-->高

        System.out.println(i);
        System.out.println(b);
        System.out.println(c);

        /*
        * 注意点:
        * 1. 不能对布尔值进行转换
        * 2. 不能把对象类型转换为不相干的类型
        * 3. 在把高容量转换到低容量的时候,强制转换
        * 4. 转换的时候可能存在内存溢出,或者精度问题!
        * */
        System.out.println("==================================");
        System.out.println((int)23.7);//23
        System.out.println((int)-45.89f);//-45

        System.out.println("==================================");
        char d = 'a';
        int e = d+1;
        System.out.println(e);
        System.out.println((char)e);
    }
}

128
-128
128.0
==================================
23
-45
==================================
98
b

Process finished with exit code 0

扩展问题:

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

        long total3 = money*((long)years); //先把一个数转换为long
        System.out.println(total3);
    }
}

-1474836480
-1474836480
20000000000
posted @ 2025-01-21 16:29  昼冷夜欢  阅读(18)  评论(0)    收藏  举报