Welcome to the world of Java! Java is one of the most popular and widely-used programming languages, powering everything from mobile apps to large-scale enterprise systems.
On IntricateDevo, you don't need to install anything to run the code examples. You can use the "Intricate yourself >>" button to run Java code directly in your browser!
Java is a high-level, object-oriented, and platform-independent programming language created by Sun Microsystems in 1995. Its core philosophy is "Write Once, Run Anywhere" (WORA). This means that Java code compiled on one platform (like Windows) can run on any other platform that has a Java Virtual Machine (JVM) without needing to be recompiled.
If you want to write and run Java code on your own computer, you need to install the Java Development Kit (JDK).
javac), a debugger, and other tools necessary to develop Java applications.To start programming, you need the JDK. You can download it for free from the official Oracle website or use an open-source alternative like OpenJDK.
Let's write the traditional first program that every programmer creates. It's a simple program that prints the text "Hello, World!" to the console.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
public class Main: Every Java application must have at least one class definition. The class keyword is used to declare a class. Main is the name of our class. By convention, class names start with a capital letter. The file name must match the public class name (e.g., Main.java).
public static void main(String[] args): This is the main method. It's the entry point of any Java application. The code inside the main method is what gets executed when you run the program.
public: An access modifier, meaning this method is accessible from anywhere.static: This keyword allows the main method to be executed without creating an instance of the Main class.void: This indicates that the main method does not return any value.main: The name of the method.(String[] args): This is a parameter that accepts command-line arguments as an array of strings.System.out.println("Hello, World!");: This line does the actual work of printing text.
System: A pre-defined class in Java that provides access to the system.out: An object within the System class that represents the standard output stream (usually the console).println(): A method of the out object that prints the given text followed by a new line.If you have the JDK installed, you can compile and run your program using a terminal or command prompt.
Save the code: Save the code above in a file named Main.java.
Open the terminal: Navigate to the directory where you saved the file.
Compile the code: Run the Java compiler, javac.
javac Main.java
This command will create a new file called Main.class, which contains the Java bytecode.
Run the program: Run the Java application launcher, java.
java Main
Notice that you don't include the .class extension. The output will be:
Hello, World!
While the interactive editor is great for learning, eventually you will want to run Java on your own machine. Here is a step-by-step guide to installing the JDK.
Oracle provides a commercial JDK, but the open-source OpenJDK is entirely free and functionally identical for most purposes. Sites like Adoptium (Eclipse Temurin) or Amazon Corretto provide excellent, free OpenJDK binaries.
.msi installer..pkg installer and follow the wizard.brew install --cask temurinOpen a new terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type:
java -version
You should see output similar to:
openjdk version "21.0.1" 2023-10-17 LTS OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode)
If your terminal says 'javac' is not recognized as an internal or external command, you need to set your environment variables manually.
C:\Program Files\Eclipse Adoptium\jdk-21...\bin). Copy this path.Path and select it, then click Edit.javac -version again.While you can write Java in standard text editors like Notepad, using an IDE makes coding significantly easier by providing code completion, syntax highlighting, and powerful debugging tools.
Here are the top choices for Java developers:
Developed by JetBrains, IntelliJ IDEA is widely considered the best IDE for Java. The "Community Edition" is completely free and more than sufficient for learning and developing standard applications. It offers the smartest code completion and refactoring tools available.
Eclipse is a classic, open-source IDE that has been around for decades. It has a massive ecosystem of plugins and is still heavily used in many enterprise environments.
VS Code is a lightweight, incredibly popular code editor developed by Microsoft. While not a full-fledged Java IDE out of the box, installing the "Extension Pack for Java" transforms it into a highly capable environment. It's a great choice if you already use VS Code for other languages like JavaScript or Python.
main Method SignatureLet's take a closer look at the signature of the main method, as it can look intimidating to beginners:
public static void main(String[] args)
public: This is an access modifier. It signifies that the method can be accessed from anywhere, including by the Java Virtual Machine (JVM) which exists outside of your program. If it were private, the JVM wouldn't be able to find and run it.static: This keyword means that the method belongs to the class itself, rather than an instance (object) of the class. Because the main method is the entry point, the JVM needs to call it before any objects have been created.void: This is the return type. It means that the main method does not return any value to the operating system when it finishes executing.main: This is the specific name that the JVM looks for when starting a program. It is case-sensitive; Main or MAIN will not work.String[] args: This is the parameter passed to the method. It is an array of Strings (text). It stores "command-line arguments". When you run a Java program from the terminal, you can pass extra information to it (e.g., java Main arg1 arg2), and that information is stored in this array.As you start writing your own Java code, you might run into a few common pitfalls. Keep an eye out for these:
public, the file name must exactly match the class name. If your class is public class MyProgram, the file must be saved as MyProgram.java.System is not the same as system. String is not the same as string.;). Forgetting one is the most common cause of compilation errors.{ must have a corresponding closing brace }. This is how Java defines blocks of code. IDEs help with this by highlighting matching braces.What is the main entry point for any Java application?