Introduction to Java: Understanding Key Concepts and Writing Your First Program

Java is a versatile and powerful programming language in the software development world. From web applications to mobile apps, Java plays a pivotal role due to its platform-independent nature and robust security features. In this post, we will start from the fundamentals of Java, discussing its core components such as JVM, JRE, and JDK, and later explain you through writing your first Java program.

Note: It is important to know that while Java is often thought of as an interpreted language because of the JVM, it is a statically typed language. This means that all variable types are explicitly declared and determined at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.

This is different from interpreted languages such as Python or JavaScript, which perform type checking at runtime. This means that you can assign any type of value to a variable, and the type can change over time as the script runs.
Core Components of Java

To get started with Java, it’s essential to understand the three main components of its environment:

Java Virtual Machine (JVM)

The JVM is the cornerstone of Java’s platform-independent functionality. It allows Java bytecode to be executed on any device that has the JVM installed, making Java applications portable across different operating systems and hardware configurations. The JVM acts as an interpreter for bytecode, converting bytecode into machine-specific instructions. This means that Java applications are compiled into an intermediate format (bytecode), which the JVM interprets and executes on the local machine’s hardware.

Java Runtime Environment (JRE)

The JRE provides a runtime environment by bundling the JVM, core libraries, and other components necessary to run Java applications. If you’re looking to simply run Java applications, installing the JRE is sufficient.

Java Development Kit (JDK)

For developers, the JDK provides a comprehensive toolkit for creating and managing Java applications. It includes the JRE, an array of development tools such as compilers (javac), document generators (javadoc), and more, necessary for developing Java applications.

Trivia

The Java logo having a steaming cup of coffee, reflects to its origins.

Java is actually an island in Indonesia known for its coffee. Back in the early 90s, when Sun Microsystems was developing the programming language, the devs spent a lot of time drinking coffee from a local café that served Java coffee. They decided to name the language “Java” as a nod to that coffee.

Development Tools Included in the JDK

The JDK is packed with tools that facilitate various aspects of Java application development:

  • javac: This is the Java compiler that turns source code into Java bytecode that the JVM can understand.
  • java: This launcher tool runs Java applications by starting the JVM.
  • javadoc: Generates useful HTML documentation from Java source code comments.
  • jar: Efficiently archives multiple files into a single JAR (Java ARchive), which simplifies the deployment of Java applications.
  • jdb: A powerful tool for debugging Java code.

and more.

Overview of Bytecode

Bytecode is created by the Java compiler (javac), which is a part of the Java Development Kit (JDK). It serves as the intermediate representation of Java code that allows for platform-independent execution.

The javac compiler transforms Java source code, which is written in the high-level Java programming language, into Java bytecode, which is a lower-level, platform-independent code. This allows Java programs to be write-once, run-anywhere (WORA), as long as the target device has a compatible JVM. This is because of not having the need to write individual compilers for different machines having different system architectures.

Although bytecode is not as fast as native machine code, the use of Just-In-Time (JIT) compilation at runtime can optimize bytecode to run nearly as fast as native code in some scenarios.

Note: First the Java code is compiled into bytecode, which the JVM then either interprets directly or compiles into native machine code using Just-In-Time (JIT) compilation. This hybrid approach generally makes Java slower than languages like C++, which are compiled directly into native machine code.
Writing Your First Java Program

Now, let’s jump into writing a basic Java program named Example.java, which outputs a simple message.

/*
 This is a simple Java program.
 Call this file "Example.java".
*/

class Example {
    // Your program begins with a call to main().
    public static void main(String[] args) {
        System.out.println("This is a simple Java program.");
    }
}

Compilation and Execution

  • Compile your program: Use the JDK’s javac compiler by running javac Example.java in your command line or terminal. This command compiles the Example.java source file into Example.class, which contains the bytecode.
  • Execute your program: Execute the program by running java Example in your command line or terminal. This command tells the JVM to run the Example class.
Understanding the Program
  • Class Declaration: Every Java program is defined inside a class. In our example, the class is named Example. The Java compiler requires that a source file use the .java filename extension. Also, by convention, the name of the main class should match the name of the file (case sensitive) that holds the program.
  • main Method: Execution starts with the main method. The public static void main(String[] args) signature enables the JVM to invoke this method without creating an instance of the class.
  • public: The public access modifier allows the method to be accessible from outside the class, including by the runtime system that starts Java programs.
  • static: The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java Virtual Machine before any objects are made.
  • void: The keyword void simply tells the compiler that main( ) does not return a value.
  • If the main method is missing any of these modifiers or does not have the correct parameters, Java will not be able to launch the application and will throw an error.

Some useful points to end the post:
  • It is important to understand that the Java compiler will compile classes that do not contain a main() method. But java will have no way to run these classes.
  • For a Java class to be executable directly using the java command, its main method must be both public and static. Additionally, it must accept a single parameter — a string array (String[] args).
  • A Java file can contain multiple classes, each with its own main method. This is perfectly valid as long as each main method is encapsulated within its respective class definition. In Java, the main method is the entry point for an application, and having multiple main methods simply means you have multiple entry points.
// Here's a simple example with two classes in the same Java file, each class having its own main method:

// First class with a main method
public class FirstClass {
    public static void main(String[] args) {
        System.out.println("Main method of FirstClass");
    }
}

// Second class with a main method
class SecondClass {
    public static void main(String[] args) {
        System.out.println("Main method of SecondClass");
    }
}

To run a specific main method, you would first compile the java file and then run the desired class:

  • Compile with: javac FileName.java
  • After compiling java file, FileName.java with javac, two separate java bytecode files will be created even though both classes were in a single file
  • Run a specific class with: java FirstClass or java SecondClass

This setup allows you to manage multiple runnable programs within a single Java file, each program being independent in terms of execution starting points. This happens regardless of whether the classes are public or have default (package-private) access.

However, only one class in the java file can be declared as public, and if a class is public, the filename must match the name of the public class. Each .class file can be executed independently by using the java command followed by the class name, provided it contains a main() method.

Java stands out due to its portability, robust tooling, and extensive community support. Also, it offers a stable foundation for building a variety of applications. By understanding its core components and getting hands-on experience writing simple programs, you’ll be well-equipped to foray into Java development.

Leave a Reply

Your email address will not be published. Required fields are marked *