Saturday, 15 August 2020

No of Occurrence in Array

package com.tutorial4u;

import java.util.HashMap;

/************************************************************

 No of Occurrence in Array

************************************************************/

public class NoOfOccurrenceInArray {

      public static void main(String[] args) {

            int [] numbers = new int[] {10,12,13,14,12,11,10,20,21,14,22};

            int count =0;

            HashMap<Integer, Integer> map = new HashMap<>();

            for(Integer i : numbers) {

                  if(map.containsKey(i)) {

                        count = map.get(i);

                        map.put(i, count+1);

                  }else {

                        map.put(i, 1);

                  }

            }

            System.out.println(map);

      }

}

 

 Output :

{20=1, 21=1, 22=1, 10=2, 11=1, 12=2, 13=1, 14=2}

Wednesday, 29 July 2020

JSON Tutorial

JSON stands for JavaScript Object Notation. JSON objects are used for transferring data between server and client, XML serves the same purpose. However JSON objects have several advantages over XML and we are going to discuss them in this tutorial along with JSON concepts and its usages

Inner Class

Introduction to Java

Java is a programming language developed by James Gosling and his team at Sun Microsystems (Sun) in 1991. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the Oak name to Java.Sun Microsystems was acquired by the Oracle Corporation in 2010.Now oracle is the steermanship for Java.

The target of Java is to write a program once and then run this program on multiple operating systems(like windows, Linux,Mac etc.)
Over time new enhanced versions of Java have been released. The current version of Java is Java 10, which was released on 20 March 2018.

The Java language was designed with the following properties:
  1. Simple
  2. Platform independent:
  3. Object-orientated programming language:
  4. Portable
  5. Robust
  6. Secured
  7. Multi threading
Simple : 

Java Language is easy to understand and its syntax is very simple.Java has removed the many complicated and rarely used features like pointers, operator overloading etc.
There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java.

Platform independent:

Object-orientated programming language :

Java is an object-oriented programming language because all elements in Java are object except the primitive data type.

4 main concepts of Object Oriented programming are:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Portable : Java is portable because it is written on one machine and the pendent byte code can be carried to any other machine. It doesn't require any implementation.

Robust :  Java is robust because Java programming language uses concept like Memory Management, Garbage Collection, Exception Handling.

Secured : In Java,  we don't have pointers that avoids the security.\

Multi-threading : Java supports multi-threading. Multi-threading is a Java feature that allows to programmer to write a programs that deal with two or more task run at a time. It doesn't occupy memory for each thread. It shares a common memory area.




Tuesday, 28 July 2020

Exception handling in Method Overriding

We can override a method by changing only the exception handling in java.

An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not. 
However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method.

Case 1: If  superclass doesn’t throw any exception but child class throws an unchecked exception.

package com.tutorial4u;

class A {

       void m1() {

             System.out.println("method A");

        }

}

class B extends A {

       void m1() throws NullPointerException {

             System.out.println("Method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

}

Output : Method B

Case 2 :  If  superclass doesn’t throw any exception but child class throws an checked exception

package com.tutorial4u;

import java.io.IOException;

class A {

       void m1() {

             System.out.println("method A");

       }

}

class B extends A {

       // Exception IOException is not compatible with throws clause in A.m1()

       void m1() throws IOException {

             System.out.println("Method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

}

Output : Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception IOException is not compatible with throws clause in A.m1()

Note : We cannot throw a checked exception if the overridden method(method of base class) is not throwing an exception.

Case 3 : If superclass throws checked exception but subclass throws narrower (subclass of) checked exception

package com.tutorial4u;

import java.io.FileNotFoundException;

import java.io.IOException;

class A {

       void m1() throws IOException {

             System.out.println("method A");

       }

}

class B extends A {

       void m1() throws FileNotFoundException {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) throws IOException {

             A a = new B();

             a.m1();

       }

}

Output : method B

Case 4 : If superclass throws checked exception but subclass throws broader (superclass of ) checked exception.

package com.tutorial4u;

import java.io.IOException;

class A {

       void m1() throws IOException {

             System.out.println("method A");

       }

}

class B extends A {

       // Exception Exception is not compatible with throws clause in A.m1()

       void m1() throws Exception {

             System.out.println("Method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             try {

                    a.m1();

             } catch (Exception e) {

                    e.printStackTrace();

             }

       }

}

Output : 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception Exception is not compatible with throws clause in A.m1()

Note : Getting compilation error because the m1() method of child class is throwing Exception which has a broader scope than the exception thrown by method m1() of parent class.

But if child class method have child exception of the super class declared exception or same  or not throwing any exception. It will execute fine.

Case 5 : If superclass throws checked exception but subclass throws an unchecked Exception (RuntimeException)

package com.tutorial4u;

import java.io.IOException;

class A {

       void m1() throws IOException {

             System.out.println("method A");

       }

}

class B extends A {

       // Exception Exception is not compatible with throws clause in A.m1()

       void m1() throws ArithmeticException {

             System.out.println("Method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             try {

                    a.m1();

             } catch (Exception e) {

                    e.printStackTrace();

             }

       }

}

Output : Method B


Case 6 : If superclass throws unchecked Exception but subclass throw a checked exception.


package com.tutorial4u;

import java.io.IOException; 

class A {

       void m1() throws ArithmeticException {

             System.out.println("method A");

       }

}

class B extends A {

       // Exception IOException is not compatible with throws clause in A.m1()

       void m1() throws IOException{

             System.out.println("Method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             try {

                    a.m1();

             } catch (Exception e) {

                    e.printStackTrace();

             }

       }

}

Output : 

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Exception IOException is not compatible with throws clause in A.m1()

Case 7: If superclass and subclass both throw an unchecked Exception (RuntimeException)

package com.tutorial4u;

class A {

       void m1() throws ArithmeticException {

             System.out.println("method A");

       }

}

class B extends A {

       void m1() throws NullPointerException {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

}

Output : method B


Case 8 : If superclass and  subclass both throws a checked exception

package com.tutorial4u; 

import java.io.IOException;

class A {

       void m1() throws IOException {

             System.out.println("method A");

       }

}

class B extends A {

       void m1() throws IOException {

             System.out.println("method B");

       }

} 

public class OverridingExample {

       public static void main(String args[]) throws IOException {

             A a = new B();

             a.m1();

       }

}

Output : method B

Case 9 : if superclass throws a checked exception but subclass does not throw any exception.

package com.tutorial4u;

import java.io.IOException; 

class A {

       void m1() throws IOException {

             System.out.println("method A");

       }

}

class B extends A {

       void m1() {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) throws IOException {

             A a = new B();

             a.m1();

       }

}

Output : method B

Case 10 : If superclass  throws an unchecked exception but subclass doesn't throw any exception 

package com.tutorial4u;

class A {

       void m1() throws ArithmeticException {

             System.out.println("method A");

       }

}

class B extends A {

       void m1() {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

}

Output : method B

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...