Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

SWAP VARIABLES WITHOUT USING THIRD VARIABLE EXAMPLE USING JAVA

public class Main {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {

                int num1 = 40;
                int num2 = 80;

                System.out.println("Before Swapping");
                System.out.println("Value of num1 is :" + num1);
                System.out.println("Value of num2 is :" +num2);

                //add both the numbers and assign it to first
                num1 = num1 + num2;
                num2 = num1 - num2;
                num1 = num1 - num2;

                System.out.println("After Swapping");
                System.out.println("Value of num1 is :" + num1);
                System.out.println("Value of num2 is :" +num2);
        }


}

output:
Before Swapping
Value of num1 is :20
Value of num2 is :80
After Swapping
Value of num1 is :80
Value of num2 is :40

InputStream and OutputStream example using java

import java.io.*;

public class Main {

    /**
     * @param args the command line arguments
     */

   public static void main(String args[]){

   try{

     byte byWrite [] = {1,12,13,14,15};
     
     OutputStream out = new FileOutputStream("D://ReadFile.txt");

     for(int x=0; x < byWrite.length ; x++){
         out.write( byWrite[x] ); // writes the bytes
      }
      out.close();

      InputStream is = new FileInputStream("D://ReadFile.txt");
      int size = is.available();

      for(int i=0; i< size; i++){
         System.out.print((char)is.read() + "  ");
      }
      is.close();
   }catch(IOException e){
      System.out.print("Exception");
   }
   }
}


creates a new thread and starts it running example using java

class NewThread implements Runnable {

   Thread t;

   NewThread() {

      // Create a new, second thread

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

      System.out.println("Child thread: " + t);

      t.start(); // Start the thread

}

   // This is the entry point for the second thread.

   public void run() {

      try {

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

            System.out.println("Child Thread: " + i);

            // Let the thread sleep for a while.

            Thread.sleep(50);

         }

     } catch (InterruptedException e) {

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

     }

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

   }

}

public class Main {

    public static void main(String args[]) {

      new NewThread(); // create a new thread

      try {

         for(int i = 4; i > 0; i--) {

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

           Thread.sleep(150);

         }

      } catch (InterruptedException e) {

         System.out.println("Main thread interrupted.");

      }

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

   }

}


output:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 4
Child Thread: 5
Child Thread: 4
Child Thread: 3
Main Thread: 3
Child Thread: 2
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

while loop in java

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // TODO code application logic here

      int x = 30;

      while( x < 40 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
output:
value of x : 32
value of x : 33
value of x : 34
value of x : 35
value of x : 36
value of x : 37
value of x : 38
value of x : 39

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...