/Java-Jet

This repository contains simple Java concepts that I learnt during the course:mag_right: :mag_right:

Primary LanguageJava

GitHub last commit Github coverage Java

alt text

Java

Java is one of the most popular and widely used programming language and platform. It is fast, reliable and secure. Also java is object-oriented programming language. Java is statically type language (because the type of data is predefined as part of the programming language and all constants or variables are defined for a given program).

Data Types:

They are basically of two types:

  1. Primitive data type
  2. Object data type

🔉Primitive consists of boolean, byte, char, short, int, long, float, double. Whereas Object consists of programmer created types.

Variables in Java:

A variable is the name given to a memory location. It is the basic unit of storage in a program. For declaring a variable it includes data type, variable name and value. For Eg. int cost = 20; Here int is data type ,cost is variable name and 20 is value.

Java Identifiers:

In Java, an identifier can be a class name, method name, variable name or a label. There are certain rules to define java identifiers:

  1. Only alphanumeric characters are allowed as identifiers i.e. [A-Z], [a-z], [0-9] and '$', '_'.

  2. Identifiers should not start with digit[0-9].

  3. Special characters like @ and & are also not allowed.

  4. Reserved words can't be used.

Here is the first java program-Hello World

Basic Syntax in Java:

About Java programs, there are some very important rules to keep in mind they are as follows:

Case Sensitivity

Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.

Class Names

For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.

Example: class MyFirstJavaClass

Method Names

All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.

Example: public void myMethodName()

Program File Name

Name of the program file should exactly match the class name.

🔓When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile).

🔓But please make a note that in case you do not have a public class present in the file then file name can be different than class name. It is also not mandatory to have a public class in the file.

Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'

public static void main(String args[])

Java program processing starts from the main() method which is a mandatory part of every Java program.

OOP Concept:

alt text

Java is an object oriented programming language. The main aim of OOP is to bind together the data and the functions that operate so that no other part of the code can access this data except that function.

OOPs Concept INCLUDES:
🔉1)Polymorphism- Way to differentiate between entities with the same name efficiently.

For example:here

🔉2) Inheritance- Mechanism by which one class can inherit the features of another class.

🔉3) Object- It the most basic unit of OOPs. An object consists of :

  • State: It is represented by attributes of an object. It also reflects the properties of an object.

  • Behaviour : It is represented by methods of an object. It also reflects the response of an object with other objects.

  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

A simple program to explain the concept of object can be found here.

🔉4)Classes-Class is a user defined blueprint or prototype from which objects are created. It shows the set of properties that are common to all objects of same type. In general, class declarations includes:

  • Modifiers : A class can be public or has default access.

  • Class Name: The name should begin with a letter (capitalized by convention).

  • Super Class: The name of the class’s parent (superclass),preceded by the keyword extends.

  • Body: The class body surrounded by braces, { }.

A simple program to explain the concept of classes can be found here.

⭐⭐NOTE: When class is defined, only the specification for the object is defined; no memory or storage is allocated. To access members defined within the class, you need to create objects.⭐ ⭐

Example of Class and Object together is here.

Object Cloning in Java

Is cloning possible in real life ?? 😂😂

alt text

Cloning basically means creating a exact copy of an object. The clone() method of Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. Else clone() method generates CloneNotSupportedException.

Syntax of the clone() method: java protected Object clone() throws CloneNotSupportedException

The clone() method is defined in the Object class.

Why do we need this clone() method??

This clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of time that is why we use object cloning. Example of clone() method is here.

Java.lang.StringBuffer.appendCodePoint() Method {RARELY USED}

This method is used to append the string representation of the codepoint argument. The syntax followed is: public StringBuffer appendCodePoint(int cp).

Parameter:

codePoint − This is a Unicode code point.

Return Value:

The method returns this object after appending the string represented by the codepoint.

Arrays

An array is a type of data structure wherein all elements are of the same data type. Once defined, the size of an array is fixed and cannot increase to accommodate more elements. The first element of an array starts with index zero. Or simply can be defined as a programming construct to define certain categories. It is present in java.util package. There are three-steps involved in defining an array:

  • Declaring an array

Syntax: <elementType> <arrayName>[];

  • Constructing an Array

Syntax: arrayname = new dataType[]

  • Initialize the Array

Syntax: Eg: intArray[0]=n; // Assigns an integer value (n=any number) to the first element 0 of the array

🔍To declare an array, define the variable type with square brackets.

While working in array we may need to access the terms that are stored in that an array.We do that by referring to the index number. Arrays can store data in form of primitive data type or in form of an objects of a class. If it stores as primitive data type then data is stored at memory location or else as a heap segment.

There are two types of array we learn about:

1️⃣ One-Dimensionnal Array

2️⃣ Multi-Dimensional Array

