This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.

Daniel Chemko

Ranch Hand
+ Follow
since Feb 27, 2008
Merit badge: grant badges
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Daniel Chemko

"Really? Can you please Quote Your Sources? Because as far as I know, there is still only one Event Dispatcher Thread.
It is made easier to handle threading in Swing with SwingWorker, but all GUI related code still must be executed on the EDT."

Caught my ambiguity. I ran into several errors when dealing with my code, and when running through a debugger, EDT based errors were automatically suspended on the last stack entry " line: 156".

If several errors are queued up, the debugger will list all the EDT's that generated the error. The problem was that the EDT was being regenerated after each error giving the false impression that they were running independently. Thanks for the catch.
13 years ago

Both the string will have same hashcode and therefore if we try to add them in HashSet then only 's' can be added, as 's1' will be considered as duplicate on the basis of hashcode. Am I right?

Nope. The first step the HashSet will do is use the hashcode() result to find the correct bucket to find where to drop in the new string. If you don't know the concept of buckets, try Once in that bucket, the hashset will .equals() every object currently found within that bucket for equality. If and only if the .equals() of every object within that bucket returns false does the new object get inserted.
13 years ago
In my own codebase, I did some profiling and found that dealing with the reflections API, I've found timing as:
Java 1.4.2 - 10
Java 1.5.0 - 5
Java 1.6.0 - 4

So yes, the newer JVM's perform much better than previous releases doing the same thing. The GUI will get some interesting optimizations from 1.6 onward since they're no longer doing Swing/AWT work on a single thread. This can possibly run into races if you're doing GUI work on other threads, so watch out for that. Also, 1.6+ has greatly improved the performance of graphics in pretty much every conceivable way. The same code should run significantly faster.
13 years ago
Just look into java boot classes... its pretty hard to support any type of truly secure scheme of class infection. You may want to integrate with JNI and class hashes, jar signing and the such, but ultimately, its an up hill battle.
14 years ago
Nope. There isn't even a standard API for getting your own process PID, which is really annoying for writing PID files. There are solutions, but they all involve calling outside java land.
14 years ago
If thats what you need, then why worry about syntax?

I'd consider replacing the nested maps into concrete objects that may have maps within them, but only if the need requires. In itself, the data structure is very complicated, so that when you're using generics, you could get lost in the nesting.

Modeling complex structures are hard, but generics makes sure you don't make obvious mistakes in bad casting.
14 years ago
On linux you can always look at all open file handles by calling:

/usr/sbin/lsof | grep java
/usr/sbin/lsof | grep my_java_app's_pid

And you can see all the files loaded into virtual memory. Of course there's a chance that the memory isn't allocated into physical ram, just that it has been internally mapped to the process space.

Just one example of an entry is:
java 17120 dchemko mem REG 8,19 50827455 7286804 /usr/java/jdk1.6.0_10/jre/lib/rt.jar

The virtual size of my rt.jar is 50MB, so if the JRE loaded the entire core java classes into memory, you'd be sitting idle at 50MB. Anyways, it is also possible that the system's default minimum stack is set higher than you wanted it to.. or maybe the memory allocated is also reflecting memory mapped files, but I couldn't think of any MMIO files that would open by your setup.
14 years ago
The law here is you cannot cast an object from one type into another unless the object being referenced really is an implementation (obj instanceof desttype) the destination type. The only exception to this case is null, where it can be cast to any type without error.

looking back at your code, is (b instanceof A) ?? NO, hence the class cast exception. You may be getting confused as to which way you cast. The following does work fine:

Because (a instanceof B) is true.
14 years ago
Restructure your code so that fo.close() is called within the finally block that is trapping .write(). You do not want to leave the handle open. Attempting to open a file for writing while it already open for writing in a different handle may cause the open to fail.

Another possible issue has to do with thread synchronization. If you have multiple writers writing to the same file, similar blow-ups may occur. Once again though, as long as you close your stream handle on failure, that failure shouldn't be a permanent, just for that one iteration.

There's too much code omitted to be more specific as to your actual failure conditions.
14 years ago
1. If you must use a local file system as the JNLP host, use the file:// prefix so that the file URL can properly be decoded.

2. When I correct #1, I run into this issue:

MissingFieldException[ The following required field is missing from the launch file: <jnlp><information><vendor>]

So you must specify a vendor code.
14 years ago
HashCode is overridden with a capital hashCode() so he wasn't overriding the method. If you replace

public int hashcode()
public int hashCode()

it will only show unique records.
You are seeing the magic of compile time vs. runtime processing. Apparently varg lists are enumerated and 'boxed' at compile time instead of performing it at runtime.

So once the compiler gets a hold of the class, the classfile pseudocode would look something like this:

Both hashCode() and equals() are used When calling add() on a HashSet.

First, the set needs to determine what bucket in the hash the object goes into. This is done using something like hashCode() % numberOfBuckets.

Next, it has to check in that bucket that the object passed in doesn't already exist in the bucket, so it iterates one by one through the list items in the set calling equals() against the added record.

If an entry exists that receives equals(), the add() method will replace the existing set entry with the new one. If no entry exists, the set will just append the object to somewhere in the bucket.
== determines that the two object references point to the exact same stack/heap variable, aka their memory addresses.

The most primitive version of .equals() also does the exact same thing.

The difference is that equals can be overridden by any class and is quite often overridden. equals() is intended to ask if the objects are 'equivalent', not necessarily the same object in RAM.
14 years ago
Looking at the JRE source code explains it all.

.toString() method of Object was never designed as an API item for equivalence which is why Object includes the .equals() methods. All toString() is really useful for is giving developers / users interesting information about the internal state of the object. Do not attempt to discover any type of meaningful information from toString() unless you yourself designed the class for that purpose.