-
8种基本数据类型:
* 整数:byte、short、int、long
* 浮点数:float、double
* 布尔类型:boolean
* 字符型:char
* *///byte 字节。最小的整数类型。占1个字节。 //-128~127 //单行注释的快捷键 control+/ //先选中要注释的行,再按control+/ //按一次是注释,再按一次是取消注释。 /*byte a = 50; System.out.println(a); //short也表示整数,但是占2个字节。 //-32768~32767 short b = 200; System.out.println(b); //int 占4个字节。 //表示的数更大了。 //-2147483648~2147483647 int c = 300; System.out.println(c); //long 占8个字节。 //属于最大的整数类型。 //能表示19位整数。 long d = 500; System.out.println(d); System.out.println("======================="); byte e = (byte)437; System.out.println(e);*/ //多行注释的快捷键 //control+shift+/ 多行注释 //control+shift+\ 取消多行注释 //数据类型有隐式转换和显示转换2种。 //一般大数据类型能容纳小数据类型的内容,因此 //系统提供了隐式转换。 // =是java里面的赋值运算符。 //它的功能是把=右边的值,赋值给左边的变量。 /*byte a1 = 100; short b1 = a1; int c1 = a1; int c2 = b1; long d1 = a1; long d2 = b1; long d3 = c1;*/ //大类型往小类型转换就出现编译错误。 //如果非要转换,可以使用强制类型转换。 /*short a1 = 100; byte b1 = (byte)a1; System.out.println(b1); int c1 = 200; byte b2 = (byte)c1; short d1 = (short)c1; System.out.println(b2); System.out.println(d1); byte a10 = 5; a10 = (byte)(a10 + 1); //整数默认类型是int System.out.println(a10); byte b10 = 5; short c10 = 6; c10 = (short)(b10 + c10); System.out.println(c10); int d10 = b10+c10; System.out.println(d10); System.out.println("-------------"); float f1 = (float)3.14; System.out.println(f1); float f2 = 3.14f; float f3 = 3.14F; double h1 = 3.14; double h2 = 3.14d; double h3 = 3.14D; System.out.println(h2);*/ //类型转换 float + int结果是float
// int x = 100;
// float y = 12.5f;
// int z = (int)(x + y);
// System.out.println(z);
// float w = x + y;
// System.out.println(w);
long x = 100;
float y = 1.25f;
long z = (long)(x + y);
System.out.println(z);
float w = x + y;
System.out.println(w);
//字符类型
//一个char类型的变量,用于保存一个字符。
//字符可以是字母,可以是符号,可以是汉字,
//可以是日文。。。
//在java里面,char类型存储的是Unicode字符。
//java的char占2个字节。
char c1 ='a';
char c2 = '张';
char c3 = 'ぁ';
char c4 = '\'';
char c5 = '\\';
char c6 = '\u6625';
System.out.println(c6);
System.out.println(c5);
char c10 = 'a';
char d10 = (char)(c10 + 3);
int e10 = c10 + 3;
System.out.println(e10);
System.out.println(d10);
boolean h10 = true;
boolean h11 = false;
System.out.println(h11);
//布尔类型不能参与运算
//布尔类型不能与其他类型互转,包括强转。
boolean h12 = (boolean)3;
/*
* 数据类型的转换:
* 隐式转换:
* 小转大。大类型可以接收小类型。
* 赋值时:小类型可以赋值给大类型。
* byte->short->int->long->float->double
* char->int->long->float->double
* 运算时:
* 小于等于int类型的整数进行运算,会自动升级为int
* 再进行运算。
* 小于等于long类型整数与long进行运算,会自动升级为long
* 再进行运算
* 如果和float运算,升级为float
* 日过和double运算,升级为double。
*
* 如果=左边的变量类型小于=右边的类型,要进行强制转换。
*
* 强制转换:
* 格式: 变量 = (指定的类型)数值,指定的类型要与变量的类型一致。
* */