Java中基本类型

本文介绍了Java中的基本数据类型及其大小,并详细解释了自动类型转换和强制类型转换的概念及用法,通过示例代码展示了不同类型的转换过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Java中的基本类型


求取代码如下:

		System.out.println(Byte.SIZE);
		System.out.println(Character.SIZE);
		System.out.println(Short.SIZE);
		System.out.println(Integer.SIZE);
		System.out.println(Long.SIZE);
		System.out.println(Float.SIZE);
		System.out.println(Double.SIZE);
		
		System.out.println(Byte.MAX_VALUE);


2.类型转换

      a)自动转换:

          程序执行过程中悄然进行的转换,不需要用户提前声明,一般是从位数低的类型转换为位数高的类型;

          优先级如下;(byte,short,char)->int->long->float->double

       b)强制类型转换

          在代码中声明,要在需要转换的类型前加上“()”,括号内填写需要转换成的类型。

测试代码如下:

class Demo{
	//char to int :auto exchange
	int fun1(){
		char i='a';
		return i;
	}	
	//byte to short :auto exchange
	short fun2(){
		byte i=1;
		return i;
	}
	//short to byte :cast exchange
	byte fun3(){
		short i=1;
		return (byte) i;
	}
    //char to byte :cast exchange
	byte fun4(){
		char i='a';
		return (byte) i;
	}
	//byte to char :cast exchange
	char fun5()
	{
		byte i=1;
		return (char) i;
	}
	//char to short :cast exchange
	short fun6(){
		char i='a';
		return (short) i;
	}
	//short to char :cast exchange
	char fun7()
	{
		short i=1;
		return (char) i;
	}
	//int to float :auto exchange
	float fun8(){
		int i=1;
		return 1;
	}
	//long to float :auto exchange
	float fun9(){
		long i=1;
		return i;
	}
	//double to float :cast exchange
	float fun10(){
		double i=1;
		return (float) i;
	}
}


参考链接:

http://www.cnblogs.com/doit8791/archive/2012/05/25/2517448.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值