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

Java Basics

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

Java Basics

JAVA Basics
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Here’s a more comprehensive guide on Java, covering key concepts, syntax, and examples to

provide a thorough understanding of the language.

1. Introduction to Java

 Java is a high-level, class-based, object-oriented programming language.


 Platform-independent: Java code runs on any device with a Java Virtual Machine
(JVM).
 Strongly typed and garbage-collected, with a focus on reliability and security.

2. Java Basics

Basic Structure of a Java Program

java
Copy code
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

 public class Main: Defines a class named Main.


 public static void main(String[] args): Main method, the entry point for the
program.
 System.out.println(): Prints to the console.

3. Data Types

Java supports primitive and non-primitive (reference) data types.

Type Description Size Example


byte 8-bit integer 1 byte byte b = 10;
short 16-bit integer 2 bytes short s = 20;
int 32-bit integer 4 bytes int i = 30;
long 64-bit integer 8 bytes long l = 40L;
float Single-precision 4 bytes float f = 5.5f;
double Double-precision 8 bytes double d = 10.5;
char Single character 2 bytes char c = 'A';
boolean True or false 1 bit boolean b = true;

 Non-Primitive Types: Arrays, Strings, Classes, Interfaces.


4. Variables and Operators

Variable Declaration

 Declaring variables with data types:

java
Copy code
int age = 25;
String name = "Alice";

Operators

 Arithmetic Operators: +, -, *, /, %
 Relational Operators: ==, !=, >, <, >=, <=
 Logical Operators: && (and), || (or), ! (not)
 Assignment Operators: =, +=, -=, *=, /=, %=
 Increment/Decrement: ++, --

5. Control Flow Statements

If-else Statements

java
Copy code
int num = 10;
if (num > 0) {
System.out.println("Positive");
} else {
System.out.println("Non-positive");
}

Switch Case

java
Copy code
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
break;
}

Loops
 For Loop:

java
Copy code
for (int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}

 While Loop:

java
Copy code
int i = 0;
while (i < 5) {
System.out.println("Count: " + i);
i++;
}

 Do-While Loop:

java
Copy code
int i = 0;
do {
System.out.println("Count: " + i);
i++;
} while (i < 5);

6. Methods and Functions

Defining a Method

java
Copy code
public static int add(int a, int b) {
return a + b;
}

 Method Call:

java
Copy code
int sum = add(5, 10);
System.out.println(sum);

Method Overloading

Multiple methods with the same name but different parameters.

java
Copy code
public int add(int a, int b) { return a + b; }
public double add(double a, double b) { return a + b; }
7. Object-Oriented Programming (OOP) Concepts

Classes and Objects

 Class: A blueprint for creating objects.


 Object: An instance of a class.

java
Copy code
public class Car {
String model;
int year;

public Car(String model, int year) {


this.model = model;
this.year = year;
}

public void displayInfo() {


System.out.println("Model: " + model + ", Year: " + year);
}
}

public class Main {


public static void main(String[] args) {
Car car = new Car("Tesla", 2022);
car.displayInfo();
}
}

Inheritance

Enables a new class (subclass) to inherit properties from an existing class (superclass).

java
Copy code
public class Animal {
public void eat() {
System.out.println("Eating...");
}
}

public class Dog extends Animal {


public void bark() {
System.out.println("Barking...");
}
}

Polymorphism

 Method Overriding: Subclass provides a specific implementation of a method in the


superclass.

java
Copy code
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("Dog eating...");
}
}

Encapsulation

Wrapping data and methods within classes, using private access modifier and getters and
setters methods.

java
Copy code
public class BankAccount {
private double balance;

public double getBalance() {


return balance;
}

public void setBalance(double balance) {


this.balance = balance;
}
}

Abstraction

Hiding implementation details using abstract classes or interfaces.

java
Copy code
public abstract class Shape {
public abstract void draw();
}

public class Circle extends Shape {


public void draw() {
System.out.println("Drawing a circle");
}
}

8. Exception Handling

Used to handle runtime errors to prevent crashing the program.

java
Copy code
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}
9. Java Collections Framework

Provides data structures like List, Set, and Map.

 List (ordered): ArrayList, LinkedList


 Set (unique): HashSet, TreeSet
 Map (key-value pairs): HashMap, TreeMap

java
Copy code
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

10. File Handling

Java offers File and Scanner classes for file manipulation.

java
Copy code
try {
File file = new File("example.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
}

11. Multithreading

Running multiple threads (concurrent execution) using the Thread class or Runnable
interface.

java
Copy code
public class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}

public class Main {


public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
This overview covers the core concepts and syntax in Java, from basics to advanced topics
like OOP, exception handling, collections, file handling, and multithreading. Practicing with
these examples and building simple projects will further solidify these concepts.

You might also like