Learning Objectives
Understand Java vs C
Learn the key differences between Java and C programming languages
Setup Development Environment
Install and configure Java JDK and IDE for development
Write First Java Program
Create, compile, and run your first Java application
Basic Input/Output
Master basic input/output operations using Scanner class
Introduction to Java
What is Java?
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It's designed to be platform-independent, meaning Java programs can run on any device that has a Java Virtual Machine (JVM).
Key Features
- Platform Independent: Write once, run anywhere
- Object-Oriented: Everything is an object
- Secure: Built-in security features
- Robust: Strong memory management
- Multithreaded: Built-in support for concurrent programming
Java vs C: Key Differences
Aspect | C | Java |
---|---|---|
Paradigm | Procedural | Object-Oriented |
Memory Management | Manual (malloc/free) | Automatic (Garbage Collection) |
Platform | Platform-dependent | Platform-independent |
Compilation | Direct to machine code | Compile to bytecode, run on JVM |
Pointers | Direct memory access | No direct pointer manipulation |
Strings | Character arrays | String objects |
Arrays | Static, fixed size | Dynamic, can grow |
Java Virtual Machine (JVM)
The JVM is what makes Java "Write Once, Run Anywhere":
Compilation
Java source code (.java) ? Bytecode (.class)
Execution
JVM interprets bytecode for the specific platform
Portability
Same bytecode runs on Windows, macOS, Linux, etc.
Java 24 New Features
Pattern Matching
Enhanced switch expressions and instanceof
String Templates
String interpolation (Preview feature)
Foreign Function & Memory API
Better C interoperability
Vector API
SIMD operations for performance
Structured Concurrency
Simplified concurrent programming
Development Environment
Required Software
Integrated Development Environment (IDE)
For writing and managing code
Build Tools
Maven or Gradle (introduced later)
IDE Options
IntelliJ IDEA Community Edition
RecommendedBest for beginners with excellent code completion and debugging
- Free Community Edition
- Excellent Java support
- Smart code completion
- Integrated debugging
Eclipse IDE
Popular open-source IDE with extensive plugin ecosystem
- Completely free
- Many plugins available
- Good Java support
- Steep learning curve
Visual Studio Code
Lightweight editor with Java extensions
- Free and lightweight
- Requires Java extensions
- Fast and responsive
- Less Java-specific features
Basic Syntax & Structure
Java File Structure
Every Java program must follow this structure:
File: HelloWorld.java
// File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
// Your code here
System.out.println("Hello, World!");
}
}
Key Components Explained
1. Class Declaration
public class HelloWorld
public
: Access modifier (accessible from anywhere)class
: Keyword to define a classHelloWorld
: Class name (must match filename)
2. Main Method
public static void main(String[] args)
public
: Accessible from anywherestatic
: Belongs to class, not instancevoid
: Returns nothingmain
: Method name (entry point)String[] args
: Command-line arguments
3. Method Body
{
// Statements go here
}
Naming Conventions
Classes
PascalCase (e.g., HelloWorld
, StudentRecord
)
Methods/Variables
camelCase (e.g., getStudentName
, totalScore
)
Constants
UPPER_SNAKE_CASE (e.g., MAX_SIZE
, PI
)
Packages
lowercase (e.g., com.example.project
)
String Concatenation
String concatenation is the process of joining (combining) multiple strings together to create a single string. In Java, you use the +
operator to concatenate strings.
Basic String Concatenation
// Simple string concatenation
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "John Doe"
// Concatenating strings with numbers
int age = 25;
String message = "I am " + age + " years old"; // "I am 25 years old"
// Multiple concatenations
String greeting = "Hello, " + firstName + "! You are " + age + " years old.";
// Result: "Hello, John! You are 25 years old."
System.out.println(greeting);
Important Rules
- String + String: Results in a new combined string
- String + Number: Number is automatically converted to string
- Number + String: Number is automatically converted to string
- Number + Number + String: Numbers are added first, then concatenated
Order Matters!
int a = 5;
int b = 3;
String result1 = a + b + " is the sum"; // "8 is the sum" (5+3=8, then "8"+" is the sum")
String result2 = "The sum is " + a + b; // "The sum is 53" ("The sum is " + "5" + "3")
String result3 = "The sum is " + (a + b); // "The sum is 8" (parentheses force addition first)
Your First Java Program
Simple Hello World Example
Let's start with the most basic Java program - the traditional "Hello World":
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
What This Program Does:
- Class Declaration:
public class HelloWorld
- defines a class named HelloWorld - Main Method:
public static void main(String[] args)
- the entry point of the program - Output Statement:
System.out.println()
- prints text to the console - String:
"Hello, World!"
- the text to be displayed
Compilation and Execution
Command Line Compilation
# Compile the Java file
javac FirstProgram.java
# Run the compiled program
java FirstProgram
What Happens During Compilation?
- Syntax Check: Java compiler checks for syntax errors
- Bytecode Generation: Creates .class file with bytecode
- Error Reporting: Shows any compilation errors
Common Compilation Errors
;
required at end of statements
{
and }
must be balanced
IDE Features
IntelliJ IDEA Basics
- Project Creation: File ? New ? Project
- File Creation: Right-click package ? New ? Java Class
- Run Configuration: Green play button or Shift+F10
- Debug: Bug icon or Shift+F9
- Code Completion: Ctrl+Space
- Quick Fix: Alt+Enter
Eclipse Basics
- Project Creation: File ? New ? Java Project
- File Creation: Right-click package ? New ? Class
- Run: Green play button or Ctrl+F11
- Debug: Bug icon or F11
- Code Completion: Ctrl+Space
- Quick Fix: Ctrl+1
Summary
Fundamental Differences
Learned the key differences between Java and C
Environment Setup
Set up Java development environment
Basic Syntax
Understood Java syntax and program structure
Compilation & Execution
Write, compile, and run Java programs
Input/Output Operations
Basic input/output operations using Scanner
Best Practices
Java naming conventions and best practices
String Concatenation
Using + operator to combine strings and numbers
Key Terms
JVM (Java Virtual Machine)
Runtime environment for Java
Bytecode
Intermediate representation of Java code
Class
Blueprint for creating objects
Method
Function within a class
Package
Namespace for organizing classes
Scanner
Class for reading input from various sources