二、java变量

public class VarDetail{ 
      //编写一个main方法
	public static void main(String[]args) {
      //变量必须先声明,后使用,即有顺序
      //System.out.println(a);×先输出错误
       int a = 50; 
       System.out.println(a);
       //
       //int a = 77 错误 变量在同一个作用域不能重名
       a = 80;
       System.out.println(a);

	}
}

 


程序中 +号的使用

“ ”双引号中 字符串 引用类型


数据类型

 

 


整型

1字节等于8比特

long型常量需加L

 

 


浮点数

 float型常量需加f

 

   //Java的浮点数常量(具体量)默认为double型,声明为float型常量,须后加'f'或'F'
	//float num1 = 1.1;默认1.1是double 要用float 存必须加1.1f
		float num2 = 1.1f;
		double num3 = 1.1;
		double num4 =1.1f;
		/*对 因为 double为8字节 float 为4字节 ,
		4字节放入8字节没毛病*/
		System.out.print(5.12e2);//512.0 double类型所以会有.0
		System.out.print(5.12E-2);//0.0512 

double的精度更高 推荐用double

浮点数注意除法警告✅ (计算出来的数无法相等比较,附的值可以比较)

    	double num11 = 2.7;
		double num12 = 8.1 / 3;//2.666666666669 主机计算不知道8.100000000002
		System.out.println(num11);
		System.out.println(num12);
		if (num11 == num12){
			System.out.println("相等");
		}
		
		if(Math.abs(num11 - num12) < 0.000001){
			System.out.println("差值非常小,到我的规定精度,认为相等");
		}
		System.out.println(Math.abs(num11 -num12));
		//细节:如果直接查询得到到小数或者直接赋值,是可以判断相等的


 

api使用

在线api www.matools.com 1.类->包—>方法 2.索引

 


类型

1.sublime 快捷键 修改


 

不能用双引号引起来 (双引号为字符串) 单引号为字符

3.转义字符

// \t :一个制表位,实现对齐的功能

// \n :换行符

// \\ :一个\

// \" :一个"

// \' :一个'

// \r :一个回车 System.out.println("韩顺平教育\r 北京");

public class ChangeChar{ 
	public static void main(String[]args) {
		System.out.print("北京\t天津\t广州\t");

		System.out.print("\njack\nsmith\nmary\n");

		System.out.print("\nC:\\Windows\\System32\\cmd.exe");

		System.out.print("\nC:\\\\Windows\\\\System32\\\\cmd.exe");
         
        System.out.print("\n老韩说:\"学习java有前途\"");

        System.out.print("\n老韩说:\'学习java有前途\'\n");

        System.out.print("韩顺平教育\r北京\n");//北京平教育

        System.out.print("书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000");
        
	}

 

public class CharDetail{ 
	public static void main(String[]args) {
         char c1 = 'a';
         char c2 = '\t';
         char c3 = '韩';
         char c4 = 97 ;
         System.out.println(c1);//a
         System.out.println(c2);//
         System.out.println(c3);//韩
         System.out.println(c4);//当输出C4时候,会输出97表示的字符 a
         char c5 = 'a';
         System.out.println((int)c5);//97
         char c6 = '韩';
         System.out.println((int)c6);//38889
         char c7 = 38889;
         System.out.println(c7);//韩
         char c8 = 'b'+ 1;
         System.out.println((int)c8);//99
         System.out.println(c8);//c
	}

}

字符类型在计算机如何存储 字符编码表

 布尔类型

 自动转换

自动数据类型转换 精度小 >>> 精度大


 

'c'为字符char型 转化为int 正确

80为int 型 可以转化为double 正确

public class AutoConvert{ 
	public static void main(String[]args){
		//自动转换类型
       int num ='a'; // ok char ->int 
       double d1 = 80; // ok int ->double
       System.out.println(num);//97 
       System.out.println(d1); //80.0
	}

}


其他细节

 

 

public class AutoConvertDetail{ 
	public static void main(String[]args){
		//🔴细节1:
		//有多种类型的数据混合运算时
		//系统首先自动将所有数据转换成容量最大的那种数据类型然后再进行计算
		int n1 = 10 ;//ok
		//float d1 = n1 + 1.1;//错误 n1 + 1.1 => 结果类型是 double
		//double d1 = n1 + 1.1;//对  n1 + 1.1 => 结果类型是 double
		float d1 = n1 + 1.1F; //对   n1 + 1.1 => 结果类型是 float
		//
		// 🔴细节2:
		// 当我们把精度容量大的数据类型赋值给精度容量小的数据类型时
		//  就会报错,反之就会进行自动类型转换
		//  
		// int n2 = 1.1; //错误 double -> int
		// 
		// 
		//🔴 细节3:
		// (byte,short )和 char 之间不会互相自动转换
		// 当把具体数赋给 byte时,(1)先判断该数是否在byte范围内,如果是就可以  (byte 1字节)
		byte b1 = 10; //对 , -128 -127 如果是 byte b1 =1000; 错误(超出范围)
		int n2 = 1; //n2 是int
		//byte b2 =n2 ; //错误 原因:如果是变量赋值,
		//判断变量类型 int n2 (4字节) 不可以自动转换为 byte b1 (1字节) 
	   	//
	   	//char  c1 =b1 ;错误  b1 为  byte    c1为 char  不可以互相自动转换
	   	//
	   	//🔴细节4:byte ,short,char 三者参与运算,再计算时首先转换为int类型
	   	//
	   	byte b2 = 1;
	   	byte b3 = 2;
	   	short s1 = 1;
	   	//short s2  = b2 + s1 ;//错, b2 + s1 => int
	   	int s2 = b2 + s1;
	   	//byte b4 = b2 +b3;
	   	//
	   	//🔴细节 5:
	   	//boolean 不参与转换
	   	boolean pass = true;
	   	//int num100 =pass; boolean 不参与类型的自动转换
	   	//
	   	//🔴细节6: 
	   	//自动提升原则: 表达式结果的类型自动提升为 操作数中最大的类型
	   	//
	   	byte b4 = 1;
	   	short s3 = 100;
	   	int num200 = 1;
	   	double num300 = 1.1f;
	   	//int num500 = b4 + s3 + num200 + num300;错 
	   	//double num500 = b4 + s3 + num200 + num300;对 
	   	//这其中最大数据类型应为double 
 	}

}

n2 为int 型 4字节 不能自动转换为byte b1 1字节

(byte ,short )和 char 不能自动转换

byte 赋值 先判断范围 -128到127 内 就可以

byte short char 三者只要参与运算 就会转换为int

boolean 不参与运算

最大类型运算原则


强制类型转换 精度大 >>> 精度大

public class ForceConvert{ 
	public static void main(String[]args){
		int n1 =(int)1.9;
		System.out.println("n1="+ n1 );//n1=1,造成精度损失
		int n2 = 2000;
		byte b1 = (byte)n2;
		System.out.println("b1="+ b1);//b1= -48 ,造成数据溢出
	}

}

