Java Programming
Brief History of Java
Origins and Development at Sun Microsystems
James Gosling, Mike Sheridan and Patrick Naughton created Java at Sun Microsystems, part of Oracle Corporation. Initially known as "Oak " the project sought to develop software for consumer electronics. However with the expansion of the internet the team redirected their efforts towards crafting a programming language with web applications. Java made its debut in 1995. Swiftly became widely embraced for its versatility, across different platforms enabling code execution without significant modifications.
Evolution and Key Features
Java's object-oriented programming (OOP) model was crucial to its success, enabling modular and reusable code. The introduction of the Java Virtual Machine (JVM) allowed Java programs to run independently of the underlying hardware and operating system. Over the years, Java has continued to evolve with numerous updates, becoming a preferred language for web, enterprise, and mobile app development.
Java SE, EE, and ME
Java SE (Standard Edition)
Java SE is the most widely used version of Java, providing the basic tools and libraries necessary for developing applications on desktops and servers.
Java EE (Enterprise Edition)
Java EE is designed for developing large-scale, distributed applications that run on servers. It includes additional features and tools for building robust enterprise applications.
Java ME (Micro Edition)
Java ME is a lightweight version of Java used for developing applications for small, embedded systems like mobile and IoT devices.
Java Virtual Machine (JVM) and Bytecode Execution
The JVM is a crucial component that allows Java applications to run on various platforms. When Java code is compiled, it is converted into bytecode, a platform-independent format. The JVM interprets this bytecode and translates it into machine-specific instructions, enabling Java programs to be executed on different hardware and operating systems. The JVM also manages memory, performs garbage collection, and optimizes code at runtime.
Getting Started with Java Development
Setting Up the Java Development Kit (JDK)
- Download the latest version of JDK from the official Oracle website.
- Install the JDK by running the installer and following the on-screen instructions.
- Set up environment variables:
- Go to Control Panel > System and Security > System > Advanced system settings > Environment Variables.
- Create a new system variable named
JAVA_HOME
and set its value to the JDK installation directory. - Edit the
Path
variable and add%JAVA_HOME%\bin
to the end of the value.
- Verify installation by opening Command Prompt and typing
java -version
.
Writing Your First Java Program
1. Create a Java file named HelloWorld.java
with the following code:
2. Compile the program using javac HelloWorld.java
.
3. Run the program using java HelloWorld
.
Understanding Java Syntax
Variables, Data Types, and Operators
In Java, variables are used to store data, and data types define the kind of data a variable can hold, such as integers, characters, or strings. Operators perform operations on variables or constants, with common types including arithmetic (+
, -
), comparison (==
, !=
), and logical (&&
, ||
) operators.
Control Flow Statements (If, Else, Loops)
Control flow statements manage the execution flow of a program:
- If statements evaluate a condition and execute code if the condition is true.
- Else statements provide an alternative code path if the condition is false.
- For loops repeat a block of code a specific number of times.
- While loops repeat code as long as a condition is true.
- Do-while loops ensure that the code is executed at least once before evaluating the condition.