java Basics

java is object oriented

basic program in java

we convert javacode to bite code with javac (javacompiler) it will create main.class (it is bite code) java follows unicode principals java is platform independent

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

Function

Block of code, collection of code that we can use again and again

The main function in Java is the entry point of any Java program. It is the method that the Java Virtual Machine (JVM) calls to start the execution of a program. Without the main method, the program will not run.

Public means this class is access everywhere class name grp of prop and funs Main name of file name of program and public class is same
main entry point of java prog , most impt , prog will not run without it void is a keyword used as a return type for methods that do not return any value. static is a keyword used to indicate that a field, method, or block belongs to the class rather than an instance of the class. It is shared among all objects of the class.

public class Main{
	public static void main(string[] args){
		System.out.println("Hello World!");
	}
}

Class

  • Class: HelloWorld is the class name. In Java, everythingbelongs to a class. first letter is always capital (of class)
  • a class is a blueprint or template for creating objects
  • defines the properties (fields) and behaviors (methods) that the objects created from the class will have

in intellij shortcut for public static void main is psvm sout shortcut for System.out.println();

Method

  • Method: public static void main(String[] args) is the main method. This is where the program starts running.
  • System.out.println: This prints output to the console.

Variables

int age = 25;
double price = 19.99;
char grade = 'A';
boolean isJavaFun = true;

control statements

// if-else
int x = 10;
if (x > 5) {
    System.out.println("x is greater than 5");
} else {
    System.out.println("x is 5 or less");
}

Loops

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

Methods in JAVA

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

    public static void main(String[] args) {
        int result = add(5, 3);
        System.out.println("Sum: " + result);
    }
}
  • Return Type: The type of data the method returns (int in this case).
  • Parameters: Values passed into the method (int a, int b).

[[2. oops in java]]

Classes and Objects: Classes are blueprints, and objects are instances.

class Dog {
    String breed;
    int age;

    void bark() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.breed = "Labrador";
        myDog.age = 3;
        myDog.bark();
    }
}

Inheritance: One class can inherit properties from another class.

class Animal {
    void eat() {
        System.out.println("This animal eats.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Woof!");
    }
}

[[Access Modifiers in Java]]

Java provides four types of access modifiers:

  • public: Accessible from anywhere.
  • private: Accessible only within the class.
  • protected: Accessible within the package and subclasses.
  • No modifier (default): Accessible within the same package.

Exception Handling in Java

Java provides a robust mechanism for handling errors using try, catch, and finally.

try {
    int result = 10 / 0;  // This will cause an error
} catch (ArithmeticException e) {
    System.out.println("Can't divide by zero!");
} finally {
    System.out.println("This block always executes.");
}

Output in java

output refers to displaying data or results to the user, typically using methods like System.out.print() or System.out.println().

  • System.out.print(): Prints text on the same line.
  • System.out.println(): Prints text and moves to the next line.
  • System.out.printf(): Formats output.
public class OutputExample {
    public static void main(String[] args) {
        System.out.print("Hello ");         // Output: Hello (on the same line)
        System.out.println("World!");      // Output: World! (on a new line)
        System.out.printf("Age: %d", 25);  // Output: Age: 25
    }
}

Input in Java

we use Scanner Class to take input from us

In Java, input refers to reading data from the user, typically using the Scanner class.

Steps for Input:
  1. Import the Scanner class: import java.util.Scanner;
  2. Create a Scanner object: Scanner sc = new Scanner(System.in);
  3. Use methods like nextInt(), nextLine(), etc., to read input.
import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine(); // Reads a string

        System.out.print("Enter your age: ");
        int age = sc.nextInt(); // Reads an integer

        System.out.println("Name: " + name + ", Age: " + age);
    }
}

[[primitive]]

cannot break one datatype into another

primitive types are the most basic data types that represent simple values. They are not objects and are predefined by the language. Each primitive type has a fixed size and value range.

comments in java

//

type casting

compressing larger value to the smaller Typecasting in Java is the process of converting one data type to another. There are two types of typecasting:

  1. Implicit (Automatic) Typecasting: Happens automatically when converting from a smaller to a larger data type (e.g., int to long).

    Example:

    int num = 10;
    long bigNum = num;  // Implicit casting (int to long)
    
  2. Explicit (Manual) Typecasting: Required when converting from a larger to a smaller data type, and it is done manually by the programmer (e.g., double to int).

    Example:

    double price = 99.99;
    int roundedPrice = (int) price;  // Explicit casting (double to int)
    
Key Points:
  • Implicit casting occurs automatically when no data loss is expected.
  • Explicit casting may lose data or precision, so it requires manual intervention.

nice