File tree Expand file tree Collapse file tree 8 files changed +149
-17
lines changed
java-base/src/main/java/com/brianway/learning/java/base Expand file tree Collapse file tree 8 files changed +149
-17
lines changed 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 4
4
* Created by Brian on 2016/4/14.
5
5
*/
6
6
public class FatherClass {
7
+ protected static int count = 10 ;
7
8
private String name ;
8
9
10
+ static {
11
+ System .out .println ("父类的静态属性count初始化:" + count );
12
+ }
13
+
9
14
public FatherClass () {
10
15
System .out .println ("执行了父类的无参构造方法" );
11
16
}
12
17
13
18
public FatherClass (String name ) {
14
19
this .name = name ;
15
- System .out .println ("执行了父类的构造方法FatherClass(String name)" );
20
+ System .out .println ("执行了父类的构造方法FatherClass(String name) " + name );
16
21
}
17
22
}
Original file line number Diff line number Diff line change 2
2
3
3
/**
4
4
* Created by Brian on 2016/4/14.
5
- */
6
-
7
- /**
5
+ *
8
6
* 构造方法调用问题
9
7
* 子类构造方法会首先默认调用父类的无参构造方法,无论是否显式写了super();
10
8
*/
11
9
public 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
+
12
18
public SonClass (String name ) {
13
19
//super(name);
14
- System .out .println ("执行了子类的构造方法SonClass(String name)" );
20
+ System .out .println ("执行了子类的构造方法SonClass(String name) " + name );
15
21
}
16
22
17
23
public SonClass () {
@@ -26,8 +32,11 @@ public static void main(String[] args) {
26
32
}
27
33
28
34
/*
35
+ 父类的静态属性count初始化:10
36
+ 子类可以访问父类的静态属性count 10
37
+ 子类的静态属性countSon初始化:0
29
38
执行了父类的无参构造方法
30
- 执行了子类的构造方法SonClass(String name)
39
+ 执行了子类的构造方法SonClass(String name) aaa
31
40
执行了父类的无参构造方法
32
41
执行了子类的无参构造方法
33
42
*/
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 ;
2
2
3
3
/**
4
4
* Created by Brian on 2016/4/14.
5
- */
6
-
7
- /**
8
- * TODO
9
- * 待理解。
10
- * 应该是考装箱和拆箱
5
+ *
6
+ * TODO 有些细节待理解
7
+ *
8
+ * 主要是考装箱和拆箱
11
9
*/
12
10
public class Boxing {
13
11
public static void main (String [] args ) {
@@ -24,16 +22,19 @@ public static void main(String[] args) {
24
22
System .out .println (c .equals (a + b ));
25
23
System .out .println (g == (a + b ));
26
24
System .out .println (g .equals (a + b ));
25
+ System .out .println (new Integer (2 ) == new Integer (2 ));
27
26
28
27
}
29
28
}
30
29
31
30
/*
32
- 输出:
33
- true
34
- false
35
- true
31
+ 输出: 原因:
32
+ true 自动装箱,缓存
33
+ false 自动装箱,未缓存
36
34
true
35
+ true 调用 equals(),比较的是值,而不是对象地址
37
36
true
38
37
false
38
+ false 比较的是对象地址
39
+
39
40
*/
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