Questions tagged [java]

Java is a high-level programming language. Use this tag when you're having problems using or understanding the language itself. This tag is rarely used alone and is most often used in conjunction with [spring], [spring-boot], [jakarta-ee], [android], [swing], [javafx], [hadoop], [gradle] and [maven].

Java is a high-level, platform-independent, object-oriented, functional programming language and runtime environment.

The Java language derives much of its syntax from C and C++, but its object model is simpler than that of the latter and it has fewer low-level facilities. Java applications are typically compiled to bytecode (called class files) that can be executed by a JVM (Java Virtual Machine), independent of computer architecture. The JVM often further compiles code to native machine code to optimize performance.

The JVM manages memory with the help of a garbage collector (see ) to handle object removal from memory when objects are no longer in use. Java's typing discipline is static, strong, safe, nominative, and manifest. Java supports features such as reflection and interfacing with C and C++ via the JNI (Java Native Interface).

Java is designed to have as few implementation dependencies as possible, intended to allow application developers to write once, run anywhere, or sometimes write once, run everywhere (code that executes on one platform does not need to be recompiled to run on another). Java was originally developed by James Gosling at Sun Microsystems (which fully merged with Oracle Corporation on January 27th, 2010) and was released in 1995 as a core component of Sun Microsystems' Java platform. Java was started as a project called "Oak" by James Gosling in June 1991.

The Java platform is the name given by Sun (now Oracle) to computing systems that have installed tools for developing and running Java programs. The platform features a wide variety of tools that can help developers work efficiently with the Java programming language.

To get started, you download a Java Development Kit (JDK), which lets you both develop and run java applications.

Background

The main reference implementation of Java is open source (the OpenJDK) and is supported by major companies including Oracle, Apple, SAP, and IBM.

Very few computers can run Java programs directly. Java software often runs on servers. Java apps intended to run directly on client computers should ship with their own installers. (NB: Before java9, you asked end-users to install a so-called JRE; however, this concept has been deprecated).

Java is compiled into bytecode, which is then compiled by the JVM into native machine code. The compilation is done just-in-time (JIT). This was initially viewed as a performance hit, but JVM and JIT compilation improvements have made this less of a concern. In some cases, the JVM may even be faster than native code compiled to target an older version of a processor for backward compatibility reasons. Since Java 9, it can be compiled ahead-of-time (AOT).

Versions

Notable Java versions, code names, and release dates include:

Version Code Name Release Date
Java SE 16 JSR 391 March 16, 2021
Java SE 15 JSR 390 September 15, 2020
Java SE 14 JSR 389 March 17, 2020
Java SE 13 JSR 388 September 17, 2019
Java SE 12 JSR 386 March 19, 2019
Java SE 11 JSR 384 September 25, 2018
Java SE 10 JSR 383 March 20, 2018
Java SE 9 JSR 376 September 21, 2017
Java SE 8 (1.8) JSR 337 March 18, 2014
Java SE 7 (1.7) Dolphin July 28, 2011
Java SE 6 (1.6) Mustang December 11, 2006
J2SE 5.0 (1.5) Tiger September 29, 2004
J2SE 1.4 Merlin February 13, 2002
J2SE 1.3 Kestrel May 8, 2000
J2SE 1.2 Playground December 4, 1998
JDK 1.1 February 19, 1997
JDK 1.0 January 23, 1996
JDK Beta 1995

Since Java SE 10, new versions will be released every six months.

Latest Stable Versions:

Version Release Date
Java Standard Edition 16 (16.0.1) April 20, 2021
Java Standard Edition 15 (15.0.2) January 19, 2021
Java Standard Edition 14 (14.0.2) July 14, 2020
Java Standard Edition 13 (13.0.2) January 14, 2020
Java Standard Edition 12 (12.0.2) July 16, 2019
Java Standard Edition 11 (11.0.11) April 20, 2021
Java Standard Edition 10 (10.0.2) July 17, 2018
Java Standard Edition 9 (9.0.4) January 16, 2018
Java Standard Edition 8 Update 291 (1.8.0_291) April 20, 2021
Java Standard Edition 7 Update 80 (1.7.0_80) April 14, 2015

For more code names and release dates, visit Java SE Code Names. To see release notes for each version of the JDK, visit the Wikipedia article on Java version history.

To get a Java JDK so you can develop (or run) java code on your machine, you need a java installer package supplier. Java is open source, but a supplier will take care of building the java source code into a binary, wrapping this into an installer, and maintaining this version on your machine (e.g. with security updates).

Java comes in LTS (Long-Term-Support) and non-LTS versions. Each supplier chooses how to deal with LTS versions. Some (Oracle OpenJDK) have the same short support window for all releases, LTS or not. Some (Amazon Corretto) do not bother to release non-LTS versions at all. Most suppliers release all versions that will support LTS versions for far longer.

The LTS versions are- Java 8, 11, and 17.

The major suppliers are:

  • AdoptOpenJDK, also known as Adoptium - free, LTS versions and non-LTS versions, LTS versions are supported a long time. Recommended.
  • Oracle OpenJDK 15 - Note that these are only supported for 6 months, even the so-called LTS (Long Term Support) releases. You will need to update as java updates.
  • Oracle JavaSE - licensed for those who want commercial support. LTS and non-LTS versions, LTS versions are supported for a long time. A free but license-wise very restricted test version is available.
  • Amazon Coretto - free, maintained by Amazon, optimized for AWS, LTS versions only.
  • Many more commercial offerings.

The End of Public Updates (Formerly called End Of Life and also referred to as EoPU) dates for the freely available distribution from Oracle are:

