123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /*
- Java定义的数据类型
- 一、变量安装数据类型来分:
- 基本数据类型
- 整型:byte short int long
- 浮点型: float double
- 字符型:char
- 布尔型:Boolean
- 引用数据类型:
- 类(class)
- 接口(interface)
- 数组(array)
- */
- class VariableTest1{
- public static void main(String[] args){
- //1、整型:byte(1字节=8bit) short(2字节) int(4字节) long(8字节)
- //(1)byte范围:-128~127
- byte b1 = 12;
- byte b2 = -128;
- //b2=128;//编译不通过
- System.out.println(b1);
- System.out.println(b2);
- //(2)声明long型变量,必须以“l”或“L”结尾
- //(3)通常,定义整型变量时,使用int型
- short s1 = 128;
- int i1 = 1234;
- long l1 = 132356464;
- //2.浮点型:float(4字节)、double(8字节)
- //(1)浮点型,表示带小数点的数值
- //(2)float表示数值的范围比long还大
- double d1 = 123.3;
- System.out.println(d1+1);
- //(3)定义float类型变量时,变量要以“f”或“F”结尾
- float f1 = 12.3f;
- System.out.println(f1);
- //(4)通常,定义浮点型变量时,使用double类型
- //3、字符型
- //(1)定义char型变量,通常使用一对'',内部只能写一个字符
- char c1 = 'a';
- //c1 = 'AB';
- System.out.println(c1);
- char c2 = '1';
- char c3 = '好';
- char c4 = 'め';
- System.out.println(c2);
- System.out.println(c3);
- System.out.println(c4);
- //(2)表示方式:1、声明一个字符 2、转义字符 3、直接使用Unicode值来表示字符型常量
- char c5 = '\n';//换行符
- c5 = '\t';//制表符
- System.out.print("hello"+c5);
- System.out.println("world");
- char c6 = '\u0123';
- System.out.println(c6);
- }
- }
|