Java - Notes


Introduction of Java

Java components

Execution Scenario of Java

Keywords

Keywords are reserved words with predefined meanings in the compiler. There are 50 keywords in Java, such as:

Class, Package, Variables

public static void main(String[] args) Explained

System.out.println(" "); Explained


16 August, 2024

Operators

Operators are symbols which are used to perform operation on operands.


Decision Controls

Decision Controls are used for decision making.If you want to execute code based on some condition then you can use decision control. In java there are following types of decision control :

  1. if Statement : if is a keyword which works as decision control. We attach a condition with if statement. If given condtiion is true then code will be executed and if given condition is false then code will not be executed.
  2.                         Syntax : 
    
                            if (Condition) {
                                //code
                            }
                        
  3. if-else Statement : if else is variation of if statement. We attact a condition with if statement. if given condition is true then if block code will executed and if given condition is false then else block code will be executed.
  4.                         Syntax : 
                            
                            if (Condition) {
                                // if block code
                            }
                            else {
                                // else block code
                            }
                        

    17 August, 2024

  5. ladder if-else Statement :
  6. Switch Statement :

Loop Control

  1. while Statement :
  2. 
                

    18 August, 2024

  3. for Statement :
  4. 
                
  5. do while loop :
  6. 
                
  7. for each loop :
  8. 
                

19 August, 2024

Array

Array is collection of simillar data types that means an array can store mutiple values of similar data types.

                Delcaration of Array :

                datatype [] arrayname = new datatype[size];

                E.g. : int [] arr = new int[10];

                The above array can store ten numbers of integer types.

                Initialization of Array :

                int [] arr = {10, 20, 30, 40, 50};

                arr[0] = 10;
                arr[1] = 20;
                arr[2] = 30;
                arr[3] = 40;
                arr[4] = 50;

                
Memory Representation of Array
arr[0] arr[1] arr[2] arr[3] arr[4]
10 20 30 40 50

How to take input from user for an array?

                int [] arr = int[5];
                int i;
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter five numbers");

                for(i=0; i<5; i++) {
                    arr[i] = sc.nextInt();
                }

            

20 August, 2024

String in Java

Technically String is a sequence of characters. In Java String is a class. The object of String class stores sequence of characters.

String name = "Hardik Srivastava";

Methods of String Class

  1. toUpperCase() : Converts String into uppercase.
  2. toLowerCase() : Converts String into lowercase.
  3. length() : Find length of String.
  4. equals() : Compare two Strings for equality. This method return boolean value.
  5. equalsignoreCase() : This method also compare two Strings for equality, but this method avoid case sensitivity.
  6. split() : This method is used to split String into sub-strings and return array of string.
                        String sen = "He is a good boy.";
                        String[] words = sen.spilt(" ");
                        words[0] = "He";
                        words[1] = "is";
                        words[2] = "a";
                        words[3] = "good";
                        words[4] = "boy.";
                    
  7. replace() : This method is used to replace on string with another string in given string.
  8. CharAt() : This method used to find characters at specified location.

21 August, 2024

Methods

Method is a named block of code which perform specific task.

Why Method?

If you have a block of code which is required different locations of program, then you can create a method of that code and call it from desired location.

How to Create Method in Java?

                modifier returntype methodname(parameters) {
                    // code
                }

                E.g.

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

Types of Methods in Java :

  1. Static Method : Static Methods are those methods which are created by using 'static' modifier. These methods are called class methods. There is no need of object to call static method. eg. Arrays.sort();
  2. Non-Static Method : Non-Static methods are created without using Static keyword. Non-Static methods can call by using object.

Note : You can create userdefined method inside class and outside of main method.

22 August, 2024

OOPS

OOPS stand for object oriented Programming System. It is a mechanism of software Development. The Object Oriented Programming System has four pillars.

  1. Abstraction : Abstraction is a mechanism to show only essential functionalities and hide all other functionalities of an object.
  2. Encapsulation : Encapsulation is a mechanism to warp properties and functionalities in a single unit, that single unit is called object
  3. Inheritance : Inheritance is a feature of object oriented programming. In Inheritance you can create a new product by using existing product.
  4. Polymorphism : Thw term Polymorphism means one thing many forms.

Any Programming language which follows these four concept is known as object oriented programming language.

  1. Class : Class is a blueprint of object. Class is collection of variables and methods. Class is created by using class keyword followed by classname. and the body of class is enclosed within braces.
  2. class clasname {
        // variables and methods
    }