变量的使用

  1.java定义变量的格式:数据类型 变量名 = 变量值;
  2.变量需要先声明,后使用

public class Variable {
    public static void main(String[] args){
        //变量定义
        int number = 1001;
        //变量使用
        System.out.println(number);
        //变量声明
        int number2;
        //变量赋值
        number2 = 100;
        //变量使用
        System.out.println(number2);
    }
}

基本数值类型:

数值型:
  整数类型(byte,short,int,long)
    byte 1字节8bit -128~127
    short 2字节 -215~215-1
    int 4字节 -231~231-1(~21亿)
     long 8字节 -263~263-1
  浮点类型(float,double)
  字符型(char)
  布尔型(boolean)
  引用数据类型:
    类(class)
    接口(interface)
    数组(【】)

public class Variable_02 {
    public static void main(String[] args){
        byte b1 = 12;
        byte b2 = -128;
        //byte b3 = 128;
        //超过范围,编译不通过
        System.out.println(b1);
        System.out.println(b2);

        short s1 = 128;
        int i1 = 1234;
        long l1 = 1231231L;
        //声明long型变量,必须以‘l‘或’L‘结尾
        System.out.println(l1);

        double d1 = 123.3;
        System.out.println(d1);
        float f1 = 12.3f;
        //定义float类型变量是,结尾以f或F结尾
        System.out.println(f1);

        char c1 = 'a';
        //char只能写一个字符
        System.out.println(c1);
        c1 = 'A';
        System.out.println(c1);
        char c2 = '中';
        System.out.println(c2);
        //转义字符
        char c3 = '\n'; //换行符
        System.out.print("hello" + c3);
        System.out.println("world");
        char c4 = '\u0043';
        //使用Unicode值来表示字符型常量
        System.out.println(c4);

        //布尔型:boolean
        //只能取两个值:true/false
        //常常在条件判断、循环结构中使用
        boolean bl1 = true;
        System.out.println(bl1);

        boolean isMarried = false;
        if(isMarried){
            System.out.println("sorry");
        }else{
            System.out.println("yes");
        }
    }
}

/*

基本数据类型之间的运算规则

  不包含boolean
  1.自动类型提升:
    低级和高级运算,选择高级,否则编译不通过
    byte、char、short -> int -> long -> float -> double
    byte、char、short三种类型做运算时,结果为int型
  2.强制类型转换
    需要使用强转符号:()
    注意点:可能导致精度损失

public class Variable_use {
    public static void main(String[] args){
        byte b1 = 2;
        int i1 = 12;
        //byte b2 = b1 + 12;
        //编译不通过
        int i2 = b1 + i1;
        System.out.println(i2);

        float f = b1 + i1;
        System.out.println(f);

        char c1 = 'a';
        int i3 = 10;
        int i4 = c1 + i3;
        System.out.println(i4);

        short s2 = 10;
        //short s3 = s2 + c1;
        //char c4 = s2 + c1;
        //char和short运算只能从int往上取

        double d01 = 12.3;
        //int i01 = d01;
        int i01 = (int)d01; //截断操作
        System.out.println(i01);

        //没有精度损失
        long l01 = 123;
        short s02 = (short)l01;
        System.out.println(s02);

    }
}

/*

String类型变量的使用

  1.String属于引用数据类型,翻译为字符串
  2.声明变量是,使用一对""
  3.可以和8中基本数据类型变量做基本运算你,且运算只能是连接运算+,运算结果任然是String类型

public class String_01 {
    public static void main(String[] args){
        String s1 = "Hello World!";
        System.out.println(s1); //Hello World!

        int number = 1001;
        String numberstr = "学号";
        String info = numberstr + number;
        //连接运算
        System.out.println(info); //学号1001
        boolean b1 = true;
        String info1 = numberstr + b1;
        System.out.println(info1); //学号true

        char c = 'a'; //97
        int num = 10;
        String str = "hello";
        System.out.println(c + num + str); //107hello
        System.out.println(c + str + num); //ahello10
        System.out.println(c + (num + str)); //a10hello
        System.out.println((c + num) + str); //107hello
        System.out.println(str + num + c); //hello10a

        //char对应asc码,数相加
        System.out.println("*   *"); //*   *
        System.out.println('*' + '\t' + '*'); //93
        System.out.println('*' + "\t" + '*'); //*   *
        System.out.println("*" + '\t' +  "*"); //*   *
        System.out.println("*" + ('\t' +  "*")); //*   *

    }
}