Version EoPU Date
Java SE 16 September 2021 for OpenJDK
Java SE 15 March 2021 for OpenJDK
Java SE 14 September 2020 for OpenJDK
Java SE 13 March 2020 for OpenJDK
Java SE 12 September 2019 for OpenJDK
Java SE 11 At least September 2022 for AdoptOpenJDK
Java SE 10 September 2018
Java SE 9 March 2018
Java SE 8 January 2019 (commercial use)
Java SE 7 Apr 2015
Java SE 6 Feb 2013
J2SE 5.0 Oct 2009
J2SE 1.4 Oct 2008

Initial help

New to Java or need help to get your first Java program running? See the Oracle Java Tutorials section on Getting Started.

Before asking a question, please search to see if it has been asked before (we have many duplicates, some are listed below under Frequently Asked Questions), and read Writing the Perfect Question to learn how to get Jon Skeet to answer your question.

Naming conventions

Java programs should adhere to the following naming conventions to increase readability and decrease the chances of accidental errors. By following these naming conventions, you will make it easier for others to understand your code and help you.

  • Type names (classes, interfaces, enums, etc.) should begin with a capital letter and capitalize the first letter of each subsequent word. Examples include: String, ThreadLocal, and NullPointerException. This is sometimes known as PascalCase.
  • Method and field names should be camelCased; that is, they should begin with a lowercase letter and capitalize the first letter of each subsequent word. Examples: indexOf, printStackTrace, interrupt.
  • Constant expression names (static final immutable objects) should be written in ALL_CAPS_SNAKE_CASE, with underscores separating each word. Examples: YELLOW, DO_NOTHING_ON_CLOSE. This also applies to the values of an Enum class. However, static final references to non-immutable objects should be camelCased.

Hello World - Your first program

Code of a typical Hello World program:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compilation and invocation of Hello World program:

javac -d . HelloWorld.java
java -cp . HelloWorld

Java source code is compiled to an intermediate form (bytecode instructions for the Java Virtual Machine) which can be executed with the java command later on.

More information:

Useful IDEs for Java

Beginners' resources

Online Compilers

Day-to-day updated resources

Advanced resources

Java frameworks, libraries, and software

Java programming books and resources

Frequently Asked Questions

People often ask about the following Java topics:

General:

Classpath:

String, StringBuilder and toString:

equals and hashCode:

Java Platform SE API:

Generics:

Classes and objects:

Arithmetic and conversions:

Debugging:

Thread and multithreading:

Interacting with the operating system:

(Editors, please only list questions which actually are frequently asked.)

Chatrooms

1777893 questions
3293
votes
33 answers

When to use LinkedList over ArrayList in Java?

I've always been one to simply use: List 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 my code. When should LinkedList be used over ArrayList and…
sdellysse
  • 36,011
  • 9
  • 23
  • 24
3213
votes
49 answers

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 number it represents. For example, given the string "1234" the result should be the number 1234.
Unknown user
  • 40,827
  • 16
  • 36
  • 40
3157
votes
26 answers

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 it important? Please show an example where missing…
ashokgelal
  • 74,448
  • 25
  • 67
  • 83
2940
votes
32 answers

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList places = new ArrayList(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as…
Macarse
  • 87,001
  • 42
  • 169
  • 229
2900
votes
57 answers

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 function that is made private by having internal linkage (static in C/C++) or is in a private (anonymous) namespace? It seems bad to change…
MattGrommes
  • 11,124
  • 9
  • 34
  • 40
2831
votes
3 answers

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 code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000;…
Kuba Spatny
  • 24,958
  • 9
  • 33
  • 58
2598
votes
31 answers

How can I create an executable JAR with dependencies using Maven?

I want to package my project in a single executable JAR for distribution. How can I make a Maven project package all dependency JARs into my output JAR?
soemirno
  • 26,526
  • 3
  • 17
  • 14
2523
votes
64 answers

How to fix 'android.os.NetworkOnMainThreadException'?

I got an error while running my Android project for RssReader. Code: URL url = new URL(urlToRssFeed); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlreader =…
bejoy george
  • 27,155
  • 5
  • 15
  • 15
2506
votes
49 answers

Does a finally block always get executed in Java?

Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? try { something(); return success; } catch (Exception e) { return failure; } finally { …
jonny five
  • 25,492
  • 3
  • 18
  • 11
2421
votes
29 answers

How do I determine whether an array contains a particular value in Java?

I have a String[] with values like so: public static final String[] VALUES = new String[] {"AB","BC","CD","AE"}; Given String s, is there a good way of testing whether VALUES contains s?
Mike Sickler
  • 30,442
  • 17
  • 58
  • 89
2413
votes
23 answers

How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
ashokgelal
  • 74,448
  • 25
  • 67
  • 83
2273
votes
22 answers

What is reflection and why is it useful?

What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principles are the same in any language.
Lehane
  • 42,680
  • 14
  • 46
  • 53
2265
votes
29 answers

What's the difference between @Component, @Repository & @Service annotations in Spring?

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device? In other words, if I have a Service class and I change the annotation from…
Colin McCree
  • 22,707
  • 3
  • 12
  • 3
2224
votes
28 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an array in Java?
bestattendance
  • 23,477
  • 5
  • 23
  • 22
2212
votes
42 answers

"implements Runnable" vs "extends Thread" in Java

From what time I've spent with threads in Java, I've found these two ways to write threads: With implements Runnable: public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a "new Thread(new…
user65374
  • 22,779
  • 4
  • 17
  • 7