So lets discuss in detail;

1️⃣One-dimensional array declaration has two components: the type and the name.

type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like array of int type, we can also create have an array of primitive data types like char, float, double..etc or user defined data type(objects of a class). Thus, the element type for the array determines what type of data the array will hold.

Array is basically declared as:

➡️ type var-name[];

OR

➡️ type[] var-name;

Now moving on to the next step that is intializing an array.It basically refers to assigning a initial condition/value to the array. Syntax:

variable-name = new type [size];

⚠️ type specifies

The type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate.

Example:

int intArray[]; //declaring array

intArray = new int[n]; // allocating memory to array

where n=any whole number

  • The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types). It is a default value storage.

  • Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable.

❗Our above example can we written as this also:

int[] intArray = new int[20]; // combining both statements as one statement.

Array Literals

This type is used when we already know about the array like variables and size of the array.

🔍 There is no need to write the new int[] part.

Accessing Array elements

Once we have an array declared and initialised we can access any element stored in that array by inputing the location of that element. Example we create an array called students and we stored the list of all students with their roll no. then we can access any student name by giving his roll no. and transversing in the array as student[12]
//Let the boy named Deepak is having the roll no.12, so the output will be "Deepak".

Arrays of Objects

Lets at first consider an example .

❓❓Would you like to guess the output❓❓. Just like general arrays the difference with the array of objects is that it stores objects in the array whereas the general array stores integers, variables, boolean etc. Also the array elements store the location of reference variables of the object.

Syntax:

Class obj[]= new Class[array_length]

So now lets look towards the output of the ⏫example. The output will be:

Element at 1 : 2 Vaibhav
Element at 2 : 3 Gargi
Element at 3 : 4 Sunil
Element at 4 : 5 Shikhar

101: Symmetric tree

class Solution {
    public boolean isSymmetric(TreeNode root) {
	if (root == null)
		return true;
	return isSymmetric(root.left, root.right);
}
 
public boolean isSymmetric(TreeNode l, TreeNode r) {
	if (l == null && r == null) {
		return true;
	} else if (r == null || l == null) {
		return false;
	}
 
	if (l.val != r.val)
		return false;
 
	if (!isSymmetric(l.left, r.right))
		return false;
	if (!isSymmetric(l.right, r.left))
		return false;
 
	return true;
}
}

Validate Binary Search Tree

class Solution {
public boolean isValidBST(TreeNode root) {
    return helper(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
 
public boolean helper(TreeNode root, double min, double max){
    if(root==null){
        return true;
    }
 
    if(root.val<=min||root.val>=max){
        return false;
    }
 
    boolean isLeftBST = helper(root.left, min, root.val);
    boolean isRightBST = helper(root.right, root.val, max);
 
    if(!isLeftBST||!isRightBST){
        return false;
    }    
 
    return true;
}
}

Delete Node in a BST

     public TreeNode deleteNode(TreeNode root, int key) {
         if(root == null){
             return root;
         }
         if(key < root.val){
             root.left = deleteNode(root.left, key);
         }else if(key > root.val){
             root.right = deleteNode(root.right, key);
         }else{
             if(root.left == null){
                 return root.right;
             }else if(root.right == null){
                 return root.left;
             }else{
                 TreeNode suc = findSuc(root.right);
                 root.val =suc.val;
                 root.right = deleteNode(root.right, suc.val);
             }
         }
         return root;
     }
     
     private TreeNode findSuc(TreeNode root){
       while(root.left != null){
    root = root.left;         }         return root;     }
} 

Insert into a Binary Search Tree

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) { return new TreeNode(val); }
        TreeNode p = null;
        TreeNode r = root;
        while (r != null) {
            p = r;
            if (r.val > val) { 
                r = r.left; 
            } else if (r.val < val) { 
                r = r.right; 
            }
        }
        TreeNode n = new TreeNode(val);
        if (p.val > val) { p.left = n; }
        if (p.val < val) { p.right = n; }
        return root;
    }
}

Same tree

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
    if(p==null && q==null)
        return true;
    else if(p==null || q==null)
        return false;
    if(p.val==q.val)
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    else
        return false;
        }
}

Maximum Depth of Binary Tree

    public int maxDepth(TreeNode root) {
        if(root == null)
            return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
 
        int bigger = Math.max(leftDepth, rightDepth);
        return bigger+1;
}
    }

Trim a binary search tree

class Solution {
    public TreeNode trimBST(TreeNode root, int L, int R) {
        if(root == null) 
            return root; 
        if(root.val < L) { 
            return trimBST(root.right, L, R); 
        }
        if(root.val > R) { 
            return trimBST(root.left, L, R); 
        }
        root.left = trimBST(root.left, L, R);
        root.right = trimBST(root.right, L, R);
        return root;
    }
}