分享

Java教程基础——变量

 印度阿三17 2019-07-12

变量

变量(variable)可以理解为一个“有名称的容器”,用于装各种不同类型的数据。编程人员通过对变量的访问和修改,操作内存中的数据。

对变量的理解:https://www.cnblogs.com/tigerlion/p/10665321.html

Java是一种强类型(strongly typed)的语言,在编译时有严格的语法检查,需要为变量指定类型。

基本类型 整 型 byte 1byte
(基本类型) (整型) short 2byte
(基本类型) (整型) int 4byte
(基本类型) (整型) long 8byte
(基本类型) 浮点型 float 4byte
(基本类型) (浮点型) double 8byte
(基本类型) 字符型 char -
(基本类型) 布尔型 boolean -
引用类型 数 组 - -
(引用类型) 接 口 - -
(引用类型) - -

byte类型的范围是-128~127,即-2^7~2^7-1
char类型相当于无符号整数类型,一般认为是2字节(Unicode),但此说法有争议。
字符串不是基本类型,字符串是类,属于引用类型。
引用类型变量就是指针,只是Java不使用指针的说法。

标识符

用于程序变量、类、方法命名的符号。

|-包括字母、0-9、_、$(美元符号)组成
|-字母包括A-Z、a-z、中文、日文等
|-数字不打头
|-不能含有空格和特殊字符(@、#等)
|-不能和关键字重复

※关于​$符号作为变量名
int $n=1;

Java 8 VS2012(C#.Net Framework 4.5) Dev C (5.11@2015)
OK(有的Eclipse不支持) Err OK

命名规范

Java的命名一般遵循“驼峰标识”:每个单词首字母大写,另外——
|-类:首字符大写
|-变量:首字母小写
|-方法:首字母小写
命名要求:见名知意

整型和字符型

public class Test整型 {
    public static void main(String[] args) {
        // -------------------------------------------------
        // ---进 制-------------------------------------------
        // -------------------------------------------------
        int n2 = 0B10;// 0B二进制(binary)(JAVA 7新增)
        System.out.println("n1="   n2);
        int n8 = 010;// 0八进制(octal)
        System.out.println("n8="   n8);
        int n10 = 10;
        System.out.println("n10="   n10);
        int n16 = 0XF;// 0X十六进制(hexadecimal)
        System.out.println("n16="   n16);
        // -------------------------------------------------
        // ---真正的长整形,需要加L-------------------------------
        // -------------------------------------------------
        // 即使声明long类型,系统也是转换为int类型
        // int类型的最大值是2147483647,当超过这个值时,系统不会自动转换,编译无法通过。
        // 需要在数字末尾加L,以示其为long类型
        long lo1 = 2147483648L;
        System.out.println("lo1="   lo1);
        // -------------------------------------------------
        long lo2 = 10_0000_0000L;// Java7开始,数值可以加下划线以看清位数
        System.out.println("lo2="   lo2);
        // -------------------------------------------------
        // ---字符类型-------------------------------------------
        // -------------------------------------------------
        char c1 = 97;// 可直接赋值ASCII码对应的数值
        System.out.println("c1 = "   c1);
        char c2 = '字';// 字符用单引号,不分中英文
        System.out.println("c2 = "   c2);
        char c3 = '\'';// 单引号、双引号、反斜线(\)等特殊符号前面加转移符【\】
        System.out.println("c3 = "   c3);
    }
}

n1=2
n8=8
n10=10
n16=15
lo1=2147483648
lo2=1000000000
c1 = a
c2 = 字
c3 = '

浮点数

public class TestVar {
    public static void main(String[] args) {
        // ------------------------------------------------------------------
        double d1 = 100.1;// double是默认浮点类型
        float f1 = 100.1F;// float就必须加F
        // ------------------------------------------------------------------
        double d2 = .123;// 整数位可以不写
        System.out.println(d2);
        double d3 = 321E2;// 科学计数法形式(只有浮点数可以使用)
        System.out.println(d3);
        // ------------------------------------------------------------------
        // 浮点数遵循IEEE 754标准,并不能精确表示一个数
        double d4 = 19.9;
        System.out.println(d4 * 3);
        // 一般算钱的时候可以转为整数再算,或者使用BigDecimal类型计算
        int n4 = (int) (d4 * 100);
        int calc = n4 * 3;
        double d5 = calc / 100;
        System.out.println(d5);
        // ------------------------------------------------------------------
        System.out.println("浮点数可以除0:"   10_0000.0 / 0);// Infinity:无穷大
        System.out.println("整数不行:"   10_0000 / 0);
    }
}

0.123
32100.0
59.699999999999996
59.0
浮点数可以除0:Infinity
Exception in thread "main" java.lang.ArithmeticException: / by zero
at TestVar.main(TestVar.java:22)


类型转换

自动转换

转换条件
|--类型兼容
|--目标类型大于源类型

byte→ short→ int→ long→ float→ double
↑char

下图是合法的数据类型之间的转换,
6个实心箭头代表无损转换,3个虚线代表有可能丢失精度

public class 自动转换 {
    public static void main(String[] args) {
        byte _byte = 1;
        char _char = 2;
        short _short = _byte;
        int _int = _short;
        _int = _char;
        long _long = _int;
        float _float = _long;
        double _double = _float;
    }
}

强制类型转换:目标类型加括号,可能造成数据丢失,称为“缩小转换”。

        double d = 9.9;
        int n = (int)d;

编译器自动强转:

对于byte/short/char三种类型,如果等号右侧数值没有超过范围,编译器自动补上(xxx)。
int及更长的类型无需转换,因为右值默认是int型。

public class 编译器优化 {
    public static void main(String[] args) {
        System.out.println(" = "   System.getenv("JAVA_HOME"));
        byte b1 = 127;// 右侧整数没有超过左侧类型范围,编译通过
        byte b2 = 128;// 超过范围,编译失败:cannot convert from int to byte
        byte b3 = 128 - 1;// 编译通过
        byte b4 = b1 - 1;// 如果变量参与计算,编译器拒绝优化:cannot convert from int to byte
    }
}
来源:https://www./content-1-321801.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多