Questions tagged [java]
Java is a high-level object-oriented programming language. Use this tag when you're having problems using or understanding the language itself. This tag is frequently used alongside other tags for libraries and/or frameworks used by Java developers.
1,920,556
questions
27293
votes
25
answers
1.9m
views
Why is processing a sorted array faster than processing an unsorted array?
In this C++ code, sorting the data (before the timed region) makes the primary loop ~6x faster:
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// ...
7750
votes
91
answers
2.7m
views
Is Java "pass-by-reference" or "pass-by-value"?
I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making.
What is the ...
7570
votes
11
answers
806k
views
Why is subtracting these two epoch-milli Times (in year 1927) giving a strange result?
If I run the following program, which parses two date strings referencing times 1 second apart and compares them:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf ...
4742
votes
66
answers
2.7m
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 have an InputStream that contains text data, and I want to convert it to a String, so for ...
4422
votes
71
answers
1.4m
views
How do I avoid checking for nulls in Java?
I use x != null to avoid NullPointerException. Is there an alternative?
if (x != null) {
// ...
}
4321
votes
35
answers
1.8m
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 applications?
4108
votes
42
answers
1.9m
views
Create ArrayList from array
Element[] array = {new Element(1), new Element(2), new Element(3)};
How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>?
ArrayList<Element> ...
4107
votes
59
answers
5.2m
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?
The following methods have bugs related to integer overflow:
randomNum = minimum + (int)(Math.random() * maximum);
// Bug: `randomNum` can be ...
4031
votes
12
answers
369k
views
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 ...
3989
votes
46
answers
3.4m
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 within it, what is the most efficient way of going through the map?
Will the ordering of ...
3857
votes
11
answers
318k
views
Why don't Java'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:
int i = 5;
long j = 8;
Then i = i + j; will not compile but i += j; will compile fine.
...
3851
votes
17
answers
520k
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() (returns String) method. Similarly, I have come across a suggestion not to use String to handle ...
3773
votes
7
answers
4.5m
views
Iterate through a HashMap [duplicate]
What's the best way to iterate over the items in a HashMap?
3736
votes
61
answers
754k
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 dumb, having no idea how to start creating one.
What would an example be?
3697
votes
32
answers
2.6m
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), public, protected and private, while making class and interface and dealing with ...