You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
General <strong>Java</strong> Questions II<br />
the array[].length but it does not work, how can I solve this problem?<br />
Answer: <strong>Java</strong> doesn't really have "multidimensional arrays", only arrays of arrays. So<br />
try: array[0].length and you will get this dimension.<br />
I guess what I'm asking is "Is java.util.Hashtable thread safe?"<br />
Q: It's been a while since I've used hashtables for anything significant, but I seem to<br />
recall the get() and put() methods being synchronized.<br />
The <strong>Java</strong>Docs don't reflect this. They simply say that the class Hashtable is<br />
synchronized. What can I assume? If several threads access the hashtable at the<br />
same time (assuming they are not modifying the same entry), the operations will<br />
succeed, right? I guess what I'm asking is "Is java.util.Hashtable thread safe?"<br />
Answer: That is right! It is recommendable, if you have questions like these, always<br />
look at source for the API, it's freely available.<br />
Q: I try to copy an object of my own using the clone() method from<br />
java.lang.Object, but this is a protected method so I can't use it. Is there some other<br />
way to get my objective of duplicating an arbitrary object?<br />
Answer: If you want to clone your object, you need to make it cloneable. To achieve<br />
this, you need to do two things:<br />
1. implement the interface Cloneable<br />
2. override the method clone(), so that it<br />
a. becomes public<br />
b. calls super.clone()<br />
c. if necessary, clones any members, or<br />
d. if a member can't be cloned, creates a new instance.<br />
Simple example:<br />
public MyClass implements Cloneable {<br />
int someNumber;<br />
String someString;<br />
public Object clone() {<br />
// primitives and Strings are no<br />
// problem<br />
return super.clone();<br />
}<br />
}<br />
In this case the method clone() of the class MyClass returns a new instance of<br />
MyClass, where all members have exactly the same value. That means, the object<br />
reference 'someString' points to the same object. This is called a shallow<br />
copy. In many cases this is no problem. Strings are immutable and you do not<br />
need a new copy. But if you need new copies of members, you have to do it in<br />
the clone() method. Here is another simple example:<br />
public class SomeMember implements Cloneable {<br />
long someLong;<br />
file:///F|/350_t/350_tips/general_java-II.htm (10 of 13) [2002-02-27 21:18:24]