How Java handles arguments of a class type.
=====suppose such a method:
public static void openFile(String fileName, PrintWriter stream) throws FileNotFoundException
{
stream = new PrintWriter(fileName);
}
=====then we want to use it this way:
PrintWriter toFile = null;
try
{
openFile("data.txt", toFile);
}
*****After this code is executed, the value of toFile is still null. The file that was opened in the method openFile went away when the method ended. The problem has to do with how Java handles arguments of a class type.
"These arguments are passed to the method as memory addresses that cannot be changed. The state of the object at the memory address normally can be changed, but the memory address itself cannot be changed"!
相关文档:
http://www.onjava.com
O'Reilly的Java网站. 每周都有新文章
http://java.sun.com
官方的Java开发者网站 - 每周都有新文章发表
http://www.developer.com/java
由Gamelan.com 维护的Java技术文章网站
http://www.java.net
&nb ......
多线程的同步依靠的是对象锁机制,synchronized关键字的背后就是利用了封锁来实现对共享资源的互斥访问。
下面以一个简单的实例来进行对比分析。实例要完成的工作非常简单,就是创建10个线程,每个线程都打印从0到99这100个数字,我们希望线程之间不会出现交叉乱序打印,而是顺序地打印。
先来看第一段代码,这里我们在ru ......
首先java接口和抽象类代表的就是抽象类型,就是我们需要提出抽象层的具体实现,如果要提高程序的复用率,可维护性,可扩展性,就必须面向接口和抽象编程,正确使用他们。 (1)抽象类可以提供实现方法,接口不能
这是抽象类的唯一优点,而且非常有用,例如,你定义一个接口,子类不需要他的所有方法,可是你没办法不去 ......
众所周知,在Java多线程编程中,一个非常重要的方面就是线程的同步问题。
关于线程的同步,一般有以下解决方法:
1.
在需要同步的方法的方法签名中加入synchronized关键字。
2.
使用synchronized块对需要进行同步的代码段进行同步。
3. 使用JDK
5中提供的java.util.concurrent.lock包中的Lock对象。 ......