Total Pageviews

Friday, April 12, 2019

dynamic 2d array in java

class cll
{
public static void main(String args[])
{

int a[][];
a=new int[2][];
for (int i=0;i<2;i++)
{
   a[i]=new int [i+2];
   for(int j=0;j<i+2;j++)
     {
            a[i][j]=i*j;
     }
}

System.out.println("matrix  ");
for (int i=0;i<2;i++)
{
 
   for(int j=0;j<i+2;j++)
     {
            System.out.print(a[i][j]+" ");
     }
     System.out.println(" ");
}

System.out.println("total row "+ a.length);
System.out.println("total column in row 0="+a[0].length);
System.out.println("total column in row 1="+a[1].length);

}
}




D:\>javac cll.java

D:\>java cll
matrix
0 0
0 1 2
total row 2
total column in row 0=2
total column in row 1=3

D:\>



exception handling



public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}




$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
 public static void main(String args[]) {
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at cl.main(cl.java:4)

exception handling

{
 public static void main(String args[]) {
  String s="abc";
int i=Integer.parseInt(s);//NumberFormatException

  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.parseInt(Integer.java:615)
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  String s=null;
System.out.println(s.length());//NullPointerException
  System.out.println("End Of Main");
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.NullPointerException
 at cl.main(cl.java:4)

exception handling

public class cl {
 public static void main(String args[]) {
  int d = 0;
  int n = 20;
  try {
   int fraction = n / d;
   System.out.println("This line will not be Executed");
  } catch (ArithmeticException e) {
   System.out.println("In the catch Block due to Exception = " + e);
  }
  System.out.println("End Of Main");
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
In the catch Block due to Exception = java.lang.ArithmeticException: / by zero
End Of Main

exception handling

public class cl {
   public static void main(String args[]){
      int d = 0;
      int n = 20;
      int fraction = n/d;
     System.out.println("End Of Main");
   }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at cl.main(cl.java:5)

thread suspend()/resume()

public class cl extends Thread{
 public void run(){
  for(int i=1;i<=3;i++){
   try{
    Thread.sleep(500);
   }catch(Exception e){System.out.println(e);}
  System.out.println(i);
  }
 }
public static void main(String args[]){
 cl t1=new cl();
 cl t2=new cl();
 cl t3=new cl();
 t1.start();
 t2.start();
 t3.start();
 }
}


$javac cl.java
$java -Xmx128M -Xms16M cl
1
1
1
2
2
2
3
3
3




public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 
 t1.start(); 
 t2.start(); 
 t2.suspend();
 t3.start(); 
 t2.resume();
 } 
}

daemon thread

public class cl extends Thread{
 public void run(){
  if(Thread.currentThread().isDaemon()){
   System.out.println("daemon thread");
  }
  else{
  System.out.println("user thread ");
 }
 }
 public static void main(String[] args){
  cl t1=new cl();
  cl t2=new cl();
  cl t3=new cl();

  t1.setDaemon(true);
 
  t1.start();
  t2.start();
  t3.start();
 }
}

$javac cl.java
$java -Xmx128M -Xms16M cl
daemon thread
user thread 
user thread 

set priority of thread


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
  t1.setPriority(Thread.MIN_PRIORITY); 
  t2.setPriority(Thread.MAX_PRIORITY);
 
 t1.start(); 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:1
1
Thread-1 running thread priority is:10
1
Thread-2 running thread priority is:5
1
Thread-0 running thread priority is:1
2
Thread-1 running thread priority is:10
2
Thread-2 running thread priority is:5
2
Thread-0 running thread priority is:1
3
Thread-1 running thread priority is:10
3
Thread-2 running thread priority is:5
3

thread priority


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" ");
   System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); 

 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 



$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 running thread priority is:5
1
Thread-0 running thread priority is:5
2
Thread-0 running thread priority is:5
3
Name of t1:dj thread
Thread-1 running thread priority is:5
1
Thread-2 running thread priority is:5
1
Thread-1 running thread priority is:5
2
Thread-2 running thread priority is:5
2
Thread-1 running thread priority is:5
3
Thread-2 running thread priority is:5
3

current thread name

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }
   catch(Exception e){System.out.println(e);} 
   System.out.print(Thread.currentThread().getName()+" "); 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
Thread-0 1
Thread-0 2
Thread-0 3
Name of t1:dj thread
Thread-1 1
Thread-2 1
Thread-1 2
Thread-2 2
Thread-1 3
Thread-2 3

thread setname(),getname(),getid()

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
1
2
3
Name of t1:dj thread
1
1
2
2
3
3


multithreading with join method


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
 t2.start(); 
 t3.start(); 
 } 
}


$javac cl.java
$java -Xmx128M -Xms16M cl
1
2
3
1
1
2
2
3 
3 

multithreading with start()

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<5;i++){ 
    try{
        Thread.sleep(300);
       
    }
    catch(InterruptedException e)
    {System.out.println(e);} 
    System.out.println(i); 
  } 
 } 
 public static void main(String args[]){ 
  cl t1=new cl(); 
  cl t2=new cl(); 
 
  t1.start(); 
  t2.start(); 
 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
1
1
2
2
3
3
4
4

multithreading with run()

public class cl extends Thread{
 public void run(){
  for(int i=1;i<5;i++){
    try{
        Thread.sleep(300);
     
    }
    catch(InterruptedException e)
    {System.out.println(e);}
    System.out.println(i);
  }
 }
 public static void main(String args[]){
  cl t1=new cl();
  cl t2=new cl();
 
  t1.run();
  t2.run();
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
1
2
3
4
1
2
3
4

Java Thread Example by implementing Runnable interface

public class cl implements Runnable{ 
public void run(){ 
System.out.println("thread is running..............................."); 

 
public static void main(String args[]){ 
cl ob=new cl(); 
Thread t1 =new Thread(ob); 
t1.start(); 
 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
thread is running...............................

thread using thread class

code:

public class cl extends Thread{ public void run(){ System.out.println("thread is running.............."); } public static void main(String args[]){ cl ob=new cl(); ob.start(); } }


output:
$javac cl.java
$java -Xmx128M -Xms16M cl
thread is running..............

variable arguments in java.

1. variable-argument-in-java    link
2.  java-program- with overloading -  link
3.  variable-argument- with-overloading   ambiguity - link                                

variable argument with overloading ambiguity in java

// Java program to demonstrate varargs
public class cl
{
// A method that takes variable number of intger
// arguments.


static void f(double x)
{
    System.out.println("overloaded function");
    System.out.println(x);
}

static void f(int x)

{

    System.out.println("overloaded function");

    System.out.println(x);

}

static  void f(int ...a)
{
System.out.println("Number of arguments: " + a.length);


for (int i=0;i<a.length;i++)
System.out.print(i + " ");
System.out.println();
}





public static void main(String args[])
{

f(100); // one parameter
f(1, 2, 3, 4); // four parameters
f();
f(1.1);// no parameter
}
}

$javac cl.java
$java -Xmx128M -Xms16M cl
overloaded function
100
Number of arguments: 4
0 1 2 3 
Number of arguments: 0

overloaded function
1.1