java基础课堂随记2
类型转换
java是强类型语言,所以进行有些运算的时候需要用到类型转换
低----------------------------------------------->高
byte,short,char->int->long->float->double
运算中,不同类型的数据先转化为同一类型,然后再运算
public class demo2 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//内存溢出
//强制转换 (类型)变量名 高--低
//走动转换 低--高
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.在把高容量转换到低容量的时候,强制转换
4.转化的时候可能存在内存溢出,或者精度问题
*/
System.out.println("=================================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);
//操作比较大的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
System.out.println(money);
int year = 20;
int total = money*year;
System.out.println(total);//结果是-1474836480,计算的时候溢出了
long total2 = money*year;//默认是int类型,输出还是跟上面一样,转换之前已经存在问题了
System.out.println(total2);
long total3 = money*((long)year);
System.out.println(total3);
}
}
输出结果:
128
-128
23
-45
1000000000
-1474836480
-1474836480
20000000000
变量
每个变量 必须声明其类型
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
注意事项:
每个变量有类型,类型可以使基本类型,也可以是引用类型
变量名必须是合法的标识符
变量声明是一条完整的语句,因此每一个声明都必须以分号结束
int a=1,b=2,c=3;//不建议写成这种形式,程序可读性不好
int a=1;
int b=2;
int c=3;//最好写成这种形式
String name = "hjh";
char x = 'X';
double pi = 3.14;
变量作用域
-
类变量
-
实例变量
-
局部变量
常量
常量:初始化之后不能再改变的值
常量可以理解成一种特殊的变量,它的值被设定之后,在运行过程中不允许被改变。
final 常量名=值;
常量名一般使用大写字符
变量的命名规范
所有变量、方法、类名:见名知意
类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
局部变量:首字母小写和驼峰原则
常量:大写字母和下划线:MAX_VALUE
类名:首字母大写和驼峰原则,比如Demo,我就写成了demo
方法名:首字母小写和驼峰原则run()
运算符
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//crtl+D,复制当前行到下一行
int a =10;
long b =123468732;
short c =25;
byte d =5;
System.out.println(a+b+c+d);
System.out.println(a+c+d);
System.out.println(c+d);
System.out.println(a/(double)b);
}
}
结果:
123468772
40
30
8.099216569260629E-8
public class Demo2 {
public static void main(String[] args) {
//++ -- 自增,自减, 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a++ a=a+1
//++a a=a+1
int c = ++a;//执行完这行代码前,先自增,再给b赋值
//++a
System.out.println(a);
System.out.println(a);
System.out.println(b);
System.out.println(b);
System.out.println(c);
System.out.println(c);
//幂运算,2^3 2*2*2, 很多运算是使用工具类来操作的
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
结果:
5
5
3
3
5
5
8.0
package operator;
//逻辑运算符:与或非
public class Demo3 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b));
System.out.println("a || b:"+(a||b));
System.out.println("!(a&&b):"+!(a&&b));
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);//输出为5,从这里可以看出,后面的c++没有被执行,直接看到前一个c<4就判断false了
}
}
结果:
a && b:false
a || b:true
!(a&&b):true
false
5
public class Demo4 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 与
A|B = 0011 1101 或
A^B = 0011 0001 异或
~B = 1111 0010 非
2*8 = 16 ,2*2*2*2, 怎么算得快?
用位运算效率极高
<< 左移 相当于*2
>> 右移 相当于/2
0000 0000 0
0000 0001 1
0000 0010 2
*/
System.out.println(2<<3);
}
}
输出结果 16
public class Demo5 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
a-=b; //a = a-b
System.out.println(a);
//字符串连接符 + , String,按照最前面的类型转换
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
结果:
10
1020
30
三元运算符
public class Demo6 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格" : "及格";//必须掌握,可以让代码更加精简
System.out.println(type);
}
}
结果:及格
不管运算符是什么优先级,我们写代码的时候都最好用括号把他们括起来,这样方便阅读,也省的出错。

浙公网安备 33010602011771号