Java Programming

 

Java Programming Notes


1. Introduction to Java Programming

1.1 What is Java?
  • Java is a high-level, class-based, object-oriented programming language developed by James Gosling and Mike Sheridan at Sun Microsystems (now owned by Oracle).
  • Java is platform-independent, meaning it can run on any device or operating system that has the Java Runtime Environment (JRE) installed.
1.2 Structure of a Java Program

A basic Java program consists of:

  1. Class declaration: The class is the blueprint of the object.
  2. Main method: The entry point of a Java program, public static void main(String[] args).
  3. Statements inside the main method to perform tasks.
public class HelloWorld {
public static void main(String[] args) { System.out.println("Hello, World!"); } }

2. Java Data Types

2.1 Primitive Data Types

Java has eight primitive data types:

  • byte: 8-bit integer.
  • short: 16-bit integer.
  • int: 32-bit integer.
  • long: 64-bit integer.
  • float: 32-bit floating point.
  • double: 64-bit floating point.
  • char: 16-bit Unicode character.
  • boolean: Represents true or false.
byte b = 100;
short s = 200; int i = 300; long l = 100000L; float f = 3.14f; double d = 3.14159; char c = 'A'; boolean flag = true;
2.2 Reference Data Types
  • Arrays: A collection of elements of the same data type.
  • String: A sequence of characters.
  • Objects: Instances of classes.
String name = "John";
int[] arr = {1, 2, 3, 4};

3. Java Operators

3.1 Arithmetic Operators

These operators perform mathematical operations.

int sum = 5 + 3;
int diff = 5 - 3; int prod = 5 * 3; int quotient = 5 / 3; int remainder = 5 % 3;
3.2 Relational Operators

These operators compare two values.

5 == 3; // Equal to
5 != 3; // Not equal to 5 > 3; // Greater than 5 < 3; // Less than 5 >= 3; // Greater than or equal to 5 <= 3; // Less than or equal to
3.3 Logical Operators

These operators are used for logical operations.

true && false; // AND
true || false; // OR !true; // NOT
3.4 Assignment Operators

Used to assign values to variables.

int a = 5;
a += 3; // a = a + 3 a -= 2; // a = a - 2 a *= 4; // a = a * 4 a /= 2; // a = a / 2

4. Java Control Structures

4.1 If-Else Statement

The if-else statement is used to execute code conditionally.

int a = 5, b = 3;
if (a > b) { System.out.println("a is greater than b"); } else { System.out.println("a is less than or equal to b"); }
4.2 Switch-Case Statement

The switch-case statement allows you to choose from multiple options.

int day = 3;
switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day"); }
4.3 Loops
  • For loop: Used when the number of iterations is known.
for (int i = 0; i < 5; i++) {
System.out.println(i); }
  • While loop: Used when the number of iterations is unknown.
int i = 0;
while (i < 5) { System.out.println(i); i++; }
  • Do-While loop: Executes the loop body at least once before checking the condition.
int i = 0;
do { System.out.println(i); i++; } while (i < 5);

5. Methods in Java

5.1 Method Declaration

A method is a block of code that performs a specific task.

public class Calculator {
// Method to add two numbers public int add(int a, int b) { return a + b; } }
5.2 Calling a Method

You can call a method from within another method.

public class Main {
public static void main(String[] args) { Calculator calc = new Calculator(); int result = calc.add(5, 3); System.out.println("Result: " + result); } }

6. Arrays in Java

6.1 One-Dimensional Arrays

An array is a collection of elements of the same type.

int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[0]); // Output: 1
6.2 Multi-Dimensional Arrays

Multi-dimensional arrays are used to represent matrices.

int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[2][1]); // Output: 6

7. Object-Oriented Concepts in Java

7.1 Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.

public class Person {
String name; int age; void greet() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } } public class Main { public static void main(String[] args) { Person p1 = new Person(); p1.name = "John"; p1.age = 30; p1.greet(); } }
7.2 Constructors

A constructor is a special method used to initialize objects.

class Person {
String name; int age; // Constructor to initialize name and age Person(String name, int age) { this.name = name; this.age = age; } void greet() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } } public class Main { public static void main(String[] args) { Person p1 = new Person("John", 30); p1.greet(); } }
7.3 Inheritance

Inheritance allows a class to inherit properties and behaviors from another class.

class Animal {
void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); // Output: Dog barks } }

8. Exception Handling in Java

8.1 Try-Catch Block

Exception handling in Java is done using try-catch blocks.

try {
int result = 10 / 0; // This will cause an ArithmeticException } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); }

9. File Handling in Java

9.1 Reading and Writing Files

You can use FileReader and FileWriter to read and write to files in Java.

import java.io.*;
public class FileHandling { public static void main(String[] args) { try { // Writing to a file FileWriter writer = new FileWriter("output.txt"); writer.write("Hello, Java File Handling!"); writer.close(); // Reading from a file FileReader reader = new FileReader("output.txt"); int data; while ((data = reader.read()) != -1) { System.out.print((char) data); } reader.close(); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }

Assessment: Regular Tests, Assignments, and Final Project Evaluation

Certification: Certificate of Completion from Disha Institute

Number of Days Depends on your practice and feedback. The more you practice and review your work, the faster you will complete the course.

For Batch time and Fess contact to Below Address and Number.

Zamanat Sir       (MCA / Bsc. I.T / ‘A’ / ‘O’ / CCC / Govt. Certified  Domain Skill Trainer Disha Institute 153 Vijay Nagar Opp. Rg Pg College W.K Road Meerut     (9411617329 , 9458516690) 

Courses                Reviews &Location                  Download

For More Details such as Courses , Job updation , Notes , Videos ,Links  Click Here

Comments

Popular posts from this blog

VBA Modules for Excel Automation (Advance Excel)

Excel Shortcuts