public class Demo01 { public static void main(String[] args) { //八大基础数据类型 int num1 = 10; // 最常用 byte num2 = 20; short num3 =30; long num4 =30L; // Long类型要在数字后面加个L //小数:浮点数 float num5 = 50.1F; //Lfoat类型要在数字后面加个F double num6 =3.1415926; //字符 char name ='巍'; //字符串,String不是关键字,类 //String name="巍"; //布尔值:是非 boolean flag = true; //bollean flag = false;
public class Demo02 { public static void main(String[] args) { //整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x int i =10; int i2 =010;//八进制 int i3 =0x10;//十六进制0x 0~9 A~F 16 System.out.println(i); System.out.println(i2); System.out.println(i3); System.out.println("========================================"); //================================== //浮点数拓展? 银行业务怎么表示?钱 //=================================== //float //double float f =0.1f;//0.1 double d =1.0/10;//0.1 System.out.println(f==d); System.out.println(f); System.out.println(d);
public class Demo03 { public static void main(String[] args) { //整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x int i =10; int i2 =010;//八进制 int i3 =0x10;//十六进制0x 0~9 A~F 16 System.out.println(i); System.out.println(i2); System.out.println(i3); System.out.println("========================================"); //================================== //浮点数拓展? 银行业务怎么表示?钱 //=================================== //float //double //最好完全使用浮点数进行比较 //最好完全使用浮点数进行比较 //最好完全使用浮点数进行比较 float f =0.1f;//0.1 double d =1.0/10;//0.1 System.out.println(f==d);// false float d1 = 2313131313131f; float d2 = d1+1; System.out.println(d1==d2); //true //字符拓展? //======================================== System.out.println("=============================="); char c1 ='A'; char c2 ='中'; System.out.println(c1); System.out.println((int)c1);//强制转换由char转化为int类型 System.out.println(c2); System.out.println((int)c2);//强制转换由char转化为int类型 //所以的字符本质还是数字 //编码 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\nWorld"); System.out.println("========================================"); 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 Demo04 { public static void main(String[] args) { int i = 128; byte b = (byte) i;//内存溢出 /* byte 1 字节 -128~127 * int 4 字节 -2 147 483 648~2 147 483 647 * short 2 字节 -32 768~32 767 * long 8 字节 -9 223 372 036 854 775 808 ~9 223 372 036 854 775 807 * byte short 类型主要用于特定的应用场合。例如:底层的文件处理或者存储空间很宝贵的 * */ //强制转换 (类型)变量名 System.out.println(i); System.out.println(b); /*注意点: 1.不能对布尔值进行转换 2.不能把对象类型转换为不相干对的类型 3.在把高容量转换为低容量的时候,强制转换 4.转换的时候可能存在内存溢出,或者精度问题 */ System.out.println("======================"); System.out.println((int)23.7);//23 System.out.println((int)-45.89f);//-45 System.out.println("======================="); char c ='a'; int d = c+1; System.out.println(d);//'a'代表一个字符 System.out.println((char) d);//把d进行强转把int-->char
public class Demo06 { public static void main(String[] args) { int money = 10_0000_0000; int years = 20; int tolal = money*years;//-1474836480,计算的时候溢出了 long tolal2 = money*years;//默认是int,转换之前已经存在问题? long total3 =money*((long)years);//先把一个数转换为Long System.out.println(tolal); System.out.println(tolal2); System.out.println(total3);
public class Demo07 { public static void main(String[] args) { int a=1,b=2,c=3; String name ="关巍"; char x ='X'; double pi =3.14; System.out.println(name); System.out.println(x); System.out.println(pi);
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); //变量类型 变量名字 =new Demo08(); Demo08 demo08 = new Demo08(); System.out.println(demo08.age); System.out.println(demo08.name); //类变量 static System.out.println(salary); }
public class Demo01 { public static void main(String[] args) { //二元运算符 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);
public class Demo02 { public static void main(String[] args) { long a =1212121212; int b =123; short c =10; byte d =8; System.out.println(a + b + c + d);//Long System.out.println(b + c + d);//Int System.out.println(c + d);//Int
public class Demo03 { public static void main(String[] args) { //++ -- 自增,自减 一元运算符 int a=3; int b=a++;//执行完这行代码后,先赋值给b,在自增 //a=a+1 System.out.println(a); //a++ a=a+1 int c=++a;//执行完这行代码前,先自增,再给b赋值 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 Demo04 { public static void main(String[] args) { //逻辑运算 //与(and) 或 (or) 非(取反) boolean a = true; boolean b = false; System.out.println("a && b" +(b&&a));//逻辑与运算:两个变量都为真,结果才为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 Demo05 { public static void main(String[] args) { /* A=0011 1100 B=0000 1101 ---------------------------- A&&B=0000 0000 A|B =0011 1101 A^B =0011 0001 ~B =1111 0010 2*8=16 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 Demo06 { 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 Demo07 { public static void main(String[] args) { int score =80; String type =score<60?"不及格;":"及格"; System.out.println(type); /* 算数运算符:+,-,*,/,%,++,-- 赋值运算符:= 关系运算符:<,>,<=,>=,==,!instanceof 逻辑运算符:&&,||,! 条件运算符:? 扩展赋值运算符:+=,-=,*=,/=, */