Category Archives: how to

Valid Variations of main() in Java


Main method is launcher method acts as an entry point for the JVM to start execution of a program. JVM always looks the main() method signature to launch the program. Below are different variation of main() method that are valid in Java.

  1. Default Prototype: This most  preferred way to write main() method in Java
    class TestClass
    {
        public static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

    Meaning of the main Syntax:

    • main(): This is launcher method configured in the JVM to initiate execution of a program.
    • String[]: These are parameters passed as command line arguments.
    • public: This keyword is access modifier to define scope of a method. For JVM can execute the method from anywhere.
    • static: This keyword is Non access modifier use to show part of a class.Here use with Main method so that called by JVM without any object.
    • void: The main method doesn’t return anything. void keyword use to as part of method signature if there is no return type.
  2. Order of Modifiers: We can swap position of modifiers (static and public) in main method.
    class TestClass
    {
        static public void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  3. Variants of String Array Arguments: We can place square brackets at different positions or use varargs (…) for arguments in main method.
             Arguments Array Declaration (Way 1):

    class TestClass
    {
        public static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

    Arguments Array Declaration (Way 2):

    class TestClass
    {
    	public static void main(String args[])
    	{
    		System.out.println("Main Method");
    	}
    }
    

    Arguments with Variants:

    class TestClass
    {
    	public static void main(String...args)
    	{
    		System.out.println("Main Method");
    	}
    }
    
  4. Final Modifier to static main method: We can make main() as final.
    class Testclass
    {
        public final static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  5. Final Modifier String argument: We can make String args[] as final.
    class TestClass
    {
        public static void main(final String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  6. synchronized keyword to static main method:
    class TestClass
    {
        public synchronized static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  7. strictfp keyword to static main method: strictfp used to restrict floating point calculations.
    class TestClass
    {
        public strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    
  8. Combined all  above keyword to static main method:
    class TestClass
    {
        final static synchronized strictfp static void main(String[] args)
        {
            System.out.println("Main Method");
        }
    }
    

Inheritance of main() method

In inheritance JVM Executes the main() without any errors.

class Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
} 

class Child extends Parent
{ 

}

or

class Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Parent");
    }
}
class Child extends Parent
{
    public static void main(String[] args)
    {
        System.out.println("Main Method Child");
    }
}

In both the cases, Parent.class and Child.class files generated by Java compiler javac. When we execute any of the two .class, JVM will execute without any error as Child class hide parent class method when override.

Method Overloading with main()

Java allow the main method overloading but the program doesn’t execute the overloaded main method when we run your program. Program execution start for one method only which is following one of any above allowed main() method signatures.

import java.io.*;
public class Test { 

	// Normal main()
	public static void main(String[] args) {
		System.out.println("Hello Facing Issues on IT? (from main)");
		Test.main("Facing Issues on IT");
	} 

	// Overloaded main methods
	public static void main(String arg1) {
		System.out.println("Hello, " + arg1);
		Test.main("Dear ","Facing Issues on IT");
	}
	public static void main(String arg1, String arg2) {
		System.out.println("Hello , " + arg1 + ", " + arg2);
	}
}

Output


Hello Facing Issues on IT? (from main)
Hello  Facing Issues on IT
Hello Dear Facing Issues on IT
Advertisements

Java : How to convert seconds to Time?


In this blog, you will learn to convert seconds to time. An hour has 60*60=3600 seconds and a minute has 60 seconds.

Example
In this example helps you to convert total seconds to time as (hour, minute an d second). To show as text String convert these these values to String.

public class ConvertSecondToTime {

public static void main(String[] args) {
 long time = 180500700;
 ConvertSecondToTime ts = new ConvertSecondToTime();
 System.out.println("Inserted Total Seconds :"+time);
 System.out.println(ts.secondsToTimeString(time));
}

public static String secondsToTimeString(long time) {
 int seconds = (int) (time % 60);
 int minutes = (int) ((time / 60) % 60);
 int hours = (int) ((time / 3600) % 24);
 String secondsTxt = (seconds < 10 ? "0" : "") + seconds;
 String minutesTxt = (minutes < 10 ? "0" : "") + minutes;
 String hoursTxt = (hours < 10 ? "0" : "") + hours;
 return new String("Converted Time :"+ hoursTxt + " Hour : " + minutesTxt + " Minute :" + secondsTxt + " Second");
 }
}

Output


Inserted Total Seconds :180500700
Converted Time :03 Hour : 05 Minute :00 Second

Java : Convert Decimal to Binary


In this blog, you will learn to convert decimal number into binary string. The java.lang package provides api’s to convert a decimal number into a binary number.

Example
This program takes a decimal number from user as string and convert it to decimal number. toBinaryString() method takes an integer type value and returns its string representation of integer values which represents the data in binary. The base of binary number is 2.

import java.util.Scanner;

public class ConvertDecimalToBinary {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.println("Enter the decimal value:");
String hex = scan.nextLine();
// convert String to Int
int i = Integer.parseInt(hex);
// convert integer to binary
String by = Integer.toBinaryString(i);
// print binary String
System.out.println("Binary: " + by);
}
}

Output


Enter the decimal value:
12
Binary: 1100

Enter the decimal value:
200
Binary: 11001000

Java : How to convert GMT, CST, IST and PST timezone?


In this article, you will learn to convert a date to GMT, CST, EST and PST format.

  • GMT : Greenwich Mean Time
  • CST : Central Standard Time= GMT-5
  • EST : Eastern Standard Time = GMT-6
  • PST : Pacific Standard Time=GMT-7

Example
This example helps you in converting a date to  GMT, CST, EST and PST time on the console. The SimpleDateFormat() constructor uses the given pattern and date format symbols. Here we use the date format as gmtFormat for date format in GMT timezone same way can convert for CST, EST and PST. Then we have used getTimeZone() method to get the time of both the zones to be converted.

import java.util.*;
import java.text.*;

public class ConvertTimeZoneFromGMT {

public static void main(String[] args) {
//Current Date and Time
Date date = new Date();

//GMT Format
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("GMT Time: " + gmtFormat.format(date));

//CST Time
DateFormat cstFormat = new SimpleDateFormat();
TimeZone cstTime = TimeZone.getTimeZone("CST");
cstFormat.setTimeZone(cstTime);
System.out.println("CST Time: " + cstFormat.format(date));

//EST Time
DateFormat istFormat = new SimpleDateFormat();
TimeZone estTime = TimeZone.getTimeZone("EST");
estFormat.setTimeZone(estTime);
System.out.println("EST Time: " + estFormat.format(date));

// PST Time
DateFormat pstFormat = new SimpleDateFormat();
TimeZone pstTime = TimeZone.getTimeZone("PST");
pstFormat.setTimeZone(pstTime);
System.out.println("PST Time: " + pstFormat.format(date)); 

}
}

Output


GMT Time: 3/22/19 8:39 AM
CST Time: 3/22/19 3:39 AM
EST Time: 3/22/19 2:09 PM
PST Time: 3/22/19 1:39 AM