Thursday, April 9, 2020

Java Tutorial

Professional Program Online Tutorials(PPT)


Java Programming 

Java:
       Java is an object-oriented, cross platform, multi-purpose programming language .

Hello world Program:


package Example;

public class Example {

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

}

Output:
                Hello World 



Addition Two Numbers:

package Example1;

public class Example1 {

public static void main(String[] args) {
int a,b,c;
a=10;
b=5;
c=a+b;


System.out.println("The value of c is " +c);
}


}

Output:

            The value of c is 15


Get Runtime Input:

                It has two types
                         1.Scanner        2.Data Input stream


Scanner

Steps for scanner:
1. Should be give header package
              import java.util.*;
2.Declare the Scanner 
              Scanner add(Variable Name)= new Scanner(System.in);//Input
3.Declare Variable Type
               a(Variable)=add(variable name).nextInt();//Variable type


package Example2;
import java.util.*;

public class Example2 {

public static void main(String[] args) {
int a,b,c;
Scanner add=new Scanner(System.in);
a=add.nextInt();
b=add.nextInt();
c=a+b;

System.out.println("The value of c is " +c);
}


}


output:

10
5
The value of c is 15
)


Data Input Stream


Steps for scanner:
1. Should be give header package
              import java.io.*;
2.Declare the DataInputStream
             DataInputStream add(Variable Name)= new DataInputStream(System.in);//Input
3.Declare Variable Type
               a(Variable)=.parseInt(add(variable name).readLine());//Variable type
           
Integer Variables:
package Example3;
import java.io.*;

public class Example3{

public static void main(String[] args) throws IOException{
int a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Integer.parseInt(add.readLine());
b=Integer.parseInt(add.readLine());
c=a+b;

System.out.println("The value of c is " +c);
}


}

Output:

10
20
The value of c is 30


Double Variables:

package Example3;
import java.io.*;

public class Example3{

public static void main(String[] args) throws IOException{
Double a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Double.parseDouble(add.readLine());
b=Double.parseDouble(add.readLine());
c=a+b;

System.out.println("The value of c is " +c);
}


}

Output:
10.5
20.3
The value of c is 30.8


String:

import java.io.*;

public class Example3{

public static void main(String[] args) throws IOException{
String a;
DataInputStream add=new DataInputStream(System.in);
a=add.readLine();


System.out.println("The Name  is " +a);
}


}

Output

Sabari
The Name is Sabari


Conditional Statements

1.if-else

import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Integer.parseInt(add.readLine());
b=Integer.parseInt(add.readLine());
        if(a>b)
        {
        System.out.println("a is big");
        }
     
        else
        {
        System.out.println("b is big");
        }
       

}


}

Output
23
33
b is big





Odd or Even Program (if-else)

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Integer.parseInt(add.readLine());

        if(a%2==0)
        {
        System.out.println("a is even");
        }
     
        else
        {
        System.out.println("a is odd");
        }
       

}


}

Output
33
a is odd

32
a is even


Positive or Negative (if-else)

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Integer.parseInt(add.readLine());
        if(a>0)
        {
        System.out.println("a is positive");
        }
        
        else
        {
        System.out.println("a is negative");
        }
         

}

}

Output

-3
a is negative



If-else if

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int a,b,c;
DataInputStream add=new DataInputStream(System.in);
a=Integer.parseInt(add.readLine());
b=Integer.parseInt(add.readLine());
c=Integer.parseInt(add.readLine());
        if(a>b && a>b)
        {
        System.out.println("a is big");
        }
        
        else if(b>a && b>c)
        {
        System.out.println("b is big");
        }
        else
        {
        System.out.println("c is big");
        }
         

}

}

Ouput

12
33
11
b is big



Switch-Case

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int n;
DataInputStream add=new DataInputStream(System.in);
n=Integer.parseInt(add.readLine());
switch(n)
{
    case 1:
System.out.print("one");
break;
    case 2:
System.out.print("two");
break;
    default:
    System.out.print("more then two");
}

}

}

Output

1
one



Looping Statements

For Loop

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
for(i=0;i<5;i++)
{
System.out.println("Welcome");
}

}

}

Output

Welcome
Welcome
Welcome
Welcome
Welcome

While Loop


package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
i=0;
while(i<5)
{
System.out.println("Welcome");
i++;
}

}

}

Output

