Getting Started with Java: An Introduction

Java Introduction: Part I

Marcelo Domingues
devdomain
7 min readJun 25, 2024

--

Reference Image

Overview: Brief History of Java

Java is the most widely used programming language in the world today. The people who originally designed it are James Gosling and a team of developers at Sun Microsystems from way back in the middle of the 1990s. Its actual intent originally was to be a language used to drive interactive television; unfortunately, at that time, it was simply too advanced for digital cable television use. Originally, Java was named Oak after an oak tree that stood outside Gosling’s office.

Later, it was renamed Java, inspired by the coffee from Java Island in Indonesia. Finally, Java was released in 1995 and rapidly gained ground in virtue of its WORA nature, which promised the ability to run Java code on any device with a Java Virtual Machine, thereby making it platform-independent. Over the years, Java has continued to be in tandem with time, developing and growing further, with immense contributions from Oracle Corporation, which took over Sun Microsystems in 2010.

Java Features

Java’s popularity and longevity can be attributed to its robust set of features:

Platform Independence

One of the significant features of Java is platform independence. Programs in Java are compiled to bytecode, which can be executed on any gadget with a JVM. This enables developers to develop software that can run on different types of hardware and operating systems without any modification.

Object-Oriented

Object-oriented Java follows the concept of organizing objects within the code, being treated as classes regarding reusability, modularity, and flexibility. Major features of Java that reflect OOP are encapsulation, inheritance, and polymorphism.

Robustness

Java emphasizes early checking for possible errors since it is strongly typed. This can be achieved with its runtime checking and exception handling, which helps develop reliable and bug-free code. Some of its features, such as automatic garbage collection, manage memory effectively and avert problems related to memory leaks.

Security

Java is built with various security features to protect against threats; it is indeed a very secure runtime because of the use of the Java sandbox, which limits what Java programs can do once they are running on your system. Other Java security features include cryptography, public critical infrastructure, and secure communication protocols.

Multithreading

Java is capable of running multiple threads in a single program. This becomes useful to a large extent when a task like an animation, processing data, or when games are run — all such tasks take place simultaneously and independently from the rest. It is more convenient nowadays to build and manage multithreaded applications using the new java.util.concurrent package.

High Performance

Java is made robust through JIT (Just-In-Time) compilation. On-the-fly translation of bytecode to native machine code leads to optimized performance. While less performant than programs written in C or C++, Java’s performance is good enough for virtually all programs.

Distributed Computing

Java is prepared for the distributed environment while putting much emphasis on the network-centric aspects. The vast libraries in Java, such as the java.net package, make it easy to create programs to communicate with each other over networks.

Setting Up the Environment

Installing JDK

To develop and run Java applications, the Java Development Kit (JDK) is used. As of the time of this writing, the current release of the JDK is JDK 21. Below are instructions on how to install JDK on Windows, macOS, and Linux:

  1. Download the JDK:

2. Install the JDK:

2.1 Windows:

  • Run the downloaded installer.
  • Follow the installation prompts.
  • By default, the JDK is installed in C:\Program Files\Java\jdk-21.

2.1.1 Set Up Environment Variables:

  • Open the System Properties (right-click on This PC or My Computer and select Properties).
  • Click on Advanced system settings, then Environment Variables.
  • Under System variables, click New and add JAVA_HOME as the variable name and the path to your JDK installation as the variable value (e.g., C:\Program Files\Java\jdk-21).
  • Find the Path variable in the list, select it, and click Edit. Add %JAVA_HOME%\bin to the list.

2.2 macOS:

  • Open the downloaded DMG file.
  • Drag the JDK to your Applications folder.

2.2.1 Set Up Environment Variables:

  • Open a terminal.
  • Run the command nano ~/.zshrc (or ~/.bash_profile if using bash).
  • Add the following lines:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
  • Save and close the file (Ctrl+X, then Y, then Enter).
  • Run the command source ~/.zshrc (or source ~/.bash_profile if using bash) to apply the changes.

2.3 Linux:

  • Extract the downloaded tar.gz file:
tar -xvzf jdk-21_linux-x64_bin.tar.gz
  • Move the JDK folder to /usr/local/:
sudo mv jdk-21 /usr/local/

2.3.1 Set Up Environment Variables:

  • Open your profile file (~/.bashrc, ~/.zshrc, or ~/.profile)
nano ~/.bashrc
  • Add the following lines:
export JAVA_HOME=/usr/local/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
  • Save and close the file (Ctrl+X, then Y, then Enter).
  • Run the command source ~/.bashrc (or the appropriate file) to apply the changes.

Verify the Installation:

  • Open a terminal or command prompt.
  • Type java -version and javac -version to verify that the JDK is installed and configured correctly.

Setting Up an IDE

Integrated Development Environments (IDEs) make Java development easier by providing a comprehensive suite of tools for coding, debugging, and testing. Two popular Java IDEs are IntelliJ IDEA and Eclipse.

