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

Wednesday, 7 February 2018

Simple Java program to print current date and time.


Java program to display current date.

Date function in Java Programming code.



/*
Java Date example.
This Java Date example program describes how Java Date function from utility is being used in Java language.
*/

import java.util.*;

public class JavaDateProgram{

          public static void main(String args[]){
            /*
              Create date object with current date and time.
            */
 Date dateandtime = new Date();
  System.out.println("Current Time is " + dateandtime);

  }
}

Output:


Current Time is Sat Feb 08 16:10:21 IST 2018

Friday, 1 April 2016

Swap Numbers Without Using Third Variable Java Example

Java program to Swap Numbers Without Using Third Variable

Program:


public class SwapElement {  

    public static void main(String[] args) {
        
        int num1 = 10;
        int num2 = 20;
        
        System.out.println("Before Swapping");
        System.out.println("Value of num1 is :" + num1);
        System.out.println("Value of num2 is :" +num2);
        
        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        
        System.out.println("Before Swapping");
        System.out.println("Value of num1 is :" + num1);
        System.out.println("Value of num2 is :" +num2);
    }  
}

Output: 


Before Swapping
Value of num1 is :10
Value of num2 is :20
Before Swapping
Value of num1 is :20

Value of num2 is :10

Tuesday, 26 January 2016

Multi Dimensional Array Example Java Program

How to use Multi Dimensional Array in Java,


                   The Simple Java Multi Dimensional Example program shows how to declare, initialize a Multi Dimensional Array and print data from it.

Multi Dimensional Array Example Program:


// A simple java program which shows the multidimensional array declaration, Initialization and Printing.

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

     // Array declaration and initialization
      int twoDimension[][] = {{1,2,3,4},{4,3,2,1},{5,6,7,8}};

      for(int i=0; i < 3 ; i++){
            for (int j=0; j < 4 ; j++){
                 System.out.print(twoDimension[i][j]+" ");
             }
             System.out.println();
      }    
   }
}

Program Output:


1 2 3 4
4 3 2 1
5 6 7 8 

Monday, 25 January 2016

The Left Shift Operator in Java

How to use the Java Left Shift Operator


               The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times. For each shift left, the high-order bit is shifted out (and lost), and a zero is brought in on the right.

Syntax of Left Shift Operator in Java:


value << num

num: number of positions to left-shift the value in value.

Example Program for Left Shift Operator


// A Java Program to demonstrate how the Left Shift Operator works

public class LeftShift {

    public static void main(String args[]){    

      int x=12;
      int y = x << 2
      System.out.println("The value before Left Shift: " + x);
      System.out.println("The value after Left Shift: " + y);

   }
}

Output:


The value before Left Shift: 12
The value after Left Shift: 48

Java Program to open a Notepad window

Notepad window open program using Java Code

How to open Notepad using Java


We can open the Windows Notepad using Java code. The below Java program shows how to open a Notepad Window in Java


Program


//A small java program to open Notepad window on the screen.

import java.util.*;
import java.io.*; 
class Notepad 
{
  public static void main(String[] args) 
  {
      Runtime rs = Runtime.getRuntime();
       try
        {
              rs.exec("notepad");
        }
       catch (IOException e)
       {
             System.out.println(e);
       }   
  }
}

Output















Notes: 

Runtime : Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

getRuntime : Returns the runtime object associated with the current Java application. Most of the methods of class Runtime are instance methods and must be invoked with respect to the current runtime object.