Welcome
Welcome
Welcome
Welcome
Welcome

Do-While

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
i=0;
do
{
System.out.println("Welcome");
i++;
}while(i<5);

}

}

Output

Welcome
Welcome
Welcome
Welcome
Welcome


For Example Programs

1.Print 1 to 100 numbers


package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
for(i=1;i<=100;i++)
{
System.out.println(i);
}

}

}

Output

1 to 100 numbers will print

2.Print the 9 divisible numbers in 1 to 100


package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
for(i=9;i<=100;i=i+9)
{
System.out.println(i);
}

}

}

Output

9
18
27
36
45
54
63
72
81
90
99

3.Print odd numbers 1 to 100

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i;
for(i=1;i<=100;i=i+2)
{
System.out.println(i);
}

}

}

Output

1 to 100 odd number will printed

4.Factorial

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i,n,fact=1;
DataInputStream ds=new DataInputStream(System.in);
n=Integer.parseInt(ds.readLine());
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("fact is "+fact);

}

}

Output

5
fact is 120

5.Prime Number

package s;
import java.io.*;

public class s {

public static void main(String[] args) throws IOException{
int i,n,prime=1;
DataInputStream ds=new DataInputStream(System.in);
n=Integer.parseInt(ds.readLine());
for(i=2;i<n;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime==1)
System.out.println("prime number");
else
System.out.println("not prime number");

}

}

Output

5
prime number


6.Pattern Programs - * pattern

import java.io.*;

public class s {

public static void main(String[] args) {
int i,j;

for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*\t");
}
System.out.println();
}

}

}



Output:

*
* *
* * *
* * * *
* * * * *


Z Pattern

import java.io.*;

public class s {

public static void main(String[] args) {
int i,j;

for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("z\t");
}
System.out.println();
}

}

}


Output:

z
z z
z z z
z z z z
z z z z z

Continuous Number Pattern

import java.io.*;

public class s {

public static void main(String[] args) {
int i,j;

for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}

}

}

Output:

1
22
333
4444
55555

While Loop Examples

import java.util.*;

public class s {
t
public static void main(String[] args) {
int i,a,n;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
while(n!=0)
{
a=n%10;
System.out.print(a);
n=n/10;
}

}

}


Output:

75
57


Arrays:

             Is a Collection of different datatypes.
             It has three types
                     1.single dimensional array
                     2.Two dimensional array
                     3.Multi dimensional array

Example:
       
1.single Dimensional array


package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]=new int[5];
Scanner sc=new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
System.out.println("the numbers are");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}

}

}

Output

23
33
44
22
3
the numbers are
23
33
44
22
3



Single dimensional Example Programs

Find the Negative number in a Array


package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]= {23,-33,55,99};
Scanner sc=new Scanner(System.in);
System.out.println("the negative numbers are");
for(int i=0;i<a.length;i++)
{
if(a[i]<0)
System.out.println(a[i]);
}

}

}


Output

the negative numbers are
-33

2.odd array 

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]= {23,-33,52,99};
Scanner sc=new Scanner(System.in);
System.out.println("the negative numbers are");
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
System.out.println(a[i]);
}

}

}

Output

the negative numbers are
23
-33
99

3.Even Array

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]= {23,-33,52,99};
Scanner sc=new Scanner(System.in);
System.out.println("the negative numbers are");
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
System.out.println(a[i]);
}

}

}
Output

the negative numbers are
52
4.Find the divisible by 9 numbers in a Array

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]= {18,-33,52,99};
Scanner sc=new Scanner(System.in);
System.out.println("the negative numbers are");
for(int i=0;i<a.length;i++)
{
if(a[i]%9==0)
System.out.println(a[i]);
}

}

}

Output

18
99
6.Ascending Order

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[]= {21,12,2,99};
Scanner sc=new Scanner(System.in);
Arrays.sort(a);
System.out.println("the Ascending order");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}

}

}

Output

the Ascending order
2
12
21
99

2.Two Dimensional Arrays

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[][]=new int[3][3];
Scanner sc=new Scanner(System.in);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("the numbers are");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println(a[i][j]);
}
}
System.out.println("print matrix format");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("");
}
}

}
Output

1
2
3
4
5
6
7
8
9
the numbers are
1
2
3
4
5
6
7
8
9
print matrix format
1 2 3
4 5 6
7 8 9


