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.

Filter by
Sorted by
Tagged with
26284votes
27answers
1.7mviews

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 (before the timed region) miraculously makes the loop almost six times faster. #include &...
user avatar
  • 476k
7368votes
92answers
2.4mviews

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 pass-by-value. I don't think I understand the distinction they're making. What is the ...
7320votes
11answers
756kviews

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 them: public static void main(String[] args) throws ParseException { SimpleDateFormat sf ...
user avatar
  • 184k
4489votes
62answers
2.4mviews

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 ...
user avatar
4283votes
65answers
1.4mviews

Avoiding NullPointerException in Java

I use object != null a lot to avoid NullPointerException. What is an alternative to: if (someobject != null) { someobject.doCalc(); }
4124votes
35answers
1.7mviews

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?
user avatar
  • 49.4k
3937votes
39answers
1.7mviews

Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<...
user avatar
  • 51.9k
3891votes
71answers
4.7mviews

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: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum); Bug: randomNum can be ...
user avatar
  • 46.8k
3878votes
11answers
343kviews

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 avatar
  • 70.2k
3780votes
11answers
310kviews

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. ...
user avatar
  • 36.8k
3737votes
46answers
2.9mviews

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 ...
user avatar
  • 36.3k
3697votes
18answers
466kviews

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 ...
user avatar
  • 37.9k
3649votes
7answers
4.2mviews

Iterate through a HashMap [duplicate]

What's the best way to iterate over the items in a HashMap?
user avatar
  • 55.7k
3560votes
59answers
707kviews

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 clue on how to even start creating one. What would an example be?
3471votes
30answers
2.4mviews

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 ...
user avatar
  • 36.2k

15 30 50 per page
1
2 3 4 5
122416