1.字符串不变性
1.Declare a string
声明一个变量
String s = "abcd";
s stores the reference of the string object. The arrow below should be interpreted as “store reference of”.
s存储了一个字符串对象的引用,下面的箭头解释为:存储的引用是
图解:
2.Assign one string variable to another string variable
将一个字符串变量分配给另一个字符串变量
String s2 = s;
图解:
3.Concat string
字符串连接
s = s.concat("ef");
s now stores the reference of the newly created string object.
s现在存储新创建的字符串对象的引用。
总结
Once a string is created in memory(heap), it can not be changed. We should note that all methods of String do not change the string itself, but rather return a new String.
一旦在内存中(堆)创建了一个字符串,它不能被改变。我们应该注意,所有的字符串的方法都不改变字符串本身,而是返回一个新的字符串。
If we need a string that can be modified, we will need StringBuffer or StringBuilder. Otherwise, there would be a lot of time wasted for Garbage Collection, since each time a new String is created. Here is an example of StringBuilder usage.
如果我们需要一个可以修改的字符串,我们就需要StringBuffer或StringBuilder。否则,每次创建一个新的字符串时,将有大量的时间浪费在垃圾收集上。这是我们使用StringBuilder的一个例子。
2.equals()方法、hashCode()方法的区别
HashCode被设计用来提高性能。equals()方法与hashCode()方法的区别在于:
1.如果两个对象相等(equal),那么他们一定有相同的哈希值。
2. 如果两个对象的哈希值相同,但他们未必相等(equal)。
图:
Java超类 java . lang . object 定义了两个重要的方法:
public boolean equals(Object obj)
public int hashCode()
详细了解例子请看
http://www.programcreek.com/2011/07/java-equals-and-hashcode-contract/
3.Java异常类的层次结构
图中红色部分为受检查异常。它们必须被捕获,或者在函数中声明为抛出该异常。
4.集合类的层次结构
First of all, “Collection” and “Collections” are two different concepts. As you will see from the hierarchy diagram below, “Collection” is a root interface in the Collection hierarchy but “Collections” is a class which provide static methods to manipulate on some Collection types.
首先,“Collection”和“Collections”是两个不同的概念。正如您将从下面的层次结构图中看到的,“Collection”是集合层次结构中的一个根接口,但“Collections”是一个类,它提供了一些集合类型的静态方法来操作。
例子请看
http://www.programcreek.com/2009/02/the-interface-and-class-hierarchy-for-collections/
5.Java同步
A monitor can be considered as a building which contains a special room. The special room can be occupied by only one customer(thread) at a time. The room usually contains some data and code.
监视器可以被视为包含一个特殊的房间的一种建筑。 特殊房间只能被一个客户(线程)。 房间通常包含一些数据和代码。
If a customer wants to occupy the special room, he has to enter the Hallway(Entry Set) to wait first. Scheduler will pick one based on some criteria(e.g. FIFO). If he is suspended for some reason, he will be sent to the wait room, and be scheduled to reenter the special room later. As it is shown in the diagram above, there are 3 rooms in this building.
如果一个客户想占据这个特殊的房间,他必须进入走廊(进入组)等待第一。调度器会基于一些标准(如先进先出)。如果他是出于一些原因被暂停,他将被送到等候室,并在后面一段时间再次被安排进入房间。如上图所示,这座大楼里有3个房间。
详细请看
http://www.programcreek.com/2011/12/monitors-java-synchronization-mechanism/
6.Java处理混淆
Aliasing means there are multiple aliases to a location that can be updated, and these aliases have different types.
混淆意味着存在多个别名指向一个可被更新的位置,而这些别名有不同的类型。
以下代码有混淆现象(不能正常运行)
class A {
public void methodParent() {
System.out.println("method in Parent");
}
}
class B extends A {
public void methodParent() {
System.out.println("override method in Child");
}
public void methodChild() {
System.out.println("method in Child");
}
}
public class Main {
public static void main(String[] args) {
B[] b = new B[10];
A[] a = b;
a[0] = new A();
b[0].methodParent();
}
}
The reason is that Java handles aliasing during run-time. During run-time, it knows that the first element should be a B object, instead of A.
运行结果:
Exception in thread "main" java.lang.ArrayStoreException: aliasingtest.A
at aliasingtest.Main.main(Main.java:26)
1.从对象的内存角度来理解.
假设现在有一个父类Father,它里面的变量需要占用1M内存.有一个它的子类Son,里面的变量需要占用0.5M内存.
现在通过代码来看看内存的分配情况:
2.f = new Father();//系统将分配1M内存.
Son s = new Son();//系统将分配1.5M内存!因为子类中有一个隐藏的引用super会指向父类实例,所以在实例化子类之前会先实例化一个父类,也就是说会先执行父类的构造函数.由于s中包含了父类的实例,所以s可以调用父类的方法.
3.Son s1 = s;//s1指向那1.5M的内存.
Father f1 = (Father)s;//这时f1会指向那1.5M内存中的1M内存,即是说,f1只是指向了s中实例的父类实例对象,所以f1只能调用父类的方法(存储在1M内存中),而不能调用子类的方法(存储在0.5M内存中).
Son s2 = (Son)f;//这句代码运行时会报ClassCastException.因为f中只有1M内存,而子类的引用都必须要有1.5M的内存,所以无法转换.
Son s3 = (Son)f1;//这句可以通过运行,这时s3指向那1.5M的内存.由于f1是由s转换过来的,所以它是有1.5M的内存的,只是它指向的只有1M内存.
理解:
B类继承了A类,B类包含了自己的特性和A类的共性,把A类对象指向B,指向的是B类中的共性,a[0]=new B()实际上是实例出一个A类实例(B中包含了A中所有东西),又由于 a[0]与b[0]指向同一个地方(一样的)但是a[0]只包含了A类的东西,没有包含B类的东西,所以会报错
图片
解决:
B[] b = new B[10];
A[] a = b;
a[0] = new B();
b[0].methodParent();
输出:override method in Child
7.堆和栈
图解表明了方法和对象在运行时内存中的位置。
堆栈两者区别:
8.Java虚拟机运行时数据区域
图
- Data Areas for Each Individual Thread (not shared)
Data Areas for each individual thread include program counter register, JVM Stack, and Native Method Stack. They are all created when a new thread is created.
Program Counter Register - used to control each execution of each thread.
JVM Stack - contains frames which is demonstrated in the diagram below.
Native Method Stack - used to support native methods, i.e., non-Java language methods.
- Data Areas Shared by All Threads
All threads share Heap and Method Area.
Heap: it is the area that we most frequently deal with. It stores arrays and objects, created when JVM starts up. Garbage Collection works in this area.
Method Area: it stores run-time constant pool, field and method data, and methods and constructors code.
Runtime Constant Pool: It is a per-class or per-interface run-time representation of the constant_pool table in a class file. It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.
1。每个单独线程的数据区域(不共享)
每个线程的数据区域包括程序计数器登记,JVM栈,本地方法栈。当创建一个新线程时,它们都被创建。
程序计数器寄存器-用于控制每个线程的每个执行。
JVM栈帧是包含在下面的图显示。
本地方法栈用于支持native方法,即非java语言的方法。
2。所有线程共享的数据区域
所有线程共享堆和方法区。
堆:是我们最经常处理的区域。它存储数组和对象,JVM启动时创建的。在这方面的垃圾收集工作。
方法区域:存储运行时常量池、字段和方法数据,以及方法和构造函数代码。
运行时常量池:这是一个每个类或每个接口的运行时表示constant_pool表的类文件。 它包含多种常量,从数字文字在编译时知道方法和字段引用,必须在运行时解决。
Stack contains Frames, and a frame is pushed to the stack when a method is invoked. A frame contains local variable array, Operand Stack, Reference to Constant Pool.
堆栈包含帧,当一个方法被调用时,一个帧被推到堆栈。帧包含局部变量数组,操作数栈,常量池参考。
详细请看:http://www.programcreek.com/2013/04/jvm-run-time-data-areas/
本文原文:http://www.programcreek.com/2013/09/top-8-diagrams-for-understanding-java/
翻译:http://www.importnew.com/11725.html