3.Multi dimenstional Arrays
package s;

import java.util.*;

public class s {

public static void main(String[] args) {
int a[][][]=new int[2][2][2];
Scanner sc=new Scanner(System.in);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
a[i][j][k]=sc.nextInt();
}
}
}
System.out.println("the numbers are");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
System.out.println(a[i][j][k]);
}
}
}
}

}
Output

1
2
3
4
5
6
7
8
the numbers are
1
2
3
4
5
6
7
8


Exceptions:

            Runtime error is called Exception
            it has three types
  1. Runtime Exception
  2. Compile time exception
  3. User defined exception                 
Parts of Exceptions:

  1. try
  2. catch
  3. throw
  4. throws
  5. finally    

Runtime Exceptions:

  1. Arithmetic exception
  2. ArrayIndexOutofBounds Exception
  3. NegativeArraySize Exception
  4. InputMissMatch Exception
  5. NumberFormatException
  6. NullPointer Exception                   
Arithmetic exception

               The mathematical error is called arithmetic exception.
Example:
import java.util.*;

public class s {

public static void main(String[] args) {
int a, b, c;
a=10;
b=0;
try {
c=a/b;
System.out.print(c);
}
catch(Exception e) {
System.out.println(e);
}
}
}


Output:
java.lang.ArithmeticException: / by zero


ArrayIndexOutofBounds Exception

                      The array size is bigger than given size 
Example:
public class s {

public static void main(String[] args) {
try {
int a[]=new int[5];
System.out.println(a[6]);
}
catch(Exception e) {
System.out.println(e);
}
}
}


Output:
java.lang.ArrayIndexOutOfBoundsException: 6


NegativeArraySize Exception:

                 The should not be negative value.
Example:

public class s {

public static void main(String[] args) {
try {
int a[]=new int[-5];
System.out.println(a[6]);
}
catch(Exception e) {
System.out.println(e);
}
}
}

Output:

java.lang.NegativeArraySizeException


InputMissMatch Exception:
            The input is miss match with given data type.

import java.util.*;

public class s {

public static void main(String[] args) {
try {
int a;
Scanner sc= new Scanner(System.in);
a=sc.nextInt();
}
catch(Exception e) {
System.out.println(e);
}
}
}

Output:

name
java.util.InputMismatchException

NumberFormatException:

                 Count the number of given string value.

import java.util.*;

public class s {

public static void main(String[] args) {
try {
String a= "hello";
int b=Integer.parseInt(a);
}
catch(Exception e) {
System.out.println(e);
}
}
}


Output:

java.lang.NumberFormatException: For input string: "hello"


NullPointer Exception:
                   When the value or input is null 

Example:

import java.util.*;

public class s {

public static void main(String[] args) {
try {
String a= null;
System.out.println(a.length());
}
catch(Exception e) {
System.out.println(e);
}
}
}

Output:

java.lang.NullPointerException


Finally

Before the Exception Finally Printed

package s;

import java.util.*;

public class s {

public static void main(String[] args) {
try {
String a= null;
System.out.println(a.length());
}
finally {
System.out.println("before the error it will be print");
}
}
}


Output

before the error it will be print
Exception in thread "main" java.lang.NullPointerException
at s.s.main(s.java:12)

Compile Time Exceptions

1.IOException


import java.io.*;

public class Example3{

public static void main(String[] args) throws IOException{
String a;
DataInputStream add=new DataInputStream(System.in);
a=add.readLine();
System.out.println("The Name  is " +a);
}

}

Output

Sabari

2.InterruptedException

import java.io.*;

public class Example3{

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

     for(int i=0;i<5;i++)
{
Thread.sleep(1000);
System.out.println("hai");
}

}


}

Output

hai
hai
hai
hai
hai

3.SQLException

package s;

import java.sql.*;

public class s {

public static void main(String[] args){
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ex","root","");  
}catch(Exception e){ System.out.println(e);}  
}
}
}
}

Throw Example

package s;

import java.sql.*;

public class s {

static void get(int a)
{
if(a<18)
throw new ArithmeticException("not eligible for vote");
else
System.out.println("eligble for vote");
}
public static void main(String[] args){
get(5);
}


}

Output

Exception in thread "main" java.lang.ArithmeticException: not eligible for vote
at s.s.get(s.java:10)
at s.s.main(s.java:16)

