# Java Beginner Guide - Part 1: Fundamentals ## Table of Contents - [Introduction to Java](#introduction-to-java) - [Setting Up Your Environment](#setting-up-your-environment) - [Your First Java Program](#your-first-java-program) - [Understanding Variables](#understanding-variables) - [Data Types in Java](#data-types-in-java) - [Operators](#operators) - [Type Conversion](#type-conversion) - [Comments and Code Organization](#comments-and-code-organization) - [Practice Exercises](#practice-exercises) --- ## Introduction to Java ### What is Java? Java is a **powerful, object-oriented programming language** that runs on billions of devices. Think of Java as a universal language for building all kinds of applications: - **Android apps** (the apps on your phone) - **Web applications** (banking, shopping sites) - **Enterprise software** (big business systems) - **Desktop applications** (like IntelliJ, Minecraft) - **Games** (Minecraft was built with Java!) Java was created in 1995 by Sun Microsystems (now owned by Oracle). Its famous slogan is "Write Once, Run Anywhere" - meaning you write Java code once, and it can run on any device that has Java installed! ### Why Learn Java? 1. **Massive demand** - One of the most popular languages in the world 2. **Great for beginners** - Clear syntax, easy to read 3. **Career opportunities** - Huge job market, good salaries 4. **Versatile** - Build anything from apps to games to enterprise systems 5. **Strong foundation** - Learn OOP concepts that apply to other languages ### Java vs JavaScript Despite the similar name, Java and JavaScript are **completely different languages**: | Feature | Java | JavaScript | |---------|------|------------| | Type System | Static (types declared) | Dynamic (types figured out automatically) | | Compilation | Compiled to bytecode | Interpreted (runs in browser) | | Primary Use | Android, Enterprise, Desktop | Web development | | Syntax | C-like | C-like | | Created By | Sun Microsystems | Netscape | --- ## Setting Up Your Environment ### Option 1: Install Java JDK (Recommended) The Java Development Kit (JDK) includes everything you need to write and run Java: 1. **Download JDK** from [Oracle](https://www.oracle.com/java/technologies/downloads/) or use [Eclipse Temurin](https://adoptium.net/) 2. Install it on your computer 3. **Verify installation** - Open terminal/command prompt and type: ``` java -version ``` ### Option 2: Use an Online Compiler (Quickest Start) If you want to try Java immediately without installing anything: 1. Go to [Programiz](https://www.programiz.com/java-programming/online-compiler/) or [Replit](https://replit.com/) 2. Start coding right in your browser! ### Option 3: Use an IDE (Best for Learning) Integrated Development Environments make coding easier: - **VS Code** - Free, lightweight, popular - Download from [code.visualstudio.com](https://code.visualstudio.com/) - Install "Extension Pack for Java" - **IntelliJ IDEA** - Powerful, industry standard - Community Edition is free - **Eclipse** - Free, enterprise-ready --- ## Your First Java Program Every programmer starts with "Hello, World!" - it's a tradition! ### The Basic Structure ```java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` ### Let's Break It Down | Part | What It Means | |------|---------------| | `public class Main` | This is a class (blueprint) named Main | | `public static void main(String[] args)` | This is the main method - where your program starts | | `System.out.println()` | This prints text to the screen | | `;` | Every statement ends with a semicolon | ### How to Run It **Using Command Line:** 1. Save the file as `Main.java` 2. Open terminal in that folder 3. Compile: `javac Main.java` 4. Run: `java Main` **Using VS Code:** 1. Install Java extensions 2. Create a new Java file 3. Click the "Run" button --- ## Understanding Variables Variables are like labeled boxes that store data. You give them names so you can use them later. ### Declaring Variables ```java // Create a box called "age" that holds a number int age; // Create a box called "name" that holds text String name; // Create and fill a box at the same time int score = 100; String greeting = "Hello!"; ``` ### Variable Naming Rules - Start with a letter, underscore, or dollar sign - Can contain letters, numbers, underscores - **Case sensitive** - `myVar` and `myvar` are different - Use descriptive names: `int studentAge;` not `int x;` ### Best Practices ```java // Good variable names String firstName; int numberOfStudents; double accountBalance; boolean isLoggedIn; // Avoid String x; int n; double d; ``` --- ## Data Types in Java Java has two main categories of types: ### Primitive Types (Simple Values) | Type | What It Stores | Example | |------|----------------|---------| | `int` | Whole numbers | 42, -7, 0 | | `double` | Decimal numbers | 3.14, -99.9 | | `boolean` | True or false | true, false | | `char` | Single character | 'A', '!', '3' | | `long` | Big whole numbers | 1000000000L | | `float` | Decimal numbers (less precise) | 3.14f | ### Reference Types (Objects) | Type | What It Stores | Example | |------|----------------|---------| | `String` | Text | "Hello, World!" | | `Arrays` | Lists of items | {1, 2, 3} | | `Objects` | Custom data | new Person() | ### Examples ```java // Primitive types int count = 10; double price = 19.99; boolean isActive = true; char grade = 'A'; // Reference types String message = "Welcome to Java!"; String[] names = {"Alice", "Bob", "Charlie"}; ``` --- ## Operators Operators let you perform operations on values. ### Arithmetic Operators ```java int a = 10; int b = 3; System.out.println(a + b); // Addition: 13 System.out.println(a - b); // Subtraction: 7 System.out.println(a * b); // Multiplication: 30 System.out.println(a / b); // Division: 3 (integer division) System.out.println(a % b); // Modulus (remainder): 1 ``` ### Comparison Operators (Return true or false) ```java int x = 5; int y = 10; System.out.println(x == y); // Equal to: false System.out.println(x != y); // Not equal to: true System.out.println(x > y); // Greater than: false System.out.println(x < y); // Less than: true System.out.println(x >= y); // Greater or equal: false System.out.println(x <= y); // Less or equal: true ``` ### Logical Operators ```java boolean a = true; boolean b = false; System.out.println(a && b); // AND: false (both must be true) System.out.println(a || b); // OR: true (at least one must be true) System.out.println(!a); // NOT: false (reverses the value) ``` ### Assignment Operators ```java int num = 10; num += 5; // num = num + 5 → num is now 15 num -= 3; // num = num - 3 → num is now 12 num *= 2; // num = num * 2 → num is now 24 num /= 4; // num = num / 4 → num is now 6 ``` --- ## Type Conversion Sometimes you need to convert from one type to another. ### Implicit Conversion (Automatic) Java automatically converts when there's no data loss: ```java int num = 100; double converted = num; // int → double automatically System.out.println(converted); // 100.0 ``` ### Explicit Conversion (Casting) You manually convert when there might be data loss: ```java double price = 19.99; int roundedPrice = (int) price; // Force convert to int System.out.println(roundedPrice); // 19 (decimal is cut off!) ``` ### String Conversion ```java // Number to String int num = 42; String str = Integer.toString(num); // or String str2 = "" + num; // String to Number String text = "100"; int num2 = Integer.parseInt(text); // or double num3 = Double.parseDouble(text); ``` --- ## Comments and Code Organization Comments help you and others understand your code. Java ignores comments when running. ### Single-Line Comments ```java // This is a comment - Java ignores it int age = 25; // This explains the age variable ``` ### Multi-Line Comments ```java /* This is a multi-line comment. You can write as much as you want here. Java will ignore all of it. */ ``` ### Javadoc Comments (For Documentation) ```java /** * This method calculates the total price. * @param price The base price * @param tax The tax rate * @return The total price including tax */ public double calculateTotal(double price, double tax) { return price + (price * tax); } ``` --- ## Practice Exercises ### Exercise 1: Hello World Create a program that prints your name. ```java public class Main { public static void main(String[] args) { System.out.println("Your Name Here"); } } ``` ### Exercise 2: Variables Create variables for your name, age, and favorite number. Print them all. ```java public class Exercise2 { public static void main(String[] args) { String name = "Your Name"; int age = 20; double favoriteNumber = 7.5; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Favorite Number: " + favoriteNumber); } } ``` ### Exercise 3: Simple Calculator Create a program that performs basic math operations. ```java public class Calculator { public static void main(String[] args) { int a = 15; int b = 4; System.out.println("a + b = " + (a + b)); System.out.println("a - b = " + (a - b)); System.out.println("a * b = " + (a * b)); System.out.println("a / b = " + (a / b)); System.out.println("a % b = " + (a % b)); } } ``` ### Exercise 4: Type Conversion Practice converting between types. ```javapublic class TypeConversion { public static void main(String[] args) { double pi = 3.14159; int piInt = (int) pi; System.out.println("Original: " + pi); System.out.println("Converted to int: " + piInt); String numStr = "123"; int parsedNum = Integer.parseInt(numStr); System.out.println("Parsed string to int: " + parsedNum); } } ``` --- ## Summary In this guide, you learned: - ✅ What Java is and why it's worth learning - ✅ How to set up your Java environment - ✅ How to write and run your first Java program - ✅ How to create and use variables - ✅ The different data types in Java - ✅ How to use operators for math and logic - ✅ How to convert between types - ✅ How to write helpful comments **Next Steps:** Move on to [Java Beginner Part 2](./Java-Beginner-Part2.md) to learn about control flow and decision making! --- *Happy Coding! 🚀*