Tag: java

56 votes
3 answers
1.8K views
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

avatar
Shantay
0 1 0 9
asked 07 September, 2021
59 votes
30 answers
1.1K views
How do I test a private function or a class that has private methods, fields or inner classes?

How do I unit test (using xUnit) a class that has internal private methods, fields or nested classes? Or a fun

avatar
Georgie
0 1 0 9
asked 07 September, 2021
63 votes
30 answers
2.2K views
Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList<String> plac

avatar
Dorian
0 1 0 9
asked 07 September, 2021
52 votes
26 answers
719 views
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

avatar
Chaya
0 1 0 9
asked 07 September, 2021
52 votes
30 answers
2.6K views
How do I convert a String to an int in Java?

How can I convert a String to an int in Java? My String contains only numbers, and I want to return the numbe

avatar
Louetta
0 1 0 9
asked 07 September, 2021
54 votes
30 answers
924 views
When to use LinkedList over ArrayList in Java?

I've always been one to simply use: List<String> names = new ArrayList<>(); I use the inte

avatar
Sena
0 1 0 9
asked 07 September, 2021
57 votes
29 answers
2K views
What is the difference between public, protected, package-private and private in Java?

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private),

avatar
Lynette
0 1 0 9
asked 07 September, 2021
53 votes
30 answers
2.2K views
How can I create a memory leak in Java?

I just had an interview where I was asked to create a memory leak with Java. Needless to say, I felt pretty du

avatar
Percy
0 1 0 9
asked 07 September, 2021
67 votes
30 answers
834 views
How do I efficiently iterate over each entry in a Java Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained wit

avatar
Daniel
0 1 0 9
asked 07 September, 2021
60 votes
18 answers
442 views
Why is char[] preferred over String for passwords?

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (retur

avatar
Chelsea
0 1 0 9
asked 07 September, 2021
64 votes
11 answers
981 views
Why don&apos;t Java&apos;s +=, -=, *=, /= compound assignment operators require casting?

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this:

avatar
Jeff
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
2.2K views
How do I generate random integers within a specific range in Java?

How do I generate a random int value in a specific range? I have tried the following, but those do not work: A

avatar
Patsy
0 1 0 9
asked 07 September, 2021
58 votes
11 answers
2.2K views
60 votes
30 answers
343 views
Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)};

avatar
Margo
0 1 0 9
asked 07 September, 2021
60 votes
30 answers
2.4K views
What are the differences between a HashMap and a Hashtable in Java?

What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded

avatar
Eddie
0 1 0 9
asked 07 September, 2021
50 votes
30 answers
2.6K views
Avoiding NullPointerException in Java

I use object != null a lot to avoid NullPointerException. What is an alternative to: if (someobject != null) {

avatar
Serina
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
2K views
How do I read / convert an InputStream into a String in Java?

If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I

avatar
Margo
0 1 0 9
asked 07 September, 2021
52 votes
30 answers
2.2K views
Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java uses pass-by-reference. However, I've seen a blog post that claims that Java uses p

avatar
Emilia
0 1 0 9
asked 07 September, 2021
53 votes
10 answers
1.5K views
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 1 second apart and compares th

avatar
Charis
0 1 0 9
asked 07 September, 2021
63 votes
27 answers
2.9K views
Why is processing a sorted array faster than processing an unsorted array?

Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data

avatar
Boyd
0 1 0 9
asked 07 September, 2021