0% found this document useful (0 votes)
6 views5 pages

Java Fundamentals

Uploaded by

cjnamoco623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Java Fundamentals

Uploaded by

cjnamoco623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

☕ Java Programming Fundamentals

(Detailed Guide)

1. Introduction to Java
 What is Java?
o A high-level, object-oriented programming language.
o Compiled (into bytecode) → runs on the Java Virtual Machine (JVM).
o Famous motto: “Write once, run anywhere.”
 How Java Works:

1. You write code in .java file.


2. Compile:
3. javac Program.java

(creates Program.class bytecode file).

4. Run:
5. java Program

2. Basic Syntax
✅ Hello World
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}

Output:

Hello, Java!

🔑 Explanation:

 public class Hello → Every program starts with a class.


 main → entry point of program.
 System.out.println → prints text with a new line.
3. Variables & Data Types
✅ Declaring Variables
public class Variables {
public static void main(String[] args) {
String name = "Alice"; // Text
int age = 25; // Whole number
double height = 5.7; // Decimal
boolean isStudent = true; // True/False

System.out.println(name + " is " + age + " years old.");


}
}

Output:

Alice is 25 years old.

4. Operators
✅ Arithmetic
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1

✅ Comparison
System.out.println(10 > 5); // true
System.out.println(10 == 5); // false

✅ Logical
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
System.out.println(!x); // false

5. Control Flow
✅ If-Else
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}

✅ For Loop
for (int i = 0; i < 5; i++) {
System.out.println("Number: " + i);
}

✅ While Loop
int count = 1;
while (count <= 3) {
System.out.println("Count: " + count);
count++;
}

6. Data Structures
✅ Arrays
String[] fruits = {"apple", "banana", "cherry"};
System.out.println(fruits[1]); // banana

✅ ArrayList (dynamic array)


import java.util.ArrayList;

public class Lists {


public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

System.out.println(names.get(0)); // Alice
System.out.println(names.size()); // 3
}
}

✅ HashMap (dictionary in Java)


import java.util.HashMap;

public class Maps {


public static void main(String[] args) {
HashMap<String, String> capitals = new HashMap<>();
capitals.put("Japan", "Tokyo");
capitals.put("France", "Paris");

System.out.println(capitals.get("Japan")); // Tokyo
}
}

7. Methods (Functions in Java)


public class Functions {
public static void main(String[] args) {
greet("Bob");
}

static void greet(String name) {


System.out.println("Hello, " + name);
}
}

Output:

Hello, Bob

8. OOP (Object-Oriented Programming)


class Person {
String name;
int age;

// Constructor
Person(String n, int a) {
name = n;
age = a;
}

// Method
void greet() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years
old.");
}
}

public class Main {


public static void main(String[] args) {
Person p1 = new Person("Alice", 25);
p1.greet();
}
}

Output:
Hi, I'm Alice and I'm 25 years old.

9. Exception Handling
public class Errors {
public static void main(String[] args) {
try {
int num = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number");
}
}
}

Output:

Error: Invalid number

📝 Practice Exercises (Java Beginner Level)


1. Print your name, age, and favorite hobby in one line.
2. Write a program that takes two integers and prints their sum.
3. Create an array of 5 favorite foods and print the last one.
4. Write a method that checks if a number is even or odd.
5. Create a HashMap of 3 countries and their capitals, then print one capital.

You might also like