Friday, August 4, 2023

Thread class in Java - 1

 Java Thread Programs

1. Dealing with main

class T1Demo

{

public static void main(String args[])

{

Thread t=Thread.currentThread();

System.out.println("current "+t);

System.out.println(t.getName());

t.setName("MyMain");

System.out.println("current now "+t);

System.out.println(t.getName());

try

{

for(int i=0;i<10;i++)

{

System.out.println(i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

}

}

o/p: --------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T1Demo

current Thread[main,5,main]

main

current now Thread[MyMain,5,main]

MyMain

0

1

2

3

4

5

6

7

8

9

In [main, 5, main], 5 is the priority of the thread, the last main is the group name of the thread.

Thread class in Java - 2

 2. Creating new thread by implementing Runnable

class NewThread implements Runnable

{

Thread t;

NewThread()

{

//create a new thread

t=new Thread(this,"Demo Thread");

System.out.println("child thread "+t);

t.start();

}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println("child "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted");

}

System.out.println("exiting child thread");

}

}

class T2Demo

{

public static void main(String args[])

{

new NewThread();

try

{

for(int i=100;i>95;i--)

{

System.out.println("Main "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("exiting main thread");

}

}


o/p: -------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T2Demo

child thread Thread[Demo Thread,5,main]

child 5

Main 100

child 4

Main 99

child 3

child 2

Main 98

child 1

exiting child thread

Main 97

Main 96

exiting main thread

--------------------------------------------------------------------------------------------------

Saturday, July 22, 2023

Use of ' this ' keyword in Java

 How to use ' this ' keyword in java?


class A
{
    public int x;
    A()
    {
        x=0;
        System.out.println("Default Constructor");
    }
    A(int x)
    {
        this.x=x;
        System.out.println("I am THIS.X");
    }
}

public class UseofThis{
    public static void main(String[] args) {
        A ob1=new A();
        A ob2=new A(5);
    }
}

Use of Scanner Class in Java

SCANNER CLASS: 

Example 1:

import java.util.Scanner;

public class ScannerEg1 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number");

        int a = sc.nextInt();

        double sq = a * a;

        double rem = sq % 10;

        System.out.println("square is" + sq);

        System.out.println("Last Digit is" + rem);

    }

}


Example 2:

import java.util.*;

public class ScannerEg2 {

    public static void main(String[] args) {

        int n, sq, rem;

        Scanner sc = new Scanner(System.in);

        System.out.println("enter a number");

        n = sc.nextInt();

        sq = n * n;

        System.out.println(sq);

        rem = sq % 10;

        System.out.println(rem);

        if (n == rem) {

            System.out.println("equal");

        } else {

            System.out.println("not equal");

        }

    }

}

Static function in java

How to call a method in main without creating object in Java ?

public class Staticfunc {

    static void f()          //Static keyword is used as the f() is called in main method without object.

    {

        System.out.println("Sunday");

    }

    public static void main(String[] args) {

        System.out.println("Monday");

        f();

        System.out.println("friday");

    } 

Non-static Function in JAVA

How to call a function in main by creating object in java ?

 public class NonStaticFunc {

    void f()
    {
        System.out.println("Sunday");
    }
    public static void main(String[] args) {
        NonStaticFunc ob=new NonStaticFunc();
        System.out.println("Monday");
        ob.f();
        System.out.println("Friday");
    }
}
// Static keyword is not used here as object is created.//

Function Overloading using JAVA ( Method 2 )

 class Func

{
   void f()
    {
        System.out.println("this is without parameter");
    }
    void f(int x)
    {
        System.out.println("this is integer parameter");
    }
    void f(float x)
    {
        System.out.println("this is float parameter");
    }
    void f(char x)
    {
        System.out.println("this is character parameter");
    }
}
public class FunctionOverloading2 {
    public static void main(String[] args) {
         Func ob=new Func();
         ob.f();
        ob.f(5);
        ob.f(10.5f);
        ob.f('a');
    }
}

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...