Tagged Questions
Java is a general-purpose programming language designed to be used in conjugation with Java Virtual Machine (JVM). "Java platform" is a Java programming language and a set of products build around it. Use this tag for questions referring to Java programming language or Java platform as a whole.
730
votes
24answers
660k views
How do I compare strings in Java?
I've been using the == operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.
Is == bad? When should it ...
715
votes
23answers
100k views
How to avoid Java Code in JSP-Files?
I'm new to Java EE and I know that something like the following three lines
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
is an oldschool way of coding and in ...
171
votes
10answers
24k views
The Use of Multiple JFrames, Good/Bad Practice?
I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide, whether to use a separate JFrame to add Images to the Database from the GUI. I'm just ...
1380
votes
49answers
391k 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 it's not. I don't think I understand the distinction they're making.
What is ...
186
votes
15answers
358k views
What is a Null Pointer Exception, and how do I fix it?
What are null pointer exceptions, what causes them in general, and how do you track down the cause so that you can fix them?
150
votes
7answers
13k views
Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
Several times I've been criticized for having suggested the use of the following methods:
setPreferredSize
setMinimumSize
setMaximumSize
on Swing components. I don't see any alternative to their ...
29
votes
6answers
13k views
How to add JTable in JPanel
I want to add JTable into JPanel whose layout is null. JPanel contains other components. I have to add JTable at proper position.
779
votes
5answers
363k views
How to use java.net.URLConnection to fire and handle HTTP requests?
URLConnection is pretty often asked here and the Oracle tutorial is too concise about it. So
how do I use it to fire and handle HTTP requests?
Are there other hints and best practices on this that ...
198
votes
7answers
156k views
How to upload files to server using JSP/Servlet?
How can I upload files to server using JSP/Servlet? I tried this:
<form action="upload" method="post">
<input type="text" name="description" />
<input type="file" name="file" ...
602
votes
25answers
263k views
Overriding equals and hashCode in Java
What issues / pitfalls must be considered when overriding equals and hashCode?
227
votes
10answers
26k views
How do I write a correct micro-benchmark in Java?
How do you write (and run) a correct micro-benchmark in Java?
I'm looking here for code samples and comments illustrating various things to think about.
Example: Should the benchmark measure ...
198
votes
20answers
262k views
Java String.equals versus == [duplicate]
This code separates a string into tokens and stores them in an array of strings, and then compares a variable with the first home ... why isn't it working?
public static void ...
14
votes
4answers
2k views
Providing white space in a Swing GUI
A GUI with no white space appears 'crowded'. How can I provide white space without resorting to explicitly setting the position or size of components?
55
votes
3answers
21k views
JSTL in JSF2 Facelets… makes sense?
I would like to output a bit of Facelets code conditionally.
For that purpose, the JSTL tags seem to work fine:
<c:if test="${lpc.verbose}">
...
</c:if>
However, I'm not sure if ...
375
votes
6answers
81k views
How do servlets work? Instantiation, session variables and multithreading
Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am getting the servlet context and setting session variables. Now, if 2 or more users send ...
862
votes
30answers
1.1m views
Generating random integers in a range with Java
I am trying to generate a random integer with Java, but random in a specific range. For example, my range is 5-10, meaning that 5 is the smallest possible value the random number can take, and 10 is ...
68
votes
2answers
18k views
GUI not working after rewriting to MVC
I'm practicing MVC style programming. I have a Mastermind game in a single file, working fine (maybe apart of the fact that "Check" button is invisible at start).
http://paste.pocoo.org/show/226726/
...
15
votes
4answers
1k views
Java GUI listeners without AWT
I am a starting Java developer, learning just from internet tutorials. I am learning full screen GUI applications. I was told yesterday that I shouldn't use AWT in my programs, because it is outdated. ...
8151
votes
9answers
450k views
Why is processing a sorted array faster 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>
...
102
votes
2answers
27k views
How to choose the right bean scope?
I noticed that there are different bean scopes like:
@RequestScoped
@ViewScoped
@SessionScoped
@ApplicationScoped
What is the purpose of each? How do I choose a proper scope for my bean?
117
votes
7answers
48k views
How to use an existing database with an Android application [duplicate]
I have already created an SQLite database. I want to use this database file with my Android project. I want to bundle this database with my application.
Instead of creating a new database, how can ...
519
votes
39answers
507k views
How to sort a Map<Key, Value> on the values in Java?
I am relatively new to Java, and often find that I need to sort a Map on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through ...
103
votes
6answers
62k views
How to use Servlets and Ajax?
I'm very new to web apps and Servlets and I have the following question:
Whenever I print something inside the servlet and call it by the webbrowser, it returns a new page containing that text. Is ...
122
votes
8answers
35k views
Why JSF calls getters multiple times
Let's say I specify an outputText component like this:
<h:outputText value="#{ManagedBean.someProperty}"/>
If I print a log message when the getter for someProperty is called and load the ...
270
votes
21answers
132k views
What is an efficient way to implement a singleton pattern in Java?
What is an efficient way to implement a singleton pattern in Java?
250
votes
14answers
142k views
How can I convert my Java program to an .exe file? [closed]
If I have a Java source file (.java) or class file (.class). How can I convert it to an .exe file?
I also need an installer for my program.
Is there an open source program that can do that?
40
votes
4answers
32k views
Scanner issue when using nextLine after nextXXX
I've faced an issue when I'm trying to get the user input using Scanner:
import java.util.Scanner;
public class Main
{
public static Scanner input = new Scanner(System.in);
public static ...
122
votes
6answers
14k views
Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
I'm a bit confused about how Java generics handle inheritance / polymorphism.
Assume the following hierarchy -
Animal (Parent)
Dog - Cat (Children)
So suppose I have a method ...
5
votes
3answers
587 views
NullPointerException accessing views in onCreate()
This is a canonical question for a problem frequently posted on StackOverflow.
I'm following a tutorial. I've created a new activity using a wizard. I get NullPointerException when attempting to call ...
313
votes
15answers
530k views
How to round a number to n decimal places in Java
What I'd like is a method to convert a double to a string which rounds using the half-up method. I.e. if the decimal to be rounded is a 5, it always rounds up the previous number. This is the standard ...
366
votes
6answers
174k views
Download a file with Android, and showing the progress in a ProgressDialog
I am trying to write a simple application that gets updated. For this I need a simple function that can download a file and show the current progress in a ProgressDialog. I know how to do the ...
13
votes
3answers
10k views
Load Icon Image Exception
I am having a error for my GUI. Trying to set title bar icon then be included in a Runnable JAR.
BufferedImage image = null;
try
{
image = ...
279
votes
17answers
218k views
How to: generic array creation
Due to the implementation of Java generics, you can't have code like this:
public class GenSet<E> {
private E a[];
public GenSet()
{
a = new E[INITIAL_ARRAY_LENGTH]; // ...
30
votes
1answer
1k views
Unfortunately MyApp has stopped. How can I solve this?
I am developing an application, and everytime I run it, I get the message:
Unfortunately, MyApp has stopped.
What can I do to solve this?
About this question - obviously inspired by What is a ...
711
votes
26answers
320k views
Is there a unique Android device ID?
Do Android devices have a unique id, and if so, what is a simple way to access it via Java?
135
votes
10answers
53k views
What is a raw type and why shouldn't we use it?
Questions:
What are raw types in Java, and why do I often hear that they shouldn't be used in new code?
What is the alternative if we can't use raw types, and how is it better?
Similar questions
...
377
votes
20answers
351k views
Unsupported major.minor version 51.0
I am trying to use notepad++ as my all-in-one tool edit, run, compile etc.
I have JRE installed, I have setup my path variable to .../bin directory.
When I run my "Hello world" in notepad++, I get ...
125
votes
8answers
185k views
How do I import the javax.servlet API in my Eclipse project?
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet cannot be resolved. How can I add javax.servlet package to my Eclipse project?
702
votes
14answers
423k views
In Java, what's the difference between public, default, protected, and private?
Are there clear rules on when to use each of these when making classes and interfaces and dealing with inheritance?
288
votes
7answers
131k views
What is the difference between JSF, Servlet and JSP?
Is JSP = Servlet? And JSF = Pre-build UI based JSP (like asp.net web control)?
9
votes
1answer
729 views
Example images for code and mark-up Q&As
When preparing an SSCCE that involves images, it is useful to have direct access to images.
The types of images that would cover most questions are - small images in multiple colors or shapes, ...
64
votes
2answers
9k views
How to best position Swing GUIs
In another thread I stated that I liked to center my GUIs by doing something like this:
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
6
votes
2answers
5k views
Calling awt Frame methods from subclass
This question is about Frames, Java and Processing.
This questions sounds pretty convoluted but its really not. I'll try keep this to a simple minimum. I'm creating a small ball in a maze game to get ...
64
votes
2answers
35k views
What is a stack trace, and how can I use it to debug my application errors?
Sometimes when I run my application it gives me an error that looks like:
Exception in thread "main" java.lang.NullPointerException
at com.example.myproject.Book.getTitle(Book.java:16)
...
593
votes
27answers
244k views
“implements Runnable” vs. “extends Thread”
From what time I've spent with threads in Java, I've found these two ways to write threads.
public class ThreadA implements Runnable {
public void run() {
//Code
}
}
//with a "new ...
879
votes
16answers
216k 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 a static final
serialVersionUID field of type long
What is serialVersionUID and why is ...
233
votes
14answers
101k views
In Java, what is the best way to determine the size of an object?
For example, let's say I have an application that can read in a CSV file with piles of data rows. I give the user a summary of the number of rows based on types of data, but I want to make sure that ...
344
votes
4answers
124k views
Why is Java Vector class considered obsolete or deprecated?
Why is Java Vector considered a legacy class, obsolete or deprecated?
Isn't its use valid when working with concurrency?
And if I don't want to manually synchronize objects and just want to use a ...
71
votes
13answers
71k views
Evaluating a math expression given in string form
I am trying to write a Java routine to evaluate simple math expressions from Strings. Example strings:
"5+3" or "10-40" or "10*3"
I want to avoid a lot of if-then-else statements. How can I do this?
...
329
votes
23answers
273k views
How to call SOAP web service in Android
I am having a lot of trouble finding good information on how to call a standard SOAP/WSDL web service with Android. All I've been able to find are either very convoluted documents and references to ...