Java Questions

This category contains 10 posts

Why Is Printing “B” Dramatically Slower Than Printing “#”?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { … Continue reading »

Is Java “Pass-by-Reference” or “Pass-by-Value”?

I always thought Java was pass-by-reference; however I’ve seen a couple of blog posts (for example, this blog) that claim it’s not. I don’t think I understand the distinction they’re making. What is the explanation? user4315 Answer: Java is always pass-by-value. The difficult thing to understand is that Java passes objects as references and those … Continue reading »

Why Is Subtracting These Two Times (In 1927) Giving a Strange Result?

If I run the following program, which parses two date strings referencing times one second apart and compares them: public static void main(String[] args) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); String str3 = “1927-12-31 23:54:07”; String str4 = “1927-12-31 23:54:08”; Date sDt3 = sf.parse(str3); Date sDt4 = sf.parse(str4); long ld3 = sDt3.getTime() … Continue reading »

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 Strings to handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[]. Ahamed Answer: Strings … Continue reading »

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? dmanxiii Answer: 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 … Continue reading »

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 … Continue reading »

Avoiding “!= Null” Statements in Java?

I work with Java all day long. The most used idiom (code snippet) I’m programming in Java, is to test if an 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 avoid this code snippet? Update: … Continue reading »

Creating a Memory Leak With Java

I just had an interview, and I was asked to create a memory leak with Java. Needless to say I felt pretty dumb having no clue on how to even start creating one. What would an example be? Mat Banik Answer: Here’s a good way to create a true memory leak (objects inaccessible by running … Continue reading »

Why is processing a sorted array faster than an unsorted array?

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster. #include <algorithm> #include <ctime> #include <iostream> int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c < arraySize; ++c) data[c] … Continue reading »

Java += operator

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 … Continue reading »

Become a TFE Insider!