自家用近来的java小总结:(1)基本语法的查漏补缺~
首先,这是一篇自家用的文章,不对读者承担任何责任,请带着批判的眼光来看下面文章
这篇文章说了些什么?
这文章是我近来8.6号来在编程思想上打的代码,从0~200页的源码接近到在这里,下文正是总结这0~200页的的知识,涉及到接口,内部类.初始化,数值计算的一些细节.此文章不会一下子写完,可能隔一天可能再补下来.因为代码确实有点多..
注意
1 我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确),如果你确信我的内容的话,你可能会损失很大,因为我只是个菜鸟,我只是来补救一些知识的而已,如果不复制(最好自己打上)代码运行一次,可能会被我的在代码的注释所误解, 欢迎提意见和评论~~~你的意见和评论是我更新的动力(促进我更快滴读完编程思想)
本文适合读者
适合有一定的编程基础的人,因为我的代码可能出错,建议零基础的人找本书先看看,或者一定要复制我的代码运行一下,否则,后果将不堪设想 <( ̄▽ ̄)> 哇哈哈…因为我也只是新手
再一次提醒
下面的注释只是我个人的总结.
我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确)
,但是注释的语序凌乱各种天花百草不说了.!!!!绝非书本原话!!!!所以要最好还是把我的代码复制下来,运行一遍,也可以把我的注释删掉,或者提示我说应该怎么说,应该怎么说.
再一次感谢
1 感谢你可以抽出你的时间来进行下面阅读,思考的碰撞是智慧产生的前提.谢谢你的阅读和你愿意复制我的代码并阅读我那千奇百怪无比丑的注释来进行思考的碰撞!~!~
正文:
基本运算篇
/*
**
补血到的知识点:
算术运算符
关系运算符(“==”,“!==” 低于 “<,<=,>,>=")
&&
‖
条件运算符
赋值运算符 下面有具体的优先级表
*/
1 import java.util.Random; 2 3 public class Test 4 { 5 public static void main(String[] args) { 6 Random rand = new Random(47); 7 int i= rand.nextInt(100); 8 int j = rand.nextInt(100); 9 10 System.out.println("i = "+i); 11 System.out.println("j = "+j); 12 System.out.println("i>j is "+(i>j) ); // 注意这里 "i>j is" +i>j ; 这样写的话会报错,因为运算符+的优先级>j 13 System.out.println("i<j is " +(i<j)); 14 System.out.println("i>=j is " +(i>=j)); 15 System.out.println("i&j is " +(i&j)); 16 } 17 }
//注意表述+(i<j)那里要搞搞优先级
//单目运算最快 双目-->关系运算 逻辑运算 位运算 三目
一共有十五个优先级:
1 () [] . ->
2 ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) sizeof
3 * / % 算数运算
4 + -
5 >> <<
6 > >= < <=
7 == != 关系运算 (和关系很像)
8 & 位运算
9 ^
10 |
11 && 逻辑运算(二进制) (跟逻辑运算很像)?
12 ||
13 ?: 三木运算
14 = += -= *= /= %= |= ^= &= >>= <<= 赋值运算
15 ,
}
}
2.直接常量
1 public class Test 2 { 3 //所谓直接常量是编译器通过加一些字母来标志这个数字是什么数字 4 5 public static void main(String[] args) { 6 //8进制和10进制和 Integer.toBinaryString(变量);函数 7 //还有就是float f = 1f; double d =2d; 8 //还有就是byte short char 很有规律的 0x7f 0x7ff 0xffff 9 10 int i1 = 0x2f; 11 System.out.println("i1 ="+Integer.toBinaryString(i1)); 12 13 int i2 = 0X2F; //十六进制 14 System.out.println("i1 ="+Integer.toBinaryString(i2)); 15 16 int i3 = 0177; //8进制 17 System.out.println("i1 ="+Integer.toBinaryString(i3)); 18 19 char c = 0xffff;//最大的十六进制 char 20 System.out.println("c ="+Integer.toBinaryString(c)); 21 22 byte b = 0x7f; //最大的byte值 23 System.out.println("b ="+Integer.toBinaryString(b)); 24 25 short s = 0x7fff; //最大的short值 26 System.out.println("s: "+Integer.toBinaryString(s)); 27 28 long n1 = 200L; 29 long n2 = 200l; 30 long n3 = 200; 31 float f1 = 1F; 32 float f2 = 2F; 33 float f3 = 3; 34 35 } 36 }
3.指数记数
/*
补血到的知识: java中 e代表10的n次方
*/
public class Test { public static void main(String[] args) { //e表示10 的多少次放 float expFloat = 1.39e-43f; //attention float a = 3; //注意3f才能表示 a是float型,否则的话,仍然是double型 System.out.println(a);//不会报错 expFloat = 1.39E-43f; System.out.println(expFloat); double expDouble = 47e47d; // 我勒个去 double exDouble2 = 47e47; System.out.println(expDouble); } }
3.+=String的里面的知识
/*
补血到的知识
String s = "abc";
int a =3, b=4 ,c =5;
1.System.out.println(s+a+b+c) 输出为abc345
2.System.out.println(a+b+c+s) 输出为12abc
原因是优先级和String的+被重载
+号是从左到右的
1.输出的时候是因为s+a中的+被重载,a直接会被变成字符串常量 而后面也同理
*/
1 public class test{ 2 public static void main(String[] args) { 3 int x =0,y=1,z=2; 4 String s = "x,y,z"; 5 System.out.println(s+x+y+z); // x,y,z012 字符串开头,就直接把其他编程字符串 6 System.out.println(x+" "+s); // 7 s+="(summed) = "; 8 System.out.println(s+(x+y+z)); //运算后,再变成字符串,再来, 从左到右 9 System.out.println(""+x); 10 } 11 }
4.强制转换
1 public class test{ 2 public static void main(String[] args) { 3 int i=200; 4 long lng = (long) i; 5 long lng2 = (long)200; 6 lng2 = 200; 7 i = (int )lng2; 8 9 char c; 10 c = (char)lng2; 11 //int ->long 但是只要加上强转()就可以随意得强转 12 } 13 }
4.float和double的四舍五入
1 public class test{ 2 public static void main(String[] args) { 3 double above = 0.7,below = 0.4; 4 float fabove=0.7f,fbelow=0.4f; 5 System.out.println("(int above) "+(int)above); 6 System.out.println("(int below) "+(int)below); 7 System.out.println("(int falow) "+(int)fabove); 8 System.out.println("(int falow) "+(int)fbelow); 9 10 //如果想得到舍入的结果 11 System.out.println("Math.round(above) "+Math.round(above)); 12 System.out.println("Math.round(below) "+Math.round(below)); 13 System.out.println("Math.round(fabove) "+Math.round(fabove)); 14 System.out.println("Math.round(fbelow) "+Math.round(fbelow)); 15 //四舍侮辱用Math.round 16 17 18 } 19 }
5.Math.Random()的简单用法
public class Overflow{ public static void main(String[] args) { while ( condition()){ System.out.println("Inside While"); } } static boolean condition(){ boolean result = Math.random()<0.99; //MathRadom 产生包括0不包括1的double数 System.out.println(result+" "); return result; } }
6.测试重载数据
重载:
byte->short ->int ->long ->float->double
char ->
编译器在找重载数的时候,自动附合这些规律
如果找不到当前参数类型,自动向上一级找
例如 short找不到这个函数参数,就找int这个函数参数,又找不到就找long这个参数
1 package thinkingInJava; 2 3 public class test{ 4 void f1(char x){ System.out.print(" f1 " 5 + "char");} 6 void f1(double x){ System.out.print(" f1 " 7 + " double");} 8 void f1(short x){ System.out.print(" f1 " 9 + " short");} 10 void f1(int x){ System.out.print(" f1 " 11 + " int");} 12 void f1(long x){ System.out.print(" f1 " 13 + " long");} 14 void f1(float x){ System.out.print(" f1 " 15 + " float");} 16 void f1(byte x){ 17 System.out.print(" f1 "+" byte"); 18 } 19 20 21 22 void f2(double x){ System.out.print(" f2 " 23 + " double");} 24 void f2(short x){ System.out.print(" f2 " 25 + " short");} 26 void f2(int x){ System.out.print(" f2 " 27 + " int");} 28 void f2(long x){ System.out.print(" f2 " 29 + " long");} 30 void f2(float x){ System.out.print(" f2 " 31 + " float");} 32 void f2(byte x){ 33 System.out.print(" f2 "+" byte"); 34 } 35 36 37 void f3(double x){ System.out.print(" f3 " 38 + " double");} 39 void f3(short x){ System.out.print(" f3 " 40 + " short");} 41 void f3(int x){ System.out.print(" f3 " 42 + " int");} 43 void f3(long x){ System.out.print(" f3 " 44 + " long");} 45 void f3(float x){ System.out.print(" f3 " 46 + " float");} 47 48 49 50 void f4(double x){ System.out.print(" f4 " 51 + " double");} 52 53 void f4(int x){ System.out.print(" f4 " 54 + " int");} 55 void f4(long x){ System.out.print(" f4 " 56 + " long");} 57 void f4(float x){ System.out.print(" f4 " 58 + " float");} 59 60 61 62 void f5(double x){ System.out.print(" f5 " 63 + " double");} 64 65 void f5(float x){ System.out.print(" f5 " 66 + " float");} 67 void f5(long x){ System.out.print(" f5 " 68 + " long");} 69 70 71 void f6(double x){ System.out.print(" f6 " 72 + " double");} 73 74 void f6(float x){ System.out.print(" f6 " 75 + " float");} 76 77 void f7(double x){ System.out.print(" f7 " 78 + " double");} 79 80 void testConstVal(){ 81 System.out.println("5: "); 82 f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); 83 System.out.println(); 84 } 85 86 void testChar(){ 87 char x='x'; 88 System.out.println(" char:"); 89 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 90 System.out.println(); 91 } 92 93 void testByte(){ 94 byte x=0; 95 System.out.println(" byte"); 96 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 97 System.out.println(); 98 } 99 100 void testShort(){ 101 short x=0; 102 System.out.println(" Short"); 103 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 104 System.out.println(); 105 } 106 107 void testInt(){ 108 int x=0; 109 System.out.println(" Int"); 110 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 111 System.out.println(); 112 } 113 114 void testLong(){ 115 long x=0; 116 System.out.println(" Long"); 117 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 118 System.out.println(); 119 } 120 void testFloat(){ 121 float x=0; 122 System.out.println("float"); 123 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 124 System.out.println(); 125 } 126 127 void testDouble(){ 128 float x=0; 129 System.out.println("Double"); 130 f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x); 131 System.out.println(); 132 } 133 134 public static void main(String[] args) { 135 test a = new test(); 136 a.testConstVal(); 137 a.testChar(); 138 a.testByte(); 139 a.testShort(); 140 a.testInt(); 141 a.testLong(); 142 a.testFloat(); 143 a.testDouble(); 144 } 145 }