实训第二天,Java数据类型转换

package demo02;

public class type {
/*1.关键字全部小写
 * 严格区分大小写
 * 八种基本数据类型:byte short int long float double char boolean
 * 引用数据类型:类,数组,接口,集合等,必须使用new关键字创建
 * */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1,-128至127
		byte b=120;
		//b=128则错误,右边赋值的数字不能超过左侧变量类型的范围
		System.out.println(b);
		long l=10000L;//后面加L或l方为long类型,否则默认为int类型
		System.out.println(l);
		//小数
		double d=0;
		System.out.println(d);//输出为0.0,默认改为小数
		float f=100F;//flaot后要加F或f,否则默认double
		//字符型char,使用较少,char为单引,String为双引
		char c='a';
		String s="abc";
		System.out.println(s);
		//布尔值
		boolean  b1=true;
		//基本数据类型转换分为强制类型转换与自动
		short s1=100;
		int num=s1;//无需强转将小箱子内容放到大箱子里
		int num1=20;
		short s2=(short)num1;
		System.out.println(s2);//强制类型转换
		String str="123";
		int i=Integer.parseInt(str);//将字符串转换为int
		System.out.println(i);//注并非所有字符串都能转换为int,前提是数字
		System.out.println("=================");
		int num4=100;
		String str1=String.valueOf(num4);
		System.out.println(str1);
		//第二种方法
		int num5=200;
		String str3=200+"";
		System.out.println(str3);
		
		
		
	}

}

发布了58 篇原创文章 · 获赞 145 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/pengxiang1998/article/details/103733071