Introduction to Java
Learn the basics of the Java programming language and its ecosystem
Introduction to Java
Welcome to your first lesson in Java programming! In this lesson, we'll explore what Java is, its history, features, and how to set up your development environment.
What is Java?
Java is a high-level, class-based, object-oriented programming language that was developed by Sun Microsystems (now owned by Oracle) in the mid-1990s. It was designed with the goal of allowing developers to "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
Key Features of Java
- Platform Independence: Java programs are compiled into bytecode that can run on any device with a Java Virtual Machine (JVM).
- Object-Oriented: Java follows the object-oriented programming paradigm, focusing on creating objects that contain data and methods.
- Simple and Familiar: Java was designed to be easy to learn, especially for programmers familiar with C and C++.
- Robust and Secure: Java provides automatic memory management, exception handling, and strict compile-time type checking.
- Multithreaded: Java supports concurrent programming, allowing you to write programs that can perform multiple tasks simultaneously.
Setting Up Your Development Environment
To start coding in Java, you'll need to set up a development environment:
- Install the Java Development Kit (JDK):
- Visit Oracle's JDK download page or use OpenJDK
- Download and install the version appropriate for your operating system
- Verify installation by opening a terminal or command prompt and typing:
java -version
- Choose an Integrated Development Environment (IDE):
- IntelliJ IDEA: A powerful IDE with a robust set of features
- Eclipse: A popular open-source IDE with extensive plugin support
- VS Code: A lightweight editor with Java extensions
- NetBeans: An IDE that simplifies Java development with drag-and-drop features
- Create Your First Java Program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java World!");
}
}
Save this code in a file named HelloWorld.java
, compile it with javac HelloWorld.java
, and run it with java HelloWorld
.
Java Syntax Basics
Variables and Data Types
Java has two categories of data types:
- Primitive Types:
byte
: 8-bit integershort
: 16-bit integerint
: 32-bit integerlong
: 64-bit integerfloat
: 32-bit floating-pointdouble
: 64-bit floating-pointchar
: 16-bit Unicode characterboolean
: true or false
- Reference Types:
- Objects
- Arrays
- Strings
Example:
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isEmployed = true;
String name = "John Doe";
Basic Operators
- Arithmetic:
+
,-
,*
,/
,%
- Assignment:
=
,+=
,-=
,*=
,/=
- Comparison:
==
,!=
,>
,<
,>=
,<=
- Logical:
&&
,||
,!
Control Flow
- If-Else Statements:
if (condition) { // code block } else if (anotherCondition) { // code block } else { // code block }
- Switch Statements:
switch (variable) { case value1: // code block break; case value2: // code block break; default: // code block }
- Loops:
// For loop for (int i = 0; i < 5; i++) { System.out.println(i); } // While loop int j = 0; while (j < 5) { System.out.println(j); j++; } // Do-while loop int k = 0; do { System.out.println(k); k++; } while (k < 5);
Practice Exercise
Create a simple Java program that:
- Declares variables of different data types
- Performs basic arithmetic operations
- Uses if-else statements to make decisions
- Implements a for loop to iterate through numbers
Summary
In this lesson, you've learned:
- What Java is and its key features
- How to set up a Java development environment
- Basic Java syntax, including variables, operators, and control flow statements
In the next module, we'll dive into Object-Oriented Programming concepts in Java, starting with classes and objects.