-
-
Notifications
You must be signed in to change notification settings - Fork 2
Java Beginner Guide
- Introduction to Java
- Setting Up Your Environment
- Your First Java Program
- Understanding Variables
- Data Types in Java
- Operators
- Type Conversion
- Comments and Code Organization
- Practice Exercises
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!
- Massive demand - One of the most popular languages in the world
- Great for beginners - Clear syntax, easy to read
- Career opportunities - Huge job market, good salaries
- Versatile - Build anything from apps to games to enterprise systems
- Strong foundation - Learn OOP concepts that apply to other languages
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 |
The Java Development Kit (JDK) includes everything you need to write and run Java:
- Download JDK from Oracle or use Eclipse Temurin
- Install it on your computer
-
Verify installation - Open terminal/command prompt and type:
java -version
If you want to try Java immediately without installing anything:
Integrated Development Environments make coding easier:
-
VS Code - Free, lightweight, popular
- Download from code.visualstudio.com
- Install "Extension Pack for Java"
-
IntelliJ IDEA - Powerful, industry standard
- Community Edition is free
-
Eclipse - Free, enterprise-ready
Every programmer starts with "Hello, World!" - it's a tradition!
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}| 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 |
Using Command Line:
- Save the file as
Main.java - Open terminal in that folder
- Compile:
javac Main.java - Run:
java Main
Using VS Code:
- Install Java extensions
- Create a new Java file
- Click the "Run" button
Variables are like labeled boxes that store data. You give them names so you can use them later.
// 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!";- Start with a letter, underscore, or dollar sign
- Can contain letters, numbers, underscores
-
Case sensitive -
myVarandmyvarare different - Use descriptive names:
int studentAge;notint x;
// Good variable names
String firstName;
int numberOfStudents;
double accountBalance;
boolean isLoggedIn;
// Avoid
String x;
int n;
double d;Java has two main categories of types:
| 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 |
| Type | What It Stores | Example |
|---|---|---|
String |
Text | "Hello, World!" |
Arrays |
Lists of items | {1, 2, 3} |
Objects |
Custom data | new Person() |
// 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 let you perform operations on values.
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): 1int 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: trueboolean 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)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 6Sometimes you need to convert from one type to another.
Java automatically converts when there's no data loss:
int num = 100;
double converted = num; // int → double automatically
System.out.println(converted); // 100.0You manually convert when there might be data loss:
double price = 19.99;
int roundedPrice = (int) price; // Force convert to int
System.out.println(roundedPrice); // 19 (decimal is cut off!)// 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 help you and others understand your code. Java ignores comments when running.
// This is a comment - Java ignores it
int age = 25; // This explains the age variable/* This is a multi-line comment.
You can write as much as you want here.
Java will ignore all of it. *//**
* 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);
}Create a program that prints your name.
public class Main {
public static void main(String[] args) {
System.out.println("Your Name Here");
}
}Create variables for your name, age, and favorite number. Print them all.
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);
}
}Create a program that performs basic math operations.
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));
}
}Practice converting between types.
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);
}
}
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 to learn about control flow and decision making!
Happy Coding! 🚀