Why is char[] preferred over String for passwords? In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String) method. Similarly, I have come across a suggestion not to use Stringto handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]. Solution: Strings are immutable. That means once you've created the String, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before garbage collection kicks in. With an array,
undefined
Differences between HashMap and Hashtable? What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded applications? Solution: There are several differences between HashMap and Hashtable in Java: Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you could easily swap
undefined
Read/convert an InputStream to a String If you have java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert this to a String (for example, so I can write the contents of the stream to a log file). What is the easiest way to take the InputStream and convert it to a String? public String convertStreamToString(InputStream is) { // ??? } Answer: A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter... something like StringWriter
undefined
Proper use cases for Android UserManager.isUserAGoat()? I was looking at the new APIs introduced in Android 4.2. While looking at the UserManager class I came across the following method: public boolean isUserAGoat() Used to determine whether the user making this call is subject to teleportations. Returns whether the user making this call is a goat. How and when should this be used? Answer: From their source, the method used to return false until it was changed in API 21. /** * Used to determine whether
undefined
Java's +=, -=, *=, /= compound assignment operators Until today I thought that for example: i += j; is just a shortcut for: i = i + j; But what if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j)? Answer: As always with these questions, the JLS
undefined
Avoiding != null statements The idiom I use the most when programming in Java is to test if object != null before I use it. This is to avoid a NullPointerException. I find the code very ugly, and it becomes unreadable. Is there a good alternative to this? I want to address the necessity to test every object if you want to access a field or method of this object. For example: if (someobject != null) { someobject.doCalc(); } In this case I will avoid a NullPointerException,