 其他细节

public class ForceConvertDetail{ 
	public static void main(String[]args){
		//演示强制类型转换
		//🔵细节1 :
		//强制符号只针对最近的操作数有效,往往会使用小括号提升优先级
		//
		//int x = (int)10*3.5+6*1.5;// 编译错误:double -> int  
		//强制转换符先和10运算 最后结果是40.0 double(8字节) 类型 转换为 int (4字节)类型 错误;
		int x = (int)(10*3.5+6*1.5);// int 44.0 -> 44
		System.out.println(x);//44
       	//🔵细节2:
       	//char类型可以保存int的常量值,不可以保存int的变量值
       	//常量和变量的区别:变量是“可读、可写”,而常量是“只读”的
		 char c1 = 100; //ok
		 int m = 100; //ok
		 //char c2 = m; //错误 int(4字节)类型不能自动转换为 char(2字节) 
		 char c3 = (char)m; //ok  强制将100 转换为char 类型
		 System.out.println(c3);// 输出100对应的字符
		//🔵细节3: 
		//byte 和short,char类型在进行运算时,当做int类型处理
	}

}

练习

 

public class Pratice{ 
	public static void main(String[]args) {
         //1.👁️
         short s =12 ;//ok
         //s= s- 9; // int -> short 错
         // s为short(2字节)类型 参与减法运算变成int(4字节)类型✅
         //System.out.println(s);// 我的答案 3 错误 
         //2.👁️
         byte b =10;//ok
         //b= b + 11;
         //错,s为byte(1字节)类型 参与减法运算变成int(4字节)类型
         b=(byte)(b + 11); //ok
         //对,b和11运算后为int 类型 被强制转换为 byte类型
         //System.out.println(b);//我的答案 22 错误 
 	      //3.👁️
         char c ='a';//ok
         int i =16;//ok
         float d =.314F;//ok F不可省略
         double result =c +i + d;
         System.out.println(result);
         //我的答案 113.314 原答案double精度更好 答案为:113.31400299072266
         //4.👁️
         /*byte b =16;  //ok
         short s =14; //ok
         short t =s +b; */
         // 错误 short 和byte 运算变成 int类型 int ->short 错误
         //System.out.println(t);//我的答案 30❌
	}

}

基本数据类型和String类型的转换

 

基本类型转String类

定义Sting类 +“ ” 即可

Srting类转基本类型

用基本类的包 中的方法

注意事项

public class StringToBasic{ 
	public static void main(String[]args) {
      // other->string
	 int n1 = 100;
	 float f1 =1.1F;
	 double d1= 4.5;
	 boolean b1 =true;
	 String s1 = n1 + " ";
	 String s2 = f1 + " ";
	 String s3 = d1 + " ";
	 String s4 = b1 + " ";
	 System.out.println(s1  + " "+s2  + " "+s3  + " "+s4  + " ");


	 //String -> other
	 String s5 ="123";
	 int num1 =Integer.parseInt(s5);
	 double num2 = Double.parseDouble(s5);
	 float num3 = Float.parseFloat(s5);
	 long num4 =  Long.parseLong(s5);
	 byte num5 = Byte.parseByte(s5);
 	 boolean b = Boolean.parseBoolean("true");
	 short num6 = Short.parseShort(s5);
	 System.out.println("===================");
	System.out.println(num1);//123
	System.out.println(num2);//123.0
	System.out.println(num4);//123
	System.out.println(num3);//123.0
	System.out.println(num5);//123
	System.out.println(num6);//123
	System.out.println(b);//true
/ 怎么把字符串转成字符 char -> 指 含义是指 把字符串的第一个字符得到
 //解读 s5.charAt(0) 得到 s5 字符串的第一个字符 '1'  String s5 ="123";
  System.out.println(s5.charAt(0));
	}

}

字符串 hello 转int

格式不正确,就会 抛出异常,程序就会终止

本章作业

 

3.

  String book1 = "天龙八部";
      String book2 = "xiyouji";
      char c1 = '男';
      char c2 = '女';
      double cost1 = 100.1;
      double cost2 = 100.2;
      System.out.println(book1 + book2);
      System.out.println(c1 + c2);
      System.out.println(cost1 + cost2);

4.

String a1 ="小明";
      int age1 = 23;
      double b1= 100.0;
      char c3= '男';
      String a5 = "打篮球";
      System.out.println("姓名\t年龄\t成绩\t性别\t爱好\n"
            +a1+"\t"+age1+"\t"+b1+"\t"+c3+"\t"+a5);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值