太懒了,多复盘,多回顾,多敲。
晚上睡前一定要留时间复盘。
复盘这一天的心得体会、待人接物,
复盘敲得代码。
今天内容:变量。
代码蛮多的
1.byte/int/float的加减
```java
```java
class VariableTest2 {
public static void main(String[] args) {
byte b1 = 2;
int i1 = 129;
//编译未通过 byte b2 = b1 + i1;
int b2 = i1 + b1;
System.out.println(b2);
long l1 = i1 + b1;
float f = b1+ i1;
//整形会自动补个0
short s1 = 123;
double d1 = s1 + 1;
System.out.println(d1);
System.out.println("Hello World!");
}
}
2.
class VariableTest3
{
public static void main(String[] args)
{
double d1 = 12.3;
//精度损失示例1
int i1 = (int)d1;
//截断操作
long l1 = 123;
short s2 =(short)l1;
//没有精度损失
int i2 = 128;
byte b = (byte)i2;
//精度损失示例2
System.out.println(i2);
}
}
3.两种特殊情况
class VariableTest4 {
public static void main(String[] args) {
//1.编码情况
long l =123213;
System.out.println(l);
//编译失败:整数过大
long l =1232132121212121;
long l1 = 213213213456l;
//编码失败
//float f1 = 12.33;
//2.编码情况2
//整型常量,默认为int型
//浮点型常量,默认为double型
byte b = 12;
byte b1 = b + 1;
//编译失败
}
4.字符串的加减+string与char/int 区别
class StringTest
{
public static void main(String[] args) {
/*
String s1 = "Hello n";
String s2 = "=a=";
String s3 = "";
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
char c = ' ';//如果不加空格,编译不通过
System.out.println(c);
int number = 1001;
String numberStr = "学号:";
String info = numberStr + number;//+:连续运算
boolean b1 = true;
String info1 = info + b1;//+:连续运算
System.out.println(info1);
*/
char c = 'a';//a=97,A=65
int num = 10;
String str = "hello";
System.out.println(c + num + str);//107hello
System.out.println(c + str + num);//ahello10(string)
System.out.println(c + (num + str));//a10hello
System.out.println((c + num) + str);//107hello
System.out.println(str + num + c);//hello10a
System.out.println("* *");
System.out.println('*'+'\t'+'*');//char对应一个ASC码,前两个int,int+char
System.out.println('*'+"\t"+'*');//string在中间,保持连接,两边都是string
System.out.println('*'+'\t'+"*");//前两个还是int
System.out.println('*'+('\t'+"*"));//后边先运算,加出来string
}
}
string和其他类型的拼在一起还是string