0
点赞
收藏
分享

微信扫一扫

Java变量

陌岛 2022-03-17 阅读 49

JAVA变量复习FX

文章目录

一、变量

  • 变量按数据类型来分:

    • 基本的数据类型:整型byte\shor\int\long
      浮点型:float\double
      字符型:char
      布尔型:boolean

    • 引用数据类型:
      类(class)
      接口(interface)
      数组(array)

  • 变量在类中声明的位置:

    ​ 成员变量 VS 局部变量

class Sgg 
{
	public static void main(String[] args) 
	{
		long l1=3414234324L;

		System.out.println(l1);

		double d1=123.3;
		System.out.println(d1+1);
//定义float类型变量时,变量要以“f”“F”结尾
//float表示数值的范围比long还大;
		float f1=12.3F;
		System.out.println(f1+1);
//转义字符
char c5='\n';//换行符
System.out.print("hello"+c5);
System.out.println("world");
//Unicode编码集
	char c6 = '\u0043';
	System.out.println(c6);
	}
}

二、基本数据类型之间的运算规则:

2.1自动类型提升

  • 说明:此时的容量大小指的是,表示数的范围的大和小,比如:float容量要大于long的容量
class  SggZhuan{
	public static void main(String[] args){
		
		byte b1=2;
		int i1=129;
		//byte b2=b1+i1;int 转换 byte可能会损失;
		int i2=b1+i1;
		long i3=b1+i1;
		float i4=b1+i2;
		System.out.println(i2);
		System.out.println(i3);
		System.out.println(i4);
		short s1=123;
		double d1=s1;
		System.out.println(d1);

		//*************特别的 *********************
		char c1='a';//97
		int i5=10;
		int i6=c1+i5;
		System.out.println(i6);
		
		short s2=10;
		//char s3=c1+s2;//编译失败

		byte b2=10;
		//char c3=c1+b2;//编译失败

		//short s3=b2+s2;//编译失败

		//***********************************
	}
}
  • 结论:当容量小的数据类型的变量与容量大的数据类型的变量做运算时,结果自动提升为容量大的数据类型;
    byte、char、short–>int–>float–>double

  • 特别的:当byte、char、short三种类型的变量做运算时,结果为int类型;

2.2强制类型转换

class SggqZhuan{
	public static void main(String[] args) 
	{
		double d1=12.3;
		int i1=(int)d1;//截断操作
		System.out.println(i1);
		
		//没有精度损失
		long l1=123;
		short s2=(short)l1;
		//精度损失
		int i2=128;
		byte b=(byte)i2;
		System.out.println(b);//-128
	}
}
  • 强制类型转换:自动类型提升运算的逆运算。
  • 需要使用强转符;()
  • 注意点:强制类型转换,可能会导致精度损失

三、变量运算俩个特殊情况

class SggByun{
	public static void main(String[] args){
	//1.编码情况
	long l=123213;
	System.out.println(l);  
	//编译失败,过大的整数;
	//long l1=21361456341526632;
	long l1=2136145634152645632L;

	//************************************
	//编译失败,没加后缀F,默认是double类型,转float类型,大转小
	//float f1=12.3;
		
	
	//2.编码情况2:
	//整型常量,默认为int型
	//浮点型常量,默认类型为double型
	byte b=12;
	//byte c=b+1;编译失败,1为常量,默认是int型

	//float f1= b+12.3;//编译失败,12.3为浮点型,默认是double型
	

	}
}

四、String类型变量的使用

  • String属于引用数据类型,字符串
  • 声明String类型变量时,使用一对”“
  • String可以和8种基本数据类型变量做运算,且运算只能是连接运算 +
  • 运算的结果仍然是String类型
class SggYin{
	public static void main(String[] args){
		
		String s1="niubi haha";

		System.out.println(s1);

		String s2="a";
		String S3="";

		//char c='';编译不通过
		//**********************************

		int number=1001;
		String numberStr="学号:";
		String info=numberStr+ number;//+:连接运算
		boolean b1=true;
		String info1=info+b1;//+:连接运算
		System.out.println(info1);
	}
}
	
  • 练习一:
		char c='a';
		int num=10;
		String str="hello";
		System.out.println(c+num+str);//107hello;
		System.out.println(c+str+num);//ahello10
		System.out.println(c+(num+str));//a10hello
		System.out.println((c+num)+str);//1o7hello
  • 练习二:
		System.out.println("*	*");//*	*
		System.out.println('*'+'\t'+'*');//93
		System.out.println('*'+"\t"+'*');//*	*
		System.out.println('*'+'\t'+"*");//51*
		System.out.println("*"+('*'+"*"));//***
举报

相关推荐

0 条评论