File tree Expand file tree Collapse file tree
java-base/src/main/java/com/brianway/learning/java/base Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .base ;
2+
3+ /**
4+ * Created by brian on 16/11/10.
5+ *
6+ * TODO 补码/反码相关知识
7+ */
8+ public class Binary {
9+ public static void main (String [] args ) {
10+ int i = 5 ;
11+ int j = 10 ;
12+ System .out .println (i + ~j );
13+ }
14+ }
Original file line number Diff line number Diff line change 44 * Created by Brian on 2016/4/14.
55 */
66public class FatherClass {
7+ protected static int count = 10 ;
78 private String name ;
89
10+ static {
11+ System .out .println ("父类的静态属性count初始化:" + count );
12+ }
13+
914 public FatherClass () {
1015 System .out .println ("执行了父类的无参构造方法" );
1116 }
1217
1318 public FatherClass (String name ) {
1419 this .name = name ;
15- System .out .println ("执行了父类的构造方法FatherClass(String name)" );
20+ System .out .println ("执行了父类的构造方法FatherClass(String name) " + name );
1621 }
1722}
Original file line number Diff line number Diff line change 22
33/**
44 * Created by Brian on 2016/4/14.
5- */
6-
7- /**
5+ *
86 * 构造方法调用问题
97 * 子类构造方法会首先默认调用父类的无参构造方法,无论是否显式写了super();
108 */
119public class SonClass extends FatherClass {
10+
11+ private static int countSon ;
12+
13+ static {
14+ System .out .println ("子类可以访问父类的静态属性count " + count );
15+ System .out .println ("子类的静态属性countSon初始化:" + countSon );
16+ }
17+
1218 public SonClass (String name ) {
1319 //super(name);
14- System .out .println ("执行了子类的构造方法SonClass(String name)" );
20+ System .out .println ("执行了子类的构造方法SonClass(String name) " + name );
1521 }
1622
1723 public SonClass () {
@@ -26,8 +32,11 @@ public static void main(String[] args) {
2632}
2733
2834/*
35+ 父类的静态属性count初始化:10
36+ 子类可以访问父类的静态属性count 10
37+ 子类的静态属性countSon初始化:0
2938执行了父类的无参构造方法
30- 执行了子类的构造方法SonClass(String name)
39+ 执行了子类的构造方法SonClass(String name) aaa
3140执行了父类的无参构造方法
3241执行了子类的无参构造方法
3342 */
Original file line number Diff line number Diff line change 1- package com .brianway .learning .java .base ;
1+ package com .brianway .learning .java .base . datatype ;
22
33/**
44 * Created by Brian on 2016/4/14.
5- */
6-
7- /**
8- * TODO
9- * 待理解。
10- * 应该是考装箱和拆箱
5+ *
6+ * TODO 有些细节待理解
7+ *
8+ * 主要是考装箱和拆箱
119 */
1210public class Boxing {
1311 public static void main (String [] args ) {
@@ -24,16 +22,19 @@ public static void main(String[] args) {
2422 System .out .println (c .equals (a + b ));
2523 System .out .println (g == (a + b ));
2624 System .out .println (g .equals (a + b ));
25+ System .out .println (new Integer (2 ) == new Integer (2 ));
2726
2827 }
2928}
3029
3130/*
32- 输出:
33- true
34- false
35- true
31+ 输出: 原因:
32+ true 自动装箱,缓存
33+ false 自动装箱,未缓存
3634true
35+ true 调用 equals(),比较的是值,而不是对象地址
3736true
3837false
38+ false 比较的是对象地址
39+
3940 */
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .base .datatype ;
2+
3+ import java .lang .reflect .Field ;
4+
5+ /**
6+ * Created by brian on 16/11/1.
7+ *
8+ * 涉及到的知识点:
9+ * 1.java 的参数传递都是值传递
10+ * 2.Integer的内部实现(value,缓存,等等)
11+ * 3.反射操作(可访问性)
12+ * 4.自动装箱和拆箱
13+ *
14+ * 参考博客 http://www.voidcn.com/blog/zgwangbo/article/p-6101689.html
15+ */
16+ public class IntegerChanger {
17+
18+ public static void main (String [] args ) {
19+ Integer a = 1 , b = 2 ;
20+ System .out .println ("before swap a = " + a + ", b = " + b );
21+ swap (a , b );
22+ System .out .println ("after swap a = " + a + ", b = " + b );
23+
24+ Integer c = 1 ;
25+ System .out .println ("(警告:Integer缓存被改了,代码里:Integer c = 1;) 实际c=" + c );
26+ }
27+
28+ public static void swap (Integer i1 , Integer i2 ) {
29+ try {
30+ Field f = Integer .class .getDeclaredField ("value" );
31+ f .setAccessible (true );
32+
33+ int tmp = i1 ;
34+ f .setInt (i1 , i2 );
35+ f .setInt (i2 , tmp );
36+
37+ } catch (NoSuchFieldException e ) {
38+ e .printStackTrace ();
39+ } catch (IllegalAccessException e ) {
40+ e .printStackTrace ();
41+ }
42+
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .base .datatype ;
2+
3+ /**
4+ * Created by brian on 16/11/10.
5+ *
6+ * 包装类的“==”运算在不遇到算术运算的情况下不会自动拆箱;
7+ * 包装类的equals()方法不处理数据转型.
8+ */
9+ public class NumberEquation {
10+ public static void main (String [] args ) {
11+ Integer i = 42 ;
12+ Long l = 42l ;
13+ Double d = 42.0 ;
14+
15+ System .out .println (i .equals (d )); // false
16+ System .out .println (d .equals (l )); // false
17+ System .out .println (i .equals (l )); // false
18+ System .out .println (l .equals (42L )); // true
19+ }
20+ }
21+
22+ // (i == l),(i == d),(l == d)会出现编译错误
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .base .innerclass ;
2+
3+ /**
4+ * Created by brian on 16/11/10.
5+ *
6+ * 创建内部类的测试类
7+ */
8+ public class ClassCreator {
9+ public static void main (String [] args ) {
10+ //在其他类里创建非静态内部类和静态内部类
11+ EnclosingOne eo = new EnclosingOne ();
12+ EnclosingOne .InsideOne io = eo .new InsideOne ();
13+ EnclosingOne .InsideTwo it = new EnclosingOne .InsideTwo ();
14+ }
15+
16+ }
Original file line number Diff line number Diff line change 1+ package com .brianway .learning .java .base .innerclass ;
2+
3+ /**
4+ * Created by brian on 16/11/10.
5+ */
6+ public class EnclosingOne {
7+
8+ public class InsideOne {
9+
10+ }
11+
12+ static public class InsideTwo {
13+
14+ }
15+
16+ public static void main (String [] args ) {
17+ EnclosingOne eo = new EnclosingOne ();
18+ InsideOne io = eo .new InsideOne ();
19+ InsideTwo it = new InsideTwo ();
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments