//第一个java程序 public static void main(String[] args) { System.out.println("Hello World"); //java的数据类型 //数据类型有八种 六种数字型 四个整数型 两个浮点型 一种字符串型 一种布尔型 //byte 数据类型是八位的 // 最小值是 -128 最大值为 127 默认值为 0 //变量占用空间只是int类型的四分之一 byte byte_1= 123; byte byte_2=-123; System.out.println(byte_1); System.out.println(byte_2); //short 数字类型是16位的 // 最小值为 -32768 最大值为 32767 默认值为 0 // 变量占用空间是int类型的二分之一 short short_1=1208; short short_2=-1245; System.out.println(short_1); System.out.println(short_2); //int 数字类型为32位的 //最小值为 -2,147,483,648 最大值为 2,147,483,647 默认值为 0 int int_1= 100000; int int_2 = -10000; System.out.println(int_1); System.out.println(int_2); //long 数据类型为64位 //最小值为 -9,223,372,036,854,775,808 最大值为 9,223,372,036,854,775,807 // 默认值是 0L "L"理论上不分大小写,但是若写成"l"容易与数字"1"混淆,不容易分辩。所以最好大写。 long long_1= 10000000L; long long_2= -100000000L; System.out.println(long_1); System.out.println(long_2); // float 数据类型是单精度 32位 // float 在存储大型浮点数组的时候可节省内存空间 // 默认值为 0.0f // 浮点数不能用来 表示 精确的值 如货币 float float_1= 234.5f; System.out.println(float_1); //为什么Java中long后面要加L?float后面加F? //java的内存体制 //long fw = 10000000000L; //实际上内存中fw只是一个long类型的变量,它存在于向stack(栈)中, // 数值并不在其中存放,它指向heap(堆)中另一块真正存放数值的内存, // 加L的目的就是为了让 堆 中也创建一块long类型所需要的内存,用来来放数值。 //所以说=前后两者其实是在不同的两块内存,只不过有个无形的指针将两者连接起来了。 //问: //整数的默认的数据类型是int,那为什么byte和short类型后面不用加东西? //答: //java整型默认为int,且java会自动向下转型,byte和short都可以由int自动向下转型, // 但是long类型的不能自动向上转型,所以要加L,转为long类型 //不加的后果: //不加L默认是int,int转为long是安全的,所以会自动转,能编译通过 //浮点数不加F默认是double类型,double转float可能损失精度,因为不会自动转,编译通不过 //如果超过int的取值范围还不加L,那么也会直接报错了 //double // 数据类型为双精准度 64位 //浮点数的默认类型为double类型 //double类型同样不能表示精确的值,如货币 // 默认值是 0.0d; double double_1=123.1; System.out.println(double_1); // boolean true false // 默认值为 false boolean bool_1= true; System.out.println(bool_1); boolean bool_2=false; System.out.println(bool_2); //char 类型是一个单一的 16位 Unicode 字节 //最小值是 \u0000(即为0); //最大值是 \uffff(即为65,535); //char 数据类型可以储存任何字符; char char_2='A'; System.out.println(char_2); //字符串 字符串 变量 要使用 双引号 不能使用单引号 String str="bsdadsa"; System.out.println(str); //常量 // 常量在程序运行时 不能被修改 // 使用关键字 final 来修饰常量 final double PI=3.1415926; System.out.println(PI); //虽然常量名也可以用小写 但是便于区分 常量建议使用大写 // 各种数据类型在进行混合运算的时候 要先转换为同一数据类型 然后再进行运算 //数据类型转换必须满足如下规则: //1. 不能对boolean类型进行类型转换。 //2. 不能把对象类型转换成不相关类的对象。 //3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。 //4. 转换过程中可能导致溢出或损失精度,例如: int i = 128; byte b =(byte) i; //System.out.println(b); //浮点数到整数的转换是通过舍弃小数得到 而不是四舍五入 float f =23.7f; int in =(int)f; System.out.println(in); //自动类型转换 // 必须满足 转换前的数据类型的位数 要低于转换后的数据类型 // 自动转换数据类型 只能 从小往大 进行转换 如果从大往小转换 有可能会导致 数据溢出 //例如 short 数据类型位数为 16位 就可以自动转换为位数为32位的int型 //强制类型转换 //1.条件是转换的数据类型必须是兼容的 // 2. 格式为:(type)value type 是要强制类型转换后的数据类型 //隐含强制类型转换 //1. 整数的数据类型是int } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