编程语言
首页 > 编程语言> > 跟韩老师学Java第三天(章)-变量

跟韩老师学Java第三天(章)-变量

作者:互联网

变量

有图的笔记可以直接看码云

值得注意的概念

  1. Java数据类型
  2. 数据类型转换
  3. ASIIC表
  4. 基本数据类型与String类型转换
  5. 作业

1

图(必记)

2

2.1 数据类型转换-自动转换

float d1 = n1 + 1.1;//错,因为浮点默认是double
int n2 = 1.1;//错,因为不可大到小
byte b2 = n2;//错,变量赋值需判断类型,整型比byte”高一级“

byte b1 = 10;
char c1 = b1;//错,byte不可自动转char
byte b1 = 1;
byte b3 = 2;
short s1 = 1;
short s2 = b2 + s1;//错
int s2 = b2 + s1;//对
boolean pass = true;
int num = pass;//错

2.2 数据类型转换-强制转换

int y = (int)8.8;

char c1 = 100;//正确

int m = 100;
char c2 = m;//错

3

4

4.1 基本型转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 + "";

4.2 Stirng转基本型

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);
short num6 = Short.parseShort(s5);
boolean b = Boolean.parseBoolean("true");

4.3 Stringchar

String s1 = "Hello";
char c1 = s1.charAt(0);
System.out.println(c1);//输出 H

作业

  1. as
n3 = 30
n5 = 8
  1. as
public class myHomework2 {
	public static void main(String args[]) {
		char c1='\n';
		char c2='\t';
		char c3='\r';
		char c4='\\';
		char c5='1';
		char c6='2';
		char c7='3';
		System.out.println(c1);
		System.out.println(c2);
		System.out.println(c3);
		System.out.println(c4);
		System.out.println(c5);
		System.out.println(c6);
		System.out.println(c7);
	}
}
  1. 不要把"\t"写成'\t'否则会显示数字
    • 这是因为单引号是字符串,字符串的相加实际上是对应ASCII码相加
    • System.out.println('a'+'b');相当于97+98故显示195
public class myHomework3 {
	public static void main(String args[]) {
		String book1="小学语文",book2="初中物理";
		char sex1='男',sex2='女';
		double price1=17.2,price2=8.29;
		System.out.println(book1+"\t"+book2);
		System.out.println(sex1+"\t"+sex2);
		System.out.println(price1+"\t"+price2);
	}
}
public class myHomework4 {
	public static void main(String argsp[]) {
		String name="张三",hobby="玩游戏";
		int age=12;
		float grade=59;
		char sex='男';
		System.out.println("姓名\t\t年龄\t成绩\t性别\t爱好\n"
			+name+"\t\t"+age+"\t"+grade+"\t"+sex+"\t"+hobby);
	}
}

标签:Java,String,int,第三天,System,char,println,out,韩老师
来源: https://www.cnblogs.com/maritimeclimate/p/15857213.html