All In One Script
MENU

What is a serialVersionUID and why should I use it?

by 4:34:00 AM
undefined
What is a serialVersionUID and why should I use it? Eclipse issues warnings when a serialVersionUID is missing.       The serializable class Foo does not declare a static final serialVersionUID field of type long What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem. Answer: The docs for java.io.Serializable are probably about as good an explanation as you'll get: The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify

Creating a memory leak with Java

by 5:47:00 AM
undefined
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? Solution: Here's a good way to create a true memory leak (objects inaccessible by running code but still stored in memory) in pure Java: The application creates a long-running thread (or use a thread pool to leak even faster).

Why is printing “B” dramatically slower than printing “#”?

by 5:44:00 AM
undefined
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++) { if(r.nextInt(4) == 0) { System.out.print("O"); } else { System.out.print("#"); } } System.out.println(""); } With this code, the second matrix took 259.152 seconds to complete: Random r = new Random();

When to use LinkedList over ArrayList?

by 5:42:00 AM
undefined
When to use LinkedList over ArrayList? I've always been one to simply use: List<String> names = new ArrayList<String>(); I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code. When should LinkedList be used over ArrayList and vice-versa? Answer: TL;DR ArrayList with ArrayDeque are preferable in much more use-cases than LinkedList. Not sure — just start with ArrayList. LinkedList and ArrayList are two different implementations of the List interface. LinkedListimplements it with a doubly-linked list. ArrayList implements it with a dynamically re-sizing array. As with standard linked list and array

How to generate random integers within a specific range in Java?

by 5:40:00 AM
undefined
How to generate random integers within a specific range in Java? I am trying to generate a random intvalue with Java, but in a specific range. For example: My range is 5-10, meaning that 5 is the smallest possible value and 10 is the biggest. Any other number in between these numbers is possible to be a value, too. In Java, there is a method random() in the Math class, which returns a double value between 0.0 and 1.0. In the class Random there is the method nextInt(int n), which returns a random intvalue in the range of 0 (inclusive) and n (exclusive). I couldn't find

Instagram