Join us in building a kind, collaborative learning community via our updatedCode of Conduct.

Questions tagged [java]

Java (not to be confused with JavaScript or JScript or JS) is a general-purpose object-oriented programming language designed to be used in conjunction with the Java Virtual Machine (JVM). "Java platform" is the name for a computing system that has installed tools for developing and running Java ...

21697
votes
21answers
1.2m views

Why is it faster to process a sorted array 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>#...
6041
votes
8answers
555k 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 them:public static void main(String[] args) throws ParseException {SimpleDateFormat ...
5510
votes
78answers
1.6m views

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 that it isn't.I don't think I understand the distinction they're making. ...
3558
votes
59answers
1.1m views

Avoiding !=null statements

I use object !=null a lot to avoid NullPointerException.Is there a good alternative to this? For example:if (someobject !=null) {someobject.doCalc();}This avoids a NullPointerException,...
3294
votes
10answers
249k views

Why don't Java's +=, -=, *=, /=compound assignment operators require casting?

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....
3274
votes
58answers
1.7m views

Read/convert an InputStream to a String

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 ...
3151
votes
11answers
273k 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 user ...
3126
votes
35answers
1.3m views

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?
2975
votes
32answers
1.3m views

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<...
2909
votes
17answers
318k 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 ...
2906
votes
60answers
3.5m 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:Attempt 1:randomNum=minimum + (int)(Math.random() * maximum);// Bug: `randomNum` can ...
2841
votes
7answers
3.1m views

Iterate through a HashMap [duplicate]

Possible Duplicate: How to efficiently iterate over each entry in a 'Map'?What's the best way to iterate over the items in a HashMap?
2647
votes
53answers
536k views

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?
2575
votes
30answers
894k views

When to use LinkedList over ArrayList?

I've always been one to simply use:List<String> names=new ArrayList<>();I use the interface as the type name for portability, so that when I ask questions such as these I can rework ...
2538
votes
38answers
2.0m views

How to efficiently iterate over each entry in a '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 ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,