java基础-变量

1.变量的定义

用来命名一个数据的标识符

变量命名只能使用字母 数字 $ _
变量第一个字符 只能使用 字母 $ _
变量第一个字符 不能使用数字
注:_ 是下划线,不是-减号或者—— 破折号

在命名的时候,尽量使用完整的单词进行命名,比如name,moveSpeed,而不是使用缩写 n,m。

关键字不能直接用来作为变量名

中文也是可以用来命名变量的

2.八种基本类型

1.整型 (4种)整型变量用于存放整数

byte 长度为8位,缺省值0,范围 -(2^7) +(2^7)-1
short 长度为16位,缺省值0,范围 -(2^15) +(2^15)-1
int 长度为32位,缺省值0,范围 -(2^31) +(2^31)-1
long 长度为64位,缺省值0,范围 -(2^63) ~ +(2^63)-1

2.字符型 (1种)只能存放一个字符,超过一个字符就会产生编译错误

char 长度为16位,缺省值0,范围 0 ~ +2^16, 任意单个字符,加单引号,char运算的因为char在ASCII等字符编码表中,有对应的数值。

3.浮点型 (2种)

float 长度为32位,缺省值0.0,范围 -2^128 ~ +2^128,最多能有7位有效数字,但绝对能保证的为6位,也即float的精度为6~7位有效数字
double 长度为64位,缺省值0.0,范围 -2^1024 ~ +2^1024,double的精度为15~16位

4.布尔型(1种)布尔型用于表示真假,其长度为1

boolean 长度为2位,缺省值false,范围false~true

5.使用

public class HelloWorld {
    public static void main(String[] args) {
        byte b = 100;
        short s = 200;
        int i = 300;
        long l = 400;

        long val = 26L; // 以L结尾的字面值表示long型
        int decVal = 26; // 默认就是int型
        int hexVal = 0x1a; // 16进制
        int oxVal = 032; // 8进制
        int binVal = 0b11010; // 2进制

        float f1 = 123.4F;// 以F结尾的字面值表示float类型
        double d1 = 123.4;// 默认就是double类型
        double d2 = 1.234e2;// 科学计数法表示double
        char c = '中'; // char 只能存放一个字符,超过一个字符就会产生编译错误

        // 以下是转义字符
        char tab = '\t'; // 制表符
        char carriageReturn = '\r'; // 回车
        char newLine = '\n'; // 换行
        char doubleQuote = '\"'; // 双引号
        char singleQuote = '\''; // 单引号
        char backslash = '\\'; // 反斜杠

        boolean b1 = true;// 虽然布尔型真正存放的数据是0(false) 1(true)
        boolean b2 = false;// 但是,不能直接使用0 1 进行赋值

        System.out.println("b=" + b);
        System.out.println("s=" + s);
        System.out.println("i=" + i);
        System.out.println("l=" + l);
        System.out.println("val=" + val);
        System.out.println("decVal=" + decVal);
        System.out.println("hexVal=" + hexVal);
        System.out.println("oxVal=" + oxVal);
        System.out.println("binVal=" + binVal);
        System.out.println("f1=" + f1);
        System.out.println("d1=" + d1);
        System.out.println("d2=" + d2);
        System.out.println("c=" + c);
        System.out.println("tab=" + tab);
        System.out.println("b1=" + b1);
        System.out.println("b2=" + b2);
    }
}

3.String字符串

String类型其实并不是基本类型,但是它是如此广泛的被使用,常常被误以为是一种基本类型。
String类型是Immutable的,一旦创建就不能够被改变。

String 被修饰为final,所以是不能被继承的

public class HelloWorld {
    public static void main(String[] args) {
        String hello = "Hello"; // 字面值,虚拟机碰到字面值就会创建一个字符串对象

        String world = new String("World"); // 创建了两个字符串对象

        char[] java = new char[] { 'J', 'a', 'v', 'a' };

        String Java = new String(java);// 通过字符数组创建一个字符串对象

        String helloWorld = hello + world;// 通过+加号进行字符串拼接

        System.out.println(hello + Java);
        System.out.println(helloWorld);
    }
}

4.基本类型的类型转换

不同类型之间的数据可以互相转换,但是要满足一定的规则

精度高的数据类型就像容量大的杯子,可以放更大的数据
精度低的数据类型就像容量小的杯子,只能放更小的数据
小杯子往大杯子里倒东西,大杯子怎么都放得下
大杯子往小杯子里倒东西,有的时候放的下,有的时候就会有溢出
需要注意的一点是
虽然short和char都是16位的,长度是一样的
但是彼此之间,依然需要进行强制转换

byte->short(char)->int->long->float->double(低精度->高精度)

1.低精度向高精度转换(类型转换)

public class HelloWorld {
    public static void main(String[] args) {
        long l = 50;
        int i = 50;

        // int比较小,要放进比较大的long,随便怎么样,都放的进去
        l = i;
        System.out.println(l);
    }
}

2.高精度向低精度转换(类型转换)

public class HelloWorld {
    public static void main(String[] args) {
        byte b = 5;
        int i1 = 10;
        int i2 = 300;

        b = (byte) i1;
        // 因为i1的值是在byte范围之内,所以即便进行强制转换
        // 最后得到的值,也是10
        System.out.println(b);

        // 因为i2的值是在byte范围之外,所以就会按照byte的长度进行截取
        // i2的值是300,其对应的二进制数是 100101100
        // 按照byte的长度8位进行截取后,其值为 00101100 即44
        b = (byte) i2;
        System.out.println(b);

        // 查看一个整数对应的二进制的方法:
        System.out.println(Integer.toBinaryString(i2));
    }
}

3.怎么查看数据类型

public class HelloWorld {
    public static void main(String[] args) {
        short a = 32767;
        short b = 32767;
        System.out.println("a1+b1的基础数据类型为:" + getType(a + b));
    }

    public static String getType(Object o) {
        return o.getClass().toString();
    }

    public static String getType(int o) {
        return "int ";
    }

    public static String getType(byte o) {
        return "byte ";
    }

    public static String getType(char o) {
        return "char ";
    }

    public static String getType(double o) {
        return "double ";
    }

    public static String getType(float o) {
        return "float ";
    }

    public static String getType(long o) {
        return "long ";
    }

    public static String getType(boolean o) {
        return "boolean ";
    }

    public static String getType(short o) {
        return "short ";
    }
}

5.作用域

public class HelloWorld {
    int i = 1; // 属性名是i
    int j = i; // 其他的属性可以访问i

    public void method(int i) { // 参数也是i
        System.out.println("method1:" + i);
        System.out.println("method1:" + this.i);
    }

    public void method() {
        int i = 2;
        System.out.println("method2:" + i);
        { // 子块
            System.out.println("method2:" + i); // 可以访问i
            int j = 3;
            System.out.println("method2:" + j); // 可以访问j
        }
        System.out.println("method2:" + j); // 不能访问内部j
    }

    public static void main(String[] args) {
        new HelloWorld().method(4);
        new HelloWorld().method();
    }
}

结果:

method1:4
method1:1
method2:2
method2:2
method2:3
method2:1

猜你喜欢

转载自www.cnblogs.com/antao/p/12815694.html