Guangcheng Zhou

Greenhorn
+ Follow
since Oct 13, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Guangcheng Zhou

Hi all,

I was wondering if it is possible to issue commands from Java. In other words, is it possible to issue commands to the underlying operating system (for example, 'ls' for Unix) from a Java program?

Thanks,

Mack
17 years ago
Hi all,

I was wondering why the following code is printing true and false.

double d1 = 2.5;
double d2 = 2.6;
System.out.println(d1 == 2.5f);
System.out.println(d2 == 2.6f);

Shouldn't they be printing the same boolean value at least?


Thanks,


Mack
Hi All,

Just to clarify my question, would command java MyClass look for MyClass.class in the current directory before it looks for it in the classpath?


Thanks,


Mack
Hi all,

This is a really simple question. Will java command try to look for class file in the current directory before looking for it in the classpath (like javac would)?


Thanks,


Mack
I think hashCode() is used for HashSet though.
Yes, so is compareTo() method used in adding elements? I thought only hashCode() and equals() were used.
Hi all,

When a Comparator object is used in a TreeSet class, why is it that the add() method in TreeSet class does not add any elements if the compare() method in the Comparator class returns 0 for those two elements?

Thanks,


Mack
Thanks, yogesh. Your answer really helped!
Yes. But what I am asking is why sometimes when I pass an object to the get() method, the get() method returns null. For example,
Map<Object, Object> map = new HashMap<Object, Object>();
MyObject myObj = new MyObject(); // hashCode() and equals() has already being overriden. These two methods both use name attribute of MyObject.
// Initializing
myObj.setName("some_name");

// Add to HashMap object
map.put(myObj, "some_value");

// Now modify the value of the name attribute
map.name = "new_name";

// Since HashMap has a reference to myObj, then the object it refers to should be updated as well, according to Henry's post
// Then, hashCode() and equals() should work according to the updated object
map.get(myObj); // Should not return null

However, the last line return null. Why would that happen?


Thanks,

Mack
Hi all,

I was wondering when I pass a reference object as a key to the put() method from Map interface, such as
Map<Object, Object> m = new HashMap<Object, Object>();
MyObject o = new MyObject();
m.put(o, "Some info");

does the put() method make a new copy of MyObject o in the HashMap, or will the key from HashMap refer to the same object on the heap memory?

I also wrote some tests to try to uncover the answer to this question. I found that whenever I make a change to MyObject o (through o.update(), for example), the object in the HashMap m changes as well.
So I am thinking this is because they are referring to the same object.

But if that is true, then why is it that
o.setName("New name"); // Change to a new name, this is the
// name used in the overriden equals()
// Right now, assuming key reference from HashMap m and o are
// referring to the same object, then the following line should not
// return null
m.get(o);
And m.get(o) does return null.

Any thoughts or answers?


Thanks,


Mack
Hi all,

I wondering why it is that statements such as
Byte b = 25;
Short s = 25;
Integer i = 25;
will compile without any problem but line Long l = 25; will cause a "Incompatible types" error?

Thanks.

Mack