3.Userdefined Exception


class JavaException{
   public static void main(String args[]){
  try{
       throw new MyException(2);
       // throw is used to create a new exception and throw it.
  }
 catch(MyException e){
    System.out.println(e) ;
 }
}
}
class MyException extends Exception{
   int a;
   MyException(int b) {
     a=b;
   }
   public String toString(){
     return ("Exception Number =  "+a) ;
  }
}

Output

Exception Number =  2


String Functions in Java

String is im muttable in java

1.String Length

package s;



public class s {

public static void main(String[] args){
String a="hejex";
System.out.println(a.length());
}


}

Output

5


2.String Concat

package s;



public class s {

public static void main(String[] args){
String a="hello";
String b="world";
System.out.println(a.concat(b));
}


}

Output

helloworld

3.String UpperCase

package s;



public class s {

public static void main(String[] args){
String a="hello";
System.out.println(a.toUpperCase());
}


}
Output

HELLO

4.String LowerCase


package s;



public class s {

public static void main(String[] args){
String a="HELLO";
System.out.println(a.toLowerCase());
}


}
Output

hello

5.CharAt(Find the Character in a Word)

package s;



public class s {

public static void main(String[] args){
String a="hello";
System.out.println(a.charAt(0));
}


}

Output
h

6.IndexOf(Find the Letter in a Number)

package s;



public class s {

public static void main(String[] args){
String a="hello";
System.out.println(a.indexOf('e'));
}


}

Output

1

7.lastIndexOf(Find the Last letter in Continous Letter)

package s;



public class s {

public static void main(String[] args){
String a="hello";
System.out.println(a.lastIndexOf('l'));
}


}

Output

3

8.Trim(To Remove the Space in the word)


package s;



public class s {

public static void main(String[] args){
String a="     hello";
System.out.println(a.trim());
}


}

Output

hello

9.Equals

package s;



public class s {

public static void main(String[] args){
String a="hello";
String b="hello";
if(a.equals(b))
{
System.out.println("both are same");
}
else
{
System.out.println("not same");
}
}


}
Output
both are same

10.EqualsIgnoreCase

package s;


public class s {

public static void main(String[] args){
String a="hello";
String b="HELLO";
if(a.equalsIgnoreCase(b))
{
System.out.println("both are same");
}
else
{
System.out.println("not same");
}
}


}
Output
both are same

11.Replace



public class s {

public static void main(String[] args){
String a="hello";
a=a.replace('e','a');
System.out.println(a);
}


}
Output
hallo

12.Contains

package s;


public class s {

public static void main(String[] args){
String a="This is hello World Program";
if(a.contains("this"))
{
System.out.println("found");
}
else
{
System.out.println("not found");
}
}


}
Output
found


13.IsEmpty

package s;


public class s {

public static void main(String[] args){
String a="This is hello World Program";
if(a.isEmpty())
{
System.out.println("yes");
}
else
{
System.out.println("not empty");
}
}


}
Output
not empty

14.startsWith
package s;


public class s {

public static void main(String[] args){
String a="This is hello World Program";
if(a.startsWith("This"))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
}


}
Output

yes

15.endsWith
package s;


public class s {

public static void main(String[] args){
String a="This is hello World Program";
if(a.endsWith("This"))
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
}


}
Output

no

STRING BUFFER AND STRING BUILDERS

String buffer and builders are muttable

String buffer

package s;



public class s {

public static void main(String[] args){
StringBuffer s=new StringBuffer("hello");
s.append("world");
System.out.println(s);
}

}

Output 

helloworld

String Builder


package s;



public class s {

public static void main(String[] args){
StringBuilder s=new StringBuilder("hello");
s.append("world");
System.out.println(s);
}

}

Output 

helloworld


OOPS in java


Object-oriented programming:
OOPs Concepts:




1.Class and Objects Example

package s;
class A
{
void get()
{
System.out.println("hai");
}
}


public class s {

public static void main(String[] args){

A ob=new A();
ob.get();
}


}

Output

hai

2.Class and Objects with Parameters Example

package s;
class A
{
void get(int a)
{
System.out.println(a);
}
}


public class s {

public static void main(String[] args){

A ob=new A();
ob.get(5);
}


}

Output
5

3.Constructor

Constructor is a Special Methods

Rules of Constructor

  • Constructor(s) of a class must has same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static and Synchronized.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.
