Other than an anonymous class (new OutputStream() { … }), can anyone suggest a moral equivalent of new FileOutputStream(“/dev/null”) that also works on Windows? In case someone’s wondering ‘what’s this for?’ I have a program that does a consistency analysis on a file.…
Invert the Selection in JTable
On clicking a button, I want the selected rows to be inverted (non-selected rows should be selected and selected rows should be non-selected). Is there a build-in method in JTable to do it? Answer JTable doesn’t seems to have a built-in way of doing this. So I implemented it with the following code. (Ho…
What is the difference between local and instance variables in Java?
Except the scope and the storage differences, are there any other major difference between instance and local variables in Java? Answer One extra thing I can think of: Instance variables are given default values, i.e., null if it’s an object reference, and 0 if it’s an int. Local variables donR…
Importing two classes with same name. How to handle?
Say I’ve a code like: Should I be full qualified class names? Can I get rid of the import statements? Is such a scenario common in real world programming? Answer You can omit the import statements and refer to them using the entire path. Eg: But I would say that using two classes with the same name and …
Adding Buttons inside cell of JTable along with data?
Is it possible to add buttons inside the JTable cell along with data? What I am trying to do is to create a table with columns which display data(number) from the database, and two buttons to increase/decrease the number inside the same cell. |ID | Quantity| |06| 2 [+][-] | it would be something like above wi…
How can I tell if I’m running in 64-bit JVM or 32-bit JVM (from within a program)?
How can I tell if the JVM in which my application runs is 32 bit or 64-bit? Specifically, what functions or properties I can used to detect this within the program? Answer You retrieve the system property that marks the bitness of this JVM with: Possible results are: “32” – 32-bit JVM “64…
What is chain.doFilter doing in Filter.doFilter method?
In a Filter.doFilter method I made this call chain.doFilter. What is doFilter doing inside a doFilter? Isn’t it a recursive call? Answer Servlet Filters are an implementation of the Chain of responsibility design pattern. All filters are chained (in the order of their definition in web.xml). The chain.d…
Format negative amount of USD with a minus sign, not brackets (Java)
How do I get NumberFormat.getCurrencyInstance() to print negative USD currency values with a minus sign? Answer Since I faced this problem again, I did some research and found a more resilient solution provided by the ICU: Check the API documentation of NumberFormatter for more details.
What is the replacement for Sets.newConcurrentHashSet()?
I am upgrading from Google Collections 0.9 to 1.0. It seems Sets.newConcurrentHashSet() is no longer available. I was using it in the following construct: What would be the best replacement for Sets.newConcurrentHashSet() ? Edit: The reason for this construct is to have a multimap that is safe to read and wri…
How to quote/escape identifiers such as column names with JDBC?
Different database servers use different ways to quote and escape identifiers. E.g. “foo bar” vs `foo bar` vs [foo bar], or “10””” vs “10””, or identifiers such as FooBar or array need to be quoted for some databases but not for others. Is there any API me…