摘要:
Explanation: Objects A, B, C are allocated into memory. Then B is unused. The space of B is released. Then D is coming, and is looking for a memory to store. B’s space is released, but is too small to D, so D find another place below C. Later, because of the small space of B left, no variables can u 阅读全文
摘要:
Java 只支持单继承,不支持多继承 单继承就是一个类只能有一个父类;多继承就是一个类可以有多个父类。 子类可以继承父类所有的成员变量和成员方法,但子类永远无法继承父类的构造器(Constructor)。在子类构造方法中可以使用语句 Super(参数列表)调用父类的构造方法。 阅读全文
摘要:
阅读全文
摘要:
阅读全文
摘要:
when to select “extends”? Is “B” an “A”? If yes, let “B” be the subclass of “A”. B是一个A吗? 如果是,则让B做A的子类。Common mistake: A有一个B吗? 比如:让汽车轮子成为汽车的子类是错误的。因为“汽车轮子”不是一个“汽车”。 阅读全文
摘要:
The private properties/methods of the Superclass cannot be accessed by subclass. The non-private properties/methods of the Superclass can be access by subclass’s objects directly.总结: 1. “private” can’t be “extends” 2. “private” 物理上已经被继承过来了,只不过逻辑上不能访问他。所以继承必须慎重,否则浪费内存空间。 阅读全文
摘要:
“Extends” simulates the relationship between the things in real world. 父类: Superclass 子类: Subclass A Java superclass is a class which gives a method or methods to a Java subclass. A Java class may be either a subclass, a superclass, both, or neither!———-TestExtends.java———- package ch.allenstudy.new 阅读全文
摘要:
How to make sure a class only can generate one object? First, let’s think about this: ———-D.java————- package ch.allenstudy.newway02; public class D { } class A { } class C { public int i = 10; //i 是 C 的属性 } ————————- 这个没有问题吧, i 是 C 的属性。 Let’s add one more line: ———-D.java————- package ch.allenstudy 阅读全文
摘要:
For weeks I don’t touch guitar. By accident I know that Hans is the professional of guitar! Amazing! He took the guitar to me for some practice here. The guitar is very good voice. I feel it’s 单板, maybe 红木.Thanks very much, Hans! 阅读全文
摘要:
How to know how many objects has been generated for a class? We need use the “static” knowledge. Every time you new an object, you will call the constructor(s) of the class. And the “static” property can be updated every time. So, see below sample: —————————— package ch.allenstudy.newway01; public c 阅读全文
摘要:
“this” is only for dynamic method. We don’t need it for static properties/methods, because static properties/methods are already shared in the memory. The reason we use “this” is to distinguish the values for different objects. 阅读全文
摘要:
只有非 private (public, protected, or no identifier) 的 static 成员 (成员指属性和方法)才能通过类名的方式访问。 static 只是表明该成员具有了可以通过类名访问的潜在特征,但是否可以通过类名访问 (class.xxx),还必须具备一个条件:该成员必须是非private Sample: ==================== package ch.allenstudy.newway01;public class Recursion1 { public static void main(String[] args) { System.o 阅读全文
摘要:
Sample first:———————————- package ch.allenstudy.newway01; public class TestStatic { public static void main(String[] args) { A aa1 = new A(); A aa2 = new A();aa1.i = 20; //Here, object “aa1″ sets its property i’s value to “20″. aa2.show(); //Here, object “aa2″ call the show() method which calls its 阅读全文
摘要:
静态方法可以直接用类名点出来方法,而普通方法需要创建类的对象后才能调用! 静态的方法和变量会调用时在内存生成一个唯一的标示,你可以理解成在物理内存中给静态一个位子,这样的话在调用的时候可以直接找到,而且会节省内存,但是如果你声明的静态过多的话那么每一个都会在内存有一个位子,那么你就没有资源运行别的,会报内存溢出! 普通方法是由java的gc机制来控制,可能同一个对象或变量在使用的过程中,这个时间的在内存占了一个位子,而上个时间的还没有从内存中删除,这样的话就可能有2个一样的在内存中,这个2个一样东西只是内容值一样,但是内存值不一样,你可以用”==”和”equals”来查看(这个只适用对象和St 阅读全文
摘要:
As usual, a sample first. ========================== package ch.allenstudy.newway01;public class ThisPointer {public static void main(String[] args) { A aa1 = new A(10); A aa2 = new A(20); aa1.show(); aa2.show(); }}class A { public int i;public A(int a) { i = a; }public void show() { System.out.prin 阅读全文
摘要:
Ctrl+Shift+F to format code in Eclipse 阅读全文
摘要:
Let’s write a java file first. File name is Gouzaohanshu.java notice: the main() must be in the class name which is same as the file name, i.e. Gouzaohanshu ============================ package ch.allenstudy.newway01;class Gouzao{ private int i,j; public void set(int a, int b) { i = a; j = b; } publ 阅读全文
摘要:
This post is to makde a NullPointerException and explain a little bit. See the capture first.U can see that the value of string[1] is NULL, then the code wants to use charAt() method to call it. Finally there’s a NullPointerException error. This error is always because we want to operate an object w 阅读全文
摘要:
This post shows how to create a StackOverflowError with a recursion. Let’s look at the error message first: Exception in thread “main” java.lang.StackOverflowError at ch.allenstudy.newway01.Recursion1.f(Recursion1.java:9) at ch.allenstudy.newway01.Recursion1.g(Recursion1.java:14) at ch.allenstudy.ne 阅读全文
摘要:
Recursion: 递归 P(n)= P(n-1)* n 阅读全文
摘要:
Private methods/properties (priviate int a;) can’t be accessed from outside of the class. Protected (protected int a;)can be accessed in the class or from its sub-class. Public (public int a;)can be accessed from any places. The methods/properties without any identifier (int a) can be access by the 阅读全文
摘要:
Here attached an article about heap (堆) and stack (栈). HeapAndStack Java will store all things into system memory (system memory is physical mem + virtual mem), then it’s the OS to decide they go to physical memory or hard disk (virtual memory) later.堆是先进先出,而栈是先进后出. But in Java, not that obvious dif 阅读全文
摘要:
阅读全文
摘要:
Use -7 as sample.int a = -7; //declaration, notice that int is 4 bytes, so 32 bits. The bit representation will be in 2’s complement form.Steps to find 2’s Complement. Take binary representation of positive value (in this case, it is 7) 0000 0000 0000 0000 0000 0000 0000 0111Take 1’s Complement of i 阅读全文
摘要:
Question desc: I am using Eclipse for java file. I write below code, and then RUN, but the result is garbage characters. How to fix it? —————————— package ch.allenstudy.newway01; public class TestPrintf { public static void main(String[] args) { // 定义一些变量,用来格式化输出。 double d = 345.678; String s = “Hel 阅读全文