public class bitter01 { public static void main(String[] args) { //整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x int i = 10; int i2 = 010;//八进制0 int i3 = 0x10;//十六进制0x System.out.println(i); System.out.println(i2); System.out.println(i3); System.out.println("============================="); //======================================= // 浮点数拓展? 银行业务怎么表示? 钱 // BigDecimal 数学工具类 //float 有限 离散 舍入误差 大约 接近但不等于 //double //最好完全避免浮点数进行比较 //最好完全避免浮点数进行比较 //最好完全避免浮点数进行比较 float f = 0.1f;//0.1 double d = 1.0 / 10;//0.1 System.out.println(f == d);//false System.out.println(f); System.out.println(d); float d1 = 235512333444f; float d2 = d1 + 1; System.out.println(d1 == d2); //======================================= // 字符类拓展 char c1 = 'a'; char c2 = '中'; System.out.println(c1); System.out.println((int) c1);//强制转换 System.out.println(c2); System.out.println((int) c2); //所有的字符本质还是数字 //编码 Unicode 表 97=a 65=A 2个字节 0- 65536 excel 2 16=65536 //U0000 UFFFF char c3 = '\u0061'; System.out.println(c3);//a //转义字符 // \t 制表符 \n 换行 System.out.println("hello\tworld"); String sa = new String("hello world"); String sb = new String("hello world"); System.out.println(sa == sb); String sc = "hello world"; String sd = "hello world"; System.out.println(sc == sd); //对象 从内存分析 // 布尔值扩展 boolean flag = true; if (flag == true) { }//新手 if (flag) { }//老手 //Less is More! 代码要精简易读 }}
切记银行一定要避免使用浮点型数据类型
浮点型数据最好不要进行比较
public class bitter02 { public static void main(String[] args) { int i = 128; double b = i;//内存溢出 //强制转换 (类型)变量名 低-----高 //自动转换 高-----低 System.out.println(i); System.out.println(b); /* * 注意点: * 1.不能对Boolean值进行转换 * 2.不能把对象类型转化成不相干的类型 * 3.再把高容量转化成低容量的时候,强制转换 * 4.转换的时候可能存在内存溢出,或者精度问题! * */ System.out.println((int )23.7); System.out.println((int)-45.89f); char c = 'a'; int d = c+1; System.out.println(d); System.out.println((char)d); } }
类型转换
public class bitter03 { public static void main(String[] args) { //操作较大的数时,注意溢出问题 //JDK7新特性,数字可以用下划线分割 int money = 10_0000_0000; int years = 20; int total = money*years;//-1474836480 计算的时候溢出了 long total2 = money*years;//默认是int,转换之前已经出现问题了。 long total3 = money*((long)years); System.out.println(total); System.out.println(total2); System.out.println(total3); //L l } }
public class bitter04 { //类变量 static static double salary=2500; //属性: 变量 //实例变量;从属于对象; 如果不进行初始化,就会变成这类型的默认值 0 0.0 u0000 //布尔值:默认为false //除了基本类型,其余的都是null; String name; int age; //main方法 public static void main(String[] args) { //int a,b,c; 程序可读性一定要顺畅 //局部变量;必须声明和初始化值 int i=10; System.out.println(i); //变量类型 变量名字 = new bitter04() bitter04 bitter04 = new bitter04(); System.out.println(bitter04.age); System.out.println(bitter04.name); //类变量 static System.out.println(salary); } //其他方法 public static void add() { } }
变量命名是尽量是驼峰原则 lastname ->lastName
除了第一个单词之外,后面的单词首字母大写
常量尽量都是大写+下划线“_”
类名:首字母大写+驼峰原则
方法名:首字母小写+驼峰原则
局部变量+类成员变量:首字母小写+驼峰原则
public class bitter05 { //修饰符,不存在先后顺序 static final double pi = 3.14; public static void main(String[] args) { System.out.println(pi); } }
public class bitter03 { public static void main(String[] args) { //关系运行算符返回的结果: 正确 错误 布尔值 //取余 也叫模运算 int a=10; int b=20; int c=21; System.out.println(c%a);//c/a 21/10 ---1 System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); }
public class bitter05 { public static void main(String[] args) { // ++ -- 自增 自减 一元运算符 int a = 3; int b = a++;//执行完这行代码后,先给b赋值,在自增。 // a++ a = a + 1 System.out.println(a); // ++a a = a + 1; int c = ++a;//执行完这行代码钱,先自增,在给c赋值。 System.out.println(a); System.out.println(b); System.out.println(c); //幂运算2^3 2*2*2 = 8 很多运算我们会使用一些工具类来操作 double pow = Math.pow(2,3); System.out.println(pow); } }
public class bitter06 { public static void main(String[] args) { // 与(and) 或(or) 非(取反) boolean a = true; boolean b = false; System.out.println("a && b"+(a && b));//逻辑与运算:两个变量都为真,结果才为true System.out.println("a || b"+(a || b));//逻辑或运算:两个变量有一个真,结果才为true System.out.println("!(a && b)"+!(a && b));//如果是真,则变为假,如果是假则变为真 //短路运算 int c=5; boolean d = (c<4) &&(c++<4); System.out.println(d); System.out.println(c); }
public class bitter07 { 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 * 0000 0011 3 * 0000 0100 4 * 0000 1000 8 * 0001 0000 16 * */ System.out.println(2<<3); } }
public class bitter08 { 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+""); }
//三元运算符 public class bitter09 { public static void main(String[] args) { // x ? y : z //如果x==true ,则结果为y,否则结果为z int score = 80; String type = score < 60 ?"不及格":"及格";//必须掌握 //if System.out.println(type); }
包机制
包的作用就是一个文件夹的作用
一般利用公司域名倒置作为包名;