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.

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 also garbage-collection) to handle object removal from memory when objects are no longer in use. This means the programmer doesn't have to manually remove objects from memory, unlike in languages such as C. Java's typing discipline is static, strong, safe, nominative, and manifest. Java supports features such as reflection, interfacing with C and C++ via the JNI (Java Native Interface), and running scripting languages such as JavaScript.

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, Microsoft, SAP, and IBM.

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).

Very few computers can run Java programs directly. Before Java 9, users running Java programs were expected to have the JRE (Java Runtime Environment) installed on their system. Modern Java programs intended to run directly on client computers now ship with their own installers.

Versions

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

Version Code Name Release Date
Java SE 20 JSR 395 March 21, 2023
Java SE 19 JSR 394 September 20, 2022
Java SE 18 JSR 393 March 22, 2022
Java SE 17 JSR 392 September 14, 2021
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 20 (20.0.2) July 18, 2023
Java Standard Edition 19 (19.0.2) January 17, 2023
Java Standard Edition 18 (18.0.2.1) August 18, 2022
Java Standard Edition 17 (17.0.8) July 18, 2023
Java Standard Edition 16 (16.0.2) July 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.20) July 18, 2023
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 381 (1.8.0_381) July 18, 2023
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 - 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

1906089 questions
228
votes
4 answers

Illegal pattern character 'T' when parsing a date string to java.util.Date

I have a date string and I want to parse it to normal date use the java Date API,the following is my code: public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; …
hguser
  • 35,079
  • 54
  • 159
  • 293
228
votes
11 answers

How to render a PDF file in Android

Android does not have PDF support in its libraries. Is there any way to render PDF files in the Android applications?
alexleutgoeb
  • 3,123
  • 3
  • 20
  • 31
228
votes
31 answers

Java 8 stream reverse order

General question: What's the proper way to reverse a stream? Assuming that we don't know what type of elements that stream consists of, what's the generic way to reverse any stream? Specific question: IntStream provides range method to generate…
vach
  • 10,571
  • 12
  • 68
  • 106
228
votes
4 answers

@RequestParam in Spring MVC handling optional parameters

Is it possible for a Spring controller to handle both kind of requests? 1) http://localhost:8080/submit/id/ID123432?logout=true 2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432 If I define a single controller of the kind: …
luksmir
  • 3,104
  • 5
  • 22
  • 38
228
votes
3 answers

Generating a Random Number between 1 and 10 Java

I want to generate a number between 1 and 10 in Java. Here is what I tried: Random rn = new Random(); int answer = rn.nextInt(10) + 1; Is there a way to tell what to put in the parenthesis () when calling the nextInt method and what to add?
Shania
  • 2,475
  • 3
  • 13
  • 11
228
votes
4 answers

RabbitMQ and relationship between channel and connection

The RabbitMQ Java client has the following concepts: Connection - a connection to a RabbitMQ server instance Channel - ??? Consumer thread pool - a pool of threads that consume messages off the RabbitMQ server queues Queue - a structure that holds…
user1768830
228
votes
18 answers

How can I find Java heap size and memory used (Linux)?

How can I check heap size (and used memory) of a Java application on Linux through the command line? I have tried through jmap. But it gives information about internal memory areas, like Eden, PermGen, etc., which is not useful to me. I am looking…
Jasper
  • 8,440
  • 31
  • 92
  • 133
228
votes
7 answers

Difference between java.util.Random and java.security.SecureRandom

My team got handed over some server side code (in Java) that generates random tokens and I have a question regarding the same - The purpose of these tokens is fairly sensitive - used for session id, password reset links etc. So they do need to be…
user967973
  • 2,475
  • 2
  • 18
  • 13
227
votes
9 answers

Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot?

There is quite a lot of gui frameworks out there for java, but what is recognized as today's framework of choice? The following is my understanding of the different frameworks, please correct me if im wrong. This is a very loosely defined set of…
netbrain
  • 9,194
  • 6
  • 42
  • 68
227
votes
11 answers

JAX-RS / Jersey how to customize error handling?

I'm learning JAX-RS (aka, JSR-311) using Jersey. I've successfuly created a Root Resource and am playing around with parameters: @Path("/hello") public class HelloWorldResource { @GET @Produces("text/html") public String get( …
Mark Renouf
  • 30,697
  • 19
  • 94
  • 123
227
votes
20 answers

Maven surefire could not find ForkedBooter class

Recently coming to a new project, I'm trying to compile our source code. Everything worked fine yesterday, but today is another story. Every time I'm running mvn clean install on a module, once reaching the tests, it crashes into an error: [INFO]…
Sylordis
  • 2,528
  • 3
  • 17
  • 20
227
votes
9 answers

Variable used in lambda expression should be final or effectively final

Variable used in lambda expression should be final or effectively final When I try to use calTz it is showing this error. private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) { try { …
user3610470
  • 2,271
  • 2
  • 12
  • 7
227
votes
7 answers

Are there best practices for (Java) package organization?

A little while ago, I saw a question answered here regarding the fine-grained organization of Java packages. For example, my.project.util, my.project.factory, my.project.service, etc. Are there best practices with regards to the organization of…
Cyntech
  • 5,362
  • 6
  • 33
  • 47
227
votes
11 answers

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized…
wuntee
  • 12,170
  • 26
  • 77
  • 106
227
votes
16 answers

Why would an Enum implement an Interface?

I just found out that Java allows enums to implement an interface. What would be a good use case for that?
unj2
  • 52,135
  • 87
  • 247
  • 375