数据转换类型
public class demo04 {
public static void main(String[] args) {
int q =130;
byte w = (byte)q;
System.out.println(q);
System.out.println(w);//内存溢出
/*强制转换 (类型)变量名 高到低
自动转换 低到高 */
System.out.println("==========================");
int e =123;
double r =e;
System.out.println(e);
System.out.println(r);//自动转换为double后为128.0
/*注意点:
1.不能对布尔值进行转换
2.不能转换成不相关的东西
3.在把高容量转换成低容量的时候,强制转换
4.转换时可能存在精度问题和内存溢出问题
*/
System.out.println("==========================");
char i = 'a' ;
float u = i+1;
System.out.println(i);
System.out.println(u);
System.out.println((char) u);
}
}
变量
public class demo05 {
public static void main(String[] args) {
//操作比较大的数字时,注意溢出问题
//数字可以用下划线隔开,jdk7.0特性
int money =10_0000_0000;
int years =20;
int total1 =money*years;//输出-1474836480,因为还是int类型
long total2 = money*years;//输出-1474836480,因为计算的时候就已经出现问题,默认类型int
long total3= money*((long)years);//计算的时候就要转换类型,先把一个数转换为long
System.out.println(total1);
System.out.println(total2);
System.out.println(total3);
//大写 L 小写 l 小写的l容易看成1,一律用大写的L
}
}
public class demo06 {
public static void main(String[] args) {
//int a,b,c;
//int a=1,v=2,c=3;//程序可读性,不建议一行定义多个变量
String number ="liuliuliu";
char p = 'x';
double d =545;
//变量作用域
}
}
public class demo07 {
//类变量
static double salary =2500;
//属性:变量
//实例变量;从属于对象;
//布尔值 默认 false
//除了基本变量,其余默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量,必须声明和初始化
int i =10;
//变量类型 变量名字=new demo07
demo07 Demo07= new demo07();
System.out.println(Demo07.age);
System.out.println(Demo07.name);
System.out.println(salary);//demo07下才能在方法里使用类变量
}
//其他方法
public void add(){
//System.out.println(salary);这里没有引入demo07,用不了
}
}
常量
public class demo08 {
//常量
//修饰符,不存在先后顺序
final static double PI =3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
变量声明标准
常用的类名要首字母大写和驼峰原则,常量大写加下划线,类成员首字母小写加驼峰原则,局部变量和方法名都和类成员同