• 02注释、标识符、关键字、数据类型、类型转换


    一、注释

    1、单行注释

    public class HelloWorld {
        public static void main(String[] args) {
            // 输出一个Hello, world
            System.out.println("Hello, World!");
        }
    }

    2、多行注释

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

    3、文档注释

    public class HelloWorld {
        public static void main(String[] args) {
            // JavaDoc: 文档注释
            /**
             * @Description HelloWorld
             * @Author kxq
             */
            System.out.println("Hello, World!");
        }
    }

    二、标识符

     一些主要的关键字

     三、数据类型

    强类型语言

      要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用

    public class demo02 {
        public static void main(String[] args) {
            String a = "hello";
            int num = 10;
            System.out.println(a);
            System.out.println(num);
        }
    }

    java的数据类型分为两大类

      基本类型(primitive type)

      引用类型(reference type)

    /*
    基本数据类型:
    
    数值类型
        整数类型:
            byte  占  1 个字节范围: -128 - 127
            short 占  2 个字节范围:   -32768 - 32767
            int     占  4 个字节范围:   -2147483648 - 2147483647
            long  占  8  个字节范围:   -9223372036854775808 - 9223372036854775807
    
        浮点类型
            float     占 4个字节
            double  占 8 个字节
    
        字符类型
            char    占 2 个字节
    boolean类型:
       占 1 位 值只有true 和 false 两种 
    */
    /*
    引用数据类型
        类
        接口
        数组
    */
    public class demo02 {
        public static void main(String[] args) {
            // 八大基本数据类型
            
            // 整数
            int num1 = 10;
            byte num2 = 20;
            short num3 = 30;
            long num4 = 30L; // Long 类型要在数字后加上L
            
            // 小数: 浮点数
            float num5 = 50.1F; // Lfloat 类型要在数字后加F
            double num6 = 3.1415926537497345;
            
            // 字符
            char name = 'a';
            
            // 字符串
            String nmaea = "kxq";
            
            // 布尔值
            boolean flag = true;
        }
    }

    四、数据类型扩展

    1、整数拓展

    public class demo03 {
        public static void main(String[] args) {
            // 整数拓展
            // 二进制: 0b 八进制: 0 十六进制: 0x
            int i = 10;
            int i2 = 010;
            int i3 = 0x10;
    
            System.out.println(i);
            System.out.println(i2);
            System.out.println(i3);
        }
    }
    // 结果
    10
    8
    16

    2、浮点数拓展

    银行业务中表示一个数

    float 和 double 是有问题的

    public class demo03 {
        public static void main(String[] args) {
            // 浮点数拓展
            float f = 0.1f;
            double d = 1.0/10;
    
            System.out.println(f==d);
            System.out.println(f);
            System.out.println(d);
    
        }
    }

    从我们的角度,两个应该都为0.1

    打印结果:

    false
    0.1
    0.1

    f 和 d 都是0.1, 但是是否相等是false

    再看一个

    public class demo03 {
        public static void main(String[] args) {
            // float扩展
            float d1 = 123123123123f;
            float d2 = d1 + 1;
            System.out.println(d1 == d2);
    
        }
    }

    打印结果:

    true

    d1 和 d2 的值明显不一样, 但是判断两个值是否相等却是 true

    /*
    浮点数表示的类型是有限的, 离散的, 有舍入误差, 只是接近但不等于
    最好完全使用浮点数进行比较
    */

    那银行业务怎么表示呢?

    通过大数类型BigDecimal数学工具类表示

    3、字符拓展

    public class demo03 {
        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); // 强制转换
        }
    }

    打印结果:

    a
    9720013

    把字符变为数字,所有的字符本质还是数字

    会有一张字符编码表

    Unicode 编码占2个字节, 65536, 从U0000 - UFFFF

    比如:

    public class demo03 {
        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); // 强制转换
    
            char c3 = '\u0061';
            System.out.println(c3); // a
        }
    }

     4、转义字符

    public class demo03 {
        public static void main(String[] args) {
            System.out.println("Hello\tWorld");
            System.out.println("Hello\nWorld");
        }
    }

    打印结果:

    Hello    World
    Hello
    World

    预留一个关于字符串是否相等的

    public class demo03 {
        public static void main(String[] args) {
            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);
            
        }
    }

    打印结果:

    false
    true

    new 之后的是false

    下面的是true

    学习对象的时候会从内存分析

    5、布尔值扩展

    public class demo03 {
        public static void main(String[] args) {
            boolean flag = true;
            if (true) {
                // 为真 代码逻辑
            }
        }
    }

     五、类型转换

    由于java是强类型语言, 所以要进行有些运算的时候,需要用到类型转换

    运算中,不同类型的数据先转换为同一类型,然后进行运算

    // 由低到高分为
    低                               高
    byte,short,char ---> int ---> long ---> float ---> double
    public class demo04 {
        public static void main(String[] args) {
            int i = 128;
            byte b = (byte) i;
    
            System.out.println(i);
            System.out.println(b);
        }
    }

    打印结果:

    128
    -128

    i 是128, 但是b 却是 -128

     源码:

    public final class Byte extends Number implements Comparable<Byte> {
    
        /**
         * A constant holding the minimum value a {@code byte} can
         * have, -2<sup>7</sup>.
         */
        public static final byte   MIN_VALUE = -128;
    
        /**
         * A constant holding the maximum value a {@code byte} can
         * have, 2<sup>7</sup>-1.
         */
        public static final byte   MAX_VALUE = 127;

    byte 最大值是127

    造成了内存溢出

     类型转换分为

    强制类型转换

    自动类型转换

    上面的就是强制类型转换

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

    打印结果:

    128
    128.0

    总结:

    /*
    1. 不能对布尔进行转换
    2. 不能把对象类型转换为不相干的类型
    3. 在把高容量转换到低容量的时候, 强制转换
    4. 转换的时候可能存在内存溢出,或者精度问题
    */

    操作比较大的数的时候, 注意溢出问题

    JDK7新特性, 数字之间可以用下划线分割

    public class demo05 {
        public static void main(String[] args) {
            int money = 10_0000_0000;
            int years = 20;
            int total = money * years; // 默认是int, 转换之前已经存在问题了
            System.out.println(total);
        }
    }

    打印结果:

    -1474836480

    计算的时候溢出了

    解决:

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

    打印结果:

    -1474836480
    20000000000
  • 相关阅读:
    5.2 i++
    5.1 赋值语句
    Implement Queue using Stacks
    Binary Tree Paths
    Single Number III
    Ugly Number
    SurfaceHolder.Callback
    intellj(idea) 编译项目时在warnings 页签框里 报 “xxx包不存在” 或 “找不到符号” 或 “未结束的字符串字面值” 或 “需要)” 或 “需要;”等错误提示
    Hibernate添加日志--log4j
    java实现网页验证码
  • 原文地址:https://www.cnblogs.com/kongxiangqun/p/14909426.html
Copyright © 2020-2023  润新知