No-argument constructor


package s;
class A
{
A()
{
System.out.println("hai");
}
}


public class s {

public static void main(String[] args){

A ob=new A();

}


}


Output

hai

Parameterized Constructor


package s;
class A
{
A(int a)
{
System.out.println(a);
}
}


public class s {

public static void main(String[] args){

A ob=new A(5);

}


}



Output
5

4.Polymorphism

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Compile time Polymorphism

1.Method Overloading
  
 Same method name but different parameters

package s;
class A
{
void get()
{
System.out.println("hai");
}
void get(int a)
{
System.out.println(a);
}
}


public class s {

public static void main(String[] args){

A ob=new A();
ob.get();
ob.get(5);
}


}

Output

hai
5

2.Constrctor  Overloading
  
 Same constructor name but different parameters

package s;
class A
{
A()
{
System.out.println("hai");
}
A(int a)
{
System.out.println(a);
}
}


public class s {

public static void main(String[] args){

A ob=new A();
A ob=new A(5);
}


}

Output

hai
5

Run time Polymorphism

Super class and sub class have same method name and parameter called as Run time polymorphism

Method Overriding

Example

package s;
class A
{
void get()
{
System.out.println("hai");
}
}

class B
{
void get()
{
System.out.println("welcome");
}
}

public class s {

public static void main(String[] args){

B ob=new B();
ob.get();
}


}

Output

welcome

Super Keyword

    to avoid method over riding we added super keyword.super is a keyword always points the super class...


Example

package s;
class A
{
void get()
{
System.out.println("hai");
}
}

class B
{
void get()
{
super.get();//its points the super class
System.out.println("welcome");
}
}

public class s {

public static void main(String[] args){

B ob=new B();
ob.get();
}


}

Output

hai
welcome

Inheritance:

          Properties of one class can be access by another class.
          Code re usability.
Example:

1.single inheritance


package s;
class A //Super class or base class
{
void get()
{

System.out.println("hai");
}
}


 class B extends A //Sub class or Derived class

 {

void get1()
{
System.out.println("welcome");
}

}

class s
{
public static void main(String args[])
{
B ob=new B();
ob.get();
ob.get1();
}

}

Output

hai
welcome

2.Multilevel inheritance

   Example:


package s;
class A //Super class or base class
{
void get()
{

System.out.println("hai");
}
}


 class B extends A //Sub class or Derived class

 {

void get1()
{
System.out.println("welcome");
}

}

class C extends B
{
void display()
{
System.out.println("Hello");
}

}



class s{
public static void main(String args[])
{
C ob=new C();
ob.get();
ob.get1();
ob.display();

}
}

Ouput

hai
welcome

Hello

Interface:
           Instead of Multiple inheritance.
Steps for interface:

  • Interface A {        }              //Instead of class and class name
  • Method should be declared but do not define it.
                     interface A{
                     void get();
                      }
  • Method should be given in public
                      public void get()
  • Implements instead of extends   
                      interface B implements A

Example:

 interface A
{
    public void get();
}
class B implements A
{
    public void get()
    {
        System.out.println("hai");
    }
}
class JavaApplication11
{
    public static void main(String[] args) {
        B ob=new B();
        ob.get();
    }
}



Output:

hai


Thread:
          Is a small unit of program.

Multithreading:
          Group of threads

Life cycle of threads:


  • New stage  - thread starts with start () method
  • Runnable stage 
  • Running stage - run() method is used to run a thread
  • Terminated stage - Stop a thread by using stop() method
Types of thread:

  • extends thread (Inheritance)
  • implements thread(interface)
Example for extends thread:

class A extends Thread   //runnable stage
{
public void run()
{
System.out.println("run stage");
}
}


class s{
public static void main(String args[])
{
A ob=new A();
ob.start();        //new stage
}
}


Output:

run stage                    


Example for implements thread:

class A implements Runnable
{
    public void run()
    {
        System.out.println("hai");
    }
}

class JavaApplication11
{
    public static void main(String[] args) {
        A ob=new A();
        Thread t = new Thread(ob);
        t.start();
    }
}


Output:

hai


Example 2:
class A implements Runnable
{
    public void run()
    {
       for(int i=0;i<5;i++)
       {
           try{
               Thread .sleep(1000);
               System.out.println("run stage");
           }
           catch(InterruptedException e){
               System.out.println("e");
                       
           }
       }
    }
}

