0% found this document useful (0 votes)
6 views

Java

Beginning Learning Java

Uploaded by

MARK SIMIYU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java

Beginning Learning Java

Uploaded by

MARK SIMIYU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here’s an overview of the Fundamentals of Java:

1. Introduction to Java

• Java is a high-level, object-oriented, platform-independent programming language.


• It is widely used for developing web, mobile, and desktop applications.
• Known for its "Write Once, Run Anywhere" feature due to the Java Virtual Machine
(JVM).

2. Basic Syntax

• Java programs consist of classes and a main method.

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!"); // Prints output
}
}

3. Data Types

Java has two types of data:

• Primitive Data Types: int, double, char, boolean, etc.


• Non-Primitive Data Types: Objects, arrays, strings.

int age = 25; // Integer


double pi = 3.14; // Decimal
char grade = 'A'; // Character
boolean isJavaFun = true; // Boolean
String name = "Isaac"; // String

4. Variables

• Variables must be declared with a type before use.


• Java supports final (constants) and static variables.

int x = 5; // Variable
final int MAX = 100; // Constant
5. Control Flow

• Conditionals: if, else if, else, switch


• Loops: for, while, do-while

// If-Else
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}

// For Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}

6. Arrays

• Arrays store multiple values of the same type.

int[] numbers = {1, 2, 3, 4, 5};


System.out.println(numbers[0]); // Output: 1

7. Functions (Methods)

• Functions are defined inside classes and can be static or instance methods.

public class Calculator {


public int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


Calculator calc = new Calculator();
System.out.println(calc.add(5, 3)); // Output: 8
}
}

8. Object-Oriented Programming

• Core concepts: Class, Object, Inheritance, Polymorphism, Encapsulation,


Abstraction.

class Animal {
String name;

void eat() {
System.out.println(name + " is eating.");
}
}

public class Main {


public static void main(String[] args) {
Animal dog = new Animal();
dog.name = "Buddy";
dog.eat(); // Output: Buddy is eating.
}
}

9. Exception Handling

• Handle runtime errors using try-catch-finally.

try {
int result = 10 / 0; // Will throw an exception
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("Execution complete.");
}

10. Packages and Libraries

• Java organizes classes into packages.


• Example: java.util, java.io.

import java.util.Scanner;

public class InputExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
}
}

11. File Handling

• Use File and FileWriter classes.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileExample {


public static void main(String[] args) {
try {
File file = new File("example.txt");
FileWriter writer = new FileWriter(file);
writer.write("Hello, Java!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Would you like to dive deeper into any specific topic?

You might also like