首页 > 技术文章 > java变量

150643com 2019-01-15 18:01 原文

class Demo
{
	public static void main(String[] args)
	{
		byte a = 3;
		System.out.print(a);
	}
}

byte -128到 +128

class Demo
{
	public static void main(String[] args)
	{
		byte a = 300;//这里会报错
		System.out.print(a);
	}
}

超出+128 损失精度

C:\Users\admin\Desktop\d1>javac 123.java
123.java:5: 错误: 不兼容的类型: 从int转换到byte可能会有损失
                byte a = 300;
                         ^
1 个错误
class Demo
{
	public static void main(String[] args)
	{
		byte a = 30;
		short b = 4000; 
		int x = 12;
		long l = 12345678;


		
/*
		float f = 2.3;
		C:\Users\admin\Desktop\d1>javac 123.java
123.java:11: 错误: 不兼容的类型: 从double转换到float可能会有损失
                float f = 2.3;
                          ^
1 个错误
*/
		float f = 2.3f;
		double d = 3.4;

		char ch = 'a';

		boolean bl = true;
		bl = false;
		System.out.print(bl);
	}
}

推荐阅读