TutorialsJavaJava Introduction
Share:

Java Introduction

beginner

Part of Java Basics

Theory

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle) in 1995. It was designed with the principle of "Write Once, Run Anywhere" — meaning compiled Java code can run on any platform that has a Java Virtual Machine (JVM), without modification.

JVM, JRE, and JDK

Three key components make up Java's platform:

JVM (Java Virtual Machine) — The engine that runs Java bytecode. It translates bytecode into machine-specific instructions. The JVM is what makes Java platform-independent.

JRE (Java Runtime Environment) — The environment required to run Java programs. It includes the JVM plus core libraries.

JDK (Java Development Kit) — The full development toolkit. It includes the JRE plus development tools like the compiler (javac), debugger, and documentation tools.

Source Code (.java)
    ↓ javac (compiler)
Bytecode (.class)
    ↓ JVM (interpreter)
Machine Code

Platform Independence

When you compile a Java program, it produces bytecode (.class files), not machine code. This bytecode runs on the JVM. Since JVMs exist for Windows, macOS, Linux, and many other platforms, the same .class file can run on all of them without recompilation.

Installing Java

To start developing in Java:

  1. Download the latest JDK from Oracle or use OpenJDK (the open-source version)
  2. Set the JAVA_HOME environment variable
  3. Add JAVA_HOME/bin to your PATH

Verify your installation:

Check Java installation
bash

Basic Java Program

Every Java application starts with a class definition and a main method, which is the entry point:

HelloWorld.java
java

To compile and run:

Compile and run Java
bash

System.out.println

System.out.println() is the standard output method in Java. It prints text to the console and moves to a new line:

System.out.println("Hello");  // Prints "Hello" + newline
System.out.print("World");    // Prints "World" without newline
System.out.printf("%d items", 5);  // Formatted output

Packages

Packages are used to organize Java classes into namespaces:

package com.mycompany.myapp;
 
import java.util.Scanner;
import java.util.ArrayList;

Standard Java library packages include:

  • java.lang — automatically imported (String, Math, System)
  • java.util — utility classes (List, Map, Scanner)
  • java.io — input/output operations
  • java.net — networking

Comments

Java supports three types of comments:

// Single-line comment
 
/*
   Multi-line comment
   spanning multiple lines
*/
 
/**
 * Javadoc comment — used to generate API documentation
 * @param name the person's name
 * @return a greeting string
 */

Naming Conventions

Java follows specific naming conventions:

  • Classes: PascalCase (HelloWorld, UserAccount)
  • Methods: camelCase (getName(), calculateTotal())
  • Variables: camelCase (userName, itemCount)
  • Constants: UPPER_SNAKE_CASE (MAX_VALUE, PI)
  • Packages: all lowercase (com.example.myapp)

Consistent naming conventions make Java code more readable and maintainable. Most Java codebases follow these conventions strictly.

Practical Examples

Example 1: User Input and Output
java
Example 2: Using Packages and Math
java

Always close Scanner objects when you're done with them. This releases the underlying input stream resources.

Exercises

Your First Java Program

easy

Write a Java program that prints your name, your favorite color, and your age on three separate lines using System.out.println.

Expected Output:

Name: John\nFavorite Color: Blue\nAge: 25

Simple Calculator

easy

Write a Java program that declares two integer variables, calculates their sum, difference, product, and quotient, and prints each result.

Expected Output:

Sum: 19\nDifference: 11\nProduct: 60\nQuotient: 3

Fix the Errors

medium

The following Java program has several errors. Fix all syntax and convention errors so it compiles and runs correctly.

Expected Output:

Welcome to Java\nThis has errors

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Personal Information Formatter

Create a Java program that asks the user for their first name, last name, birth year, and current city. Then display a formatted profile summary with their estimated age and a greeting.

Requirements:

    Bonus Challenge

    Add validation: ensure the birth year is a reasonable number (1900-2020). If invalid, display an error message.