Java基本数据类型的运算

2021-05-04 10:30

阅读:579

标签:结果   数据类型   示例   void   基本数据类型   bsp   特殊   错误   不兼容   

自动数据类型提升的运算

byte 、char、short 、int 、long 、float、double

注意:当byte、char、short 三种数据类型做运算,结果为int型

代码1

class VariableTest1
{
	public static void main(String[] args) 
	{
		byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
	}
}
---------- java ----------
263

输出完成 (耗时 0 秒) - 正常终止

  示例2

class VariableTest1
{
	public static void main(String[] args) 
	{
		byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
        float f = y + r;
		System.out.println(f);
		short s1 = 123;
		int d1 = s1;
		System.out.println(d1);

	}
}
---------- java ----------
263
263.0
123

输出完成 (耗时 0 秒) - 正常终止

  示例3强制类型转换

class VariableTest1
{
	public static void main(String[] args) 
	{
		/*byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
        float f = y + r;
		System.out.println(f);
		short s1 = 123;
		int d1 = s1;
		System.out.println(d1);
		*/
	    double g1 = 12.9;
		int i1 = (int)g1; //截断操作
		System.out.println(i1);
	}
}
---------- java ----------
12

输出完成 (耗时 0 秒) - 正常终止

  long转换short型转换操作

class VariableTest1
{
	public static void main(String[] args) 
	{
		/*byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
        float f = y + r;
		System.out.println(f);
		short s1 = 123;
		int d1 = s1;
		System.out.println(d1);
		
	    double g1 = 12.9;
		int i1 = (int)g1;
		System.out.println(i1);
		*/
		long l1 = 123;
		short s2 = (short)l1;
		System.out.println(s2);
	}
}
---------- java ----------
123

输出完成 (耗时 0 秒) - 正常终止

  示例2

class VariableTest1
{
	public static void main(String[] args) 
	{
		int i2 = 128;
		byte b2 = (byte)i2;
		System.out.println(b2);
	}
}
---------- java ----------
-128

输出完成 (耗时 0 秒) - 正常终止

 编码特殊情况

class VariableTest1
{
	public static void main(String[] args) 
	{
		long i1 = 123456;//正常long型数据数值后应添加L或者l;不加也正常;因为没加的话表示自动类型提升操作;默认是int 有赋值给long型
		System.out.println(i1);
		long l1 = 12345678998;  // 因为没加L而它的数值int有放不下所有报错
		System.out.lprintln(l1);
	}
}
---------- javac ----------
VariableTest1.java:35: 错误: 过大的整数: 12345678998
		long l1 = 12345678998;
		          ^
1 个错误

输出完成 (耗时 0 秒) - 正常终止

  示例

class VariableTest1
{
	public static void main(String[] args) 
	{
		/*byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
        float f = y + r;
		System.out.println(f);
		short s1 = 123;
		int d1 = s1;
		System.out.println(d1);
		
	    double g1 = 12.9;
		int i1 = (int)g1;
		System.out.println(i1);
		long l1 = 123;
		short s2 = (short)l1;
		int i2 = 128;
		byte b2 = (byte)i2;
		System.out.println(b2);
		*/
		//
		long i1 = 123456;//正常long型数据数值后应添加L或者l;不加也正常;没加的话表示自动类型提升操作;默认是int 有赋值给long型
		System.out.println(i1);
		long l1 = 12345678998l;
		System.out.println(l1);
	}
}

  常量编码;

class VariableTest1
{
	public static void main(String[] args) 
	{
//整型常量默认类型int型;
//浮点型常量默认是double型;
		byte q = 12;
		byte b1 = q + 1;
		System.out.println(b1);
	
	}
}
---------- javac ----------
VariableTest1.java:38: 错误: 不兼容的类型: 从int转换到byte可能会有损失
		byte b1 = q + 1;
		            ^
1 个错误

输出完成 (耗时 1 秒) - 正常终止

class VariableTest1
{
	public static void main(String[] args) 
	{
		/*byte y = 9;
		int r = 254;
		int e = y+r;
		System.out.println(e);
        float f = y + r;
		System.out.println(f);
		short s1 = 123;
		int d1 = s1;
		System.out.println(d1);
		
	    double g1 = 12.9;
		int i1 = (int)g1;
		System.out.println(i1);
		long l1 = 123;
		short s2 = (short)l1;
		int i2 = 128;
		byte b2 = (byte)i2;
		System.out.println(b2);
		*/
		//
		//long i1 = 123456;//正常long型数据数值后应添加L或者l;不加也正常;没加的话表示自动类型提升操作;默认是int 有赋值给long型
		//System.out.println(i1);
		//long l1 = 12345678998l;
		//System.out.println(l1);
		byte q = 12;
		byte b1 = q + 1;// 编译失败
		System.out.println(b1);
		float f1 = b + 12.3;// 编译失败
		System.out.println(f1);
	
	}
}

  

  

 

Java基本数据类型的运算

标签:结果   数据类型   示例   void   基本数据类型   bsp   特殊   错误   不兼容   

原文地址:https://www.cnblogs.com/rdchenxi/p/13195621.html


评论


亲,登录后才可以留言!