IntelliJ IDEA

  1. Download and Install IntelliJ IDEA:
  • Go to the IntelliJ IDEA download page.
  • Download the Community edition (free) or the Ultimate edition (paid with additional features).
  • Follow the installation instructions for your operating system.

2. Configure IntelliJ IDEA:

  • Launch IntelliJ IDEA.
  • On the Welcome screen, click New Project.
  • Select Java and make sure the correct JDK version (21) is selected.
  • Click Next, then Finish to create a new project.

3. Create a Java Class:

  • In the Project window, right-click on the src folder, select New -> Java Class.
  • Enter the class name (e.g., Main) and click OK.
  • IntelliJ IDEA will create a new Java class file.

4. Write and Run Your First Program:

  • In the new class file, add the following code
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
  • Click the green Run button in the toolbar to compile and run your program.

Eclipse

1. Download and Install Eclipse:

  • Go to the Eclipse download page.
  • Download the Eclipse IDE for Java Developers.
  • Follow the installation instructions for your operating system.

2. Configure Eclipse:

  • Launch Eclipse.
  • Select a workspace directory where your projects will be stored and click Launch.
  • In the Welcome screen, click Create a new Java project.

3. Create a Java Project:

  • Enter a project name (e.g., HelloWorld) and click Next.
  • Ensure the correct JDK version (21) is selected.
  • Click Finish to create the project.

4. Create a Java Class:

  • In the Project Explorer, right-click on the src folder, select New -> Class.
  • Enter the class name (e.g., Main) and click Finish.

5. Write and Run Your First Program:

  • In the new class file, add the following code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
  • Click the green Run button in the toolbar to compile and run your program.

Practical Examples

Hello World Example

Let’s start with the classic “Hello, World!” program to verify our setup.

Code:

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

Output:

Hello, World!

Simple Addition Program

A simple program to add two numbers and print the result.

Code:

import java.util.Scanner;

public class Addition {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number:");
int num1 = scanner.nextInt();
System.out.println("Enter second number:");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
}
}

Output:

Enter first number:
10
Enter second number:
20
The sum is: 30

Using a Method to Calculate Factorial

A program to calculate the factorial of a number using a method.

Code:

public class Factorial {
public static void main(String[] args) {
int number = 5; // Change this number to calculate the factorial of a different number
long result = factorial(number);
System.out.println("Factorial of " + number + " is " + result);
}

public static long factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}

Output:

Factorial of 5 is 120

Conclusion

Java is a highly expressive, versatile, and powerful programming language. These are the attributes it has developed in association with its history and the comprehensive set of features it offers. Java is considered suitable for large varieties of applications due to platform independence, robustness, security, multithreading, and distributed computing. Installing a Java development environment should be accomplished by downloading the latest JDK (version 21) and setting up a development environment using an IDE like IntelliJ IDEA or Eclipse. In this setup, you can now write and run Java programs, which means you are open to a whole universe of development opportunities. Practical examples will just make concepts rock-solid and get you initiated into Java programming.

Github

References

  • Oracle. (2024). Java SE Development Kit 21 — Downloads. Retrieved from Oracle Website
  • JetBrains. (2024). IntelliJ IDEA: The Java IDE for Professional Developers by JetBrains. Retrieved from JetBrain Website
  • Eclipse Foundation. (2024). Eclipse IDE 2023–09. Retrieved from Eclipse Website
  • Baeldung. (2024). Java Hello World Example. Retrieved from Baeldung

Explore More on Spring and Java Development:

Enhance your skills with our selection of articles:

  • Java Lambda Expressions: Techniques for Advanced Developers (Jun 19, 2024): Dive deep into advanced usage of lambda expressions in Java. Read More
  • Mastering Spring Security: Roles and Privileges (Jun 19, 2024): Learn the essentials of managing roles and privileges in Spring Security. Read More
  • Publishing Your Java Library to Maven Central: A Step-by-Step Tutorial (Mar 25, 2024): A comprehensive guide to making your Java library accessible to millions of developers worldwide. Read More
  • The Art of Unit Testing: Elevate Your Code with Effective Mocks (Mar 13, 2024): A complete guide to using mocks in unit testing, emphasizing the benefits of component isolation. Read More
  • Spring Beans Mastery (Dec 17, 2023): Unlock advanced application development techniques. Read More
  • JSON to Java Mapping (Dec 17, 2023): Streamline your data processing. Read More
  • Spring Rest Tools Deep Dive (Nov 15, 2023): Master client-side RESTful integration. Read More
  • Dependency Injection Insights (Nov 14, 2023): Forge better, maintainable code. Read More
  • Spring Security Migration (Sep 9, 2023): Secure your upgrade smoothly. Read More
  • Lambda DSL in Spring Security (Sep 9, 2023): Tighten security with elegance. Read More
  • Spring Framework Upgrade Guide (Sep 6, 2023): Navigate to cutting-edge performance. Read More

--

--

Marcelo Domingues
devdomain

🚀 Senior Software Engineer | Crafting Code & Words | Empowering Tech Enthusiasts ✨ 📲 LinkedIn: https://www.linkedin.com/in/marcelogdomingues/