Java学习第二天
- 类型转换
由于Java是强类型语言,所以要进行有些运算的时候需要用到类型转换。
低→------------------------------------------------→高
byte,short,char→int→long→float→double
自动转换:低→高
强制转换:高→低
public class Demo05 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
int money = 10_0000_0000;//JDK7新特性,数字之间可以用下划线分割
int year = 20;
int total = money*year;//-1474836480,计算的时候溢出了
long total2 = money*year;//-1474836480,说明在转换成long之前已经产生溢出
long total3 = money*(long)year;//20000000000,先把一个数转换为long
System.out.println(total);
System.out.println(total2);
System.out.println(total3);
}
}
- 变量
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
public class Demo08 {
//类变量 static
static double salary =2500;
//属性:变量
//实例变量:从属于对象,如果不自行初始化,类型默认值 0 0.0
//布尔值:默认是false
//除了基本类型,其余的默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值(作用在一对花括号内)
int i=10;
System.out.println(i);
//变量类型
Demo08 demo08 = new Demo08();//快捷键Alt+Enter
System.out.println(demo08.age);
System.out.println(demo08.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
- 常量(Constant)
初始化后不能再改变值!不会变动的值!
final 常量名=值;
final double PI=3.14;//常量名一般使用大写字符
public class Demo09 {
//static,final 修饰符,不存在先后顺序
static final double PI=3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
- 变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则,monthSalary
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线,MAX_VALUE
- 类名:首字母大写和驼峰原则,Man,GoodMan
- 方法名:首字母小写和驼峰原则,run(),runRun()
-** 运算符**
自增++:++在后,先赋值,再自增;++在前,先自增,再赋值
自减 --:–在后,先赋值,再自减;–在前,先自减,再赋值
幂运算:2^3 Math.pow(2,3) 很多运算会使用一些工具类
package operator;
public class Demo03 {
public static void main(String[] args) {
double pow=Math.pow(2,3);
System.out.println(pow);
}
}
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D :复制当前行到下一行
int a = 10;
int b = 20;
int c = 30;
int d = 40;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123213L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);
System.out.println(b+c+d);
System.out.println(c+d);//cannot cast 'short' to 'java.lang.String'不能将short转换成long类型
}
}
- 逻辑运算符
package operator;
public class Demo05 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(b&&a));//逻辑与:全真为真。
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,没有进行自增运算,被短路
}
}
- 按位运算
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
--------------
A&B = 0000 1100 按位与,两个数的二进制同时为1,结果才为1,否则为0
A|B = 0011 1101 按位或,有1则为1
A^B = 0011 0001 按位异或,两个相应位为”异“,结果为1,否则为0
<< 按位乘 *2
>> 按位除 /2
*/
System.out.println(2<<3);
}
}
- 拓展赋值运算符、字符串连接符、三元运算符
+=,-=,*=,/=
package operator;
public class Demo07 {
public static void main(String[] args) {
int a=10;
int b=20;
//字符串连接符 + ,String
System.out.println(""+a+b);//+字符串连接符两侧只要出现字符串类型,会将其他操作数转换成String类型
System.out.println(a+b+"");//30,前面先进行了运算
}
}
x?y:z //如果x==true,则结果为y,否则结果为z
package operator;
public class Demo08 {
public static void main(String[] args) {
int score = 50;
String type = score<60?"不及格":"及格";//三元运算符
System.out.println(type);
}
}
- 运算符的优先级
- 包机制
为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。包的本质就是文件夹
一般将公司域名倒置作为包名
package com.lixiaochuan.operator;
//导入这个包下所有的类
import com.lixiaochuan.base.*;
public class Demo08 {
public static void main(String[] args) {
int score = 50;
String type = score<60?"不及格":"及格";
System.out.println(type);
}
}