基本数据类型:4大类8种。

代码说明:
byte 字节型 占1个字节 -128~127
- package com.zhou.variable;
-
- //目标:基本数据类型
- public class VariableDemo2 {
- public static void main(String[] args) {
- // 1. byte 字节型 占1个字节 -128~127
- byte number = 98;
- // byte number2=128;//超出字节会报错
-
-
- }
- }
2. short 短整型 占2个字节 使用8个二进制位为一组
- package com.zhou.variable;
-
- //目标:基本数据类型
- public class VariableDemo2 {
- public static void main(String[] args) {
-
- /**
- * 2. short 短整型 占2个字节 使用8个二进制位为一组,
- * 一个字节是8b 2个字节就是16 2^16次方为65536
- * short 取值范围-32768~32767加起来也就是 2^16次方为65536
- * */
- short money = 30000;
- //short money1 = 300000;//超出字节范围报错
-
-
- }
- }
int 整型 默认的类型 占4个字节 取值范围-2147483648~2147483647 (10位数)
- public class VariableDemo2 {
- public static void main(String[] args) {
- // 3.int 整型 默认的类型 占4个字节 取值范围-2147483648~2147483647 (10位数)
- int it = 2325254;
-
- }
- }
long 长整型 占8个字节
- public class VariableDemo2 {
- public static void main(String[] args) {
- long lg = 133457687;
- // 注:以下报错原因是随写定一个整数字面量默认是int类型的132223243244244,虽然没有超过long的范围,但是它超过了int类型的取值范围
- // 如果希望随便写一个整数字面量当成long类型,需要在后面加L 或小写的l
- // long lg2=132223243244244;//报错
- // 正确写法
- long lg2 = 132223243244244L;
-
- }
- }
浮点型 (小数) 占4个字节 取值范围1.401298e-45到3.402823e+38
- public class VariableDemo2 {
- public static void main(String[] args) {
- // 注:以下报错原因是随写定一个字面量默认是double类型,如果希望随便琯一个小数字面量是float类型可以在后面加F或f
- // float score=98.5;报错
- // 正确写法
- float score = 98.5F;
-
- }
- }
double双精度 占8个字节 取值范围4.9000000e-324 到1.797693e+308
- public class VariableDemo2 {
- public static void main(String[] args) {
- double score2 = 99.999;
-
- }
- }
字符类型 char
- public class VariableDemo2 {
- public static void main(String[] args) {
- char ch = 'a';
- // char ch2='中国'; 报错不能放两个字符
-
- }
- }
布尔类型 boolean 取值范围 true,false
- public class VariableDemo2 {
- public static void main(String[] args) {
- boolean rs = true;
- boolean rs2 = false;
-
- }
- }