class JavaApplication11
{
    public static void main(String[] args) {
        A ob=new A();
        Thread t = new Thread(ob);
        t.start();
    }

}

Output:

run stage
run stage
run stage
run stage

run stage   // It will take time to print the line.

Priority:










Collections in java

  1. ArrayList class
  2. LinkedList class
  3. List interface
  4. HashSet class
  5. LinkedHashSet class
  6. TreeSet class
  7. PriorityQueue class
  8. Map interface
  9. HashMap class
  10. LinkedHashMap class
  11. TreeMap class
  12. Hashtable class
  13. Sorting
  14. Comparable interface
  15. Comparator interface
  16. Properties class in Java


int a[]=new int[5];
System.out.println(a[6])
ArrayIndexOutOfBoundsException

Dynamic array store

Java ArrayList class uses a dynamic array for storing the elements.


1. Java ArrayList class can contain duplicate elements.
Ex
Input
Sabari
Saran
Sabari
Output
Sabari
Saran
          Sabari

2.Java ArrayList class maintains insertion order.
Ex
Input
Sabari 
Saran
Output
Sabari
Saran
3.Java ArrayList class is non synchronized.
4.Java ArrayList allows random access because array works at the index basis.

Package import

import java.util.*;
import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
                     ArrayList<String> al=new ArrayList<String>();//Creating arraylist   
    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
    System.out.println(al); 
             
       }
}

Output
[Ravi, Vijay, Ravi]

For each

package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
                     ArrayList<String> al=new ArrayList<String>();//Creating arraylist   
    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}

Output
Ravi
Vijay
Ravi


LinkedList
Java LinkedList class uses a doubly linked list to store the elements.

Java LinkedList class can contain duplicate elements.
Java LinkedList class maintains insertion order.
Java LinkedList class is non synchronized.
In Java LinkedList class, manipulation is fast because no shifting needs to occur.



package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
                     LinkedList<String> al=new LinkedList<String>();//Creating arraylist   
    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}
Output
Ravi
Vijay

List Interface

package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
    List<String> al=new ArrayList<String>();//list interface
    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}

Ouput

Ravi
Vijay
Ravi



























Set

HashSet

HashSet class is used to create a collection that uses a hash table for storage


1)HashSet stores the elements by using a mechanism called hashing.
2)HashSet contains unique elements only.
3)HashSet allows null value.
4)HashSet class is non synchronized.
5)HashSet doesn't maintain the insertion order.


package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
                     HashSet<String> al=new HashSet<String>();
    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}

Output

Vijay
Ravi

Linked Hashset


Java LinkedHashSet class contains unique elements only like HashSet.
Java LinkedHashSet class provides all optional set operation and permits null elements.
Java LinkedHashSet class is non synchronized.
Java LinkedHashSet class maintains insertion order.


package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
    LinkedHashSet<String> al=new LinkedHashSet<String>();

    al.add("Ravi");//Adding object in arraylist   
    al.add("Vijay");   
    al.add("Ravi");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}

Ouput

Ravi
Vijay












TreeSet


Java TreeSet class contains unique elements only like HashSet.
Java TreeSet class access and retrieval times are quiet fast.
Java TreeSet class doesn't allow null element.
Java TreeSet class is non synchronized.
Java TreeSet class maintains ascending order.


package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
    TreeSet<String> al=new TreeSet<String>();//list interface
    al.add("bala");//Adding object in arraylist   
    al.add("abi");   
    al.add("cat");   
    
  for(String a:al) //for each loop 
  {
         System.out.println(a);
  }
             
       }
}

Output
Abi
Bala
Cat

Queue
 Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.

package s;

import java.util.*; //run time input
class s{
              public static void main(String args[])
       {
                    
                     PriorityQueue<String> queue=new PriorityQueue<String>(); 
                     queue.add("abc"); 
                     queue.add("bcd"); 
                     queue.add("efg"); 
                    
  for(String a:queue) //for each loop 
  {
         System.out.println(a);
  }

 
  System.out.println("head element   "+queue.peek());
  queue.poll();//remove
  System.out.println("after remove"); 
  for(String a:queue) //for each loop 
  {
         System.out.println(a);
  }

 
       }
}

Output
abc
bcd
efg

head element   abc

after remove

bcd
efg










1 comment:

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...