Understand Command Line Arguments in java

In this example we are going to understand the command line arguments in java. We are going to write a very simple java class with the print statement and which will take one argument.

public class CmdArgs {

    public static void main(String[] args) {

        System.out.println("My name is: " + args[0]);
    }
}


In order to compile the above java class, go to the command prompt and navigate to the directory where the java class is stored. Now, issue the command to compile the java class as:
javac CmdArgs.java

Now, to run the java class, fire the following command.
java CmdArgs Amzi

The output will be something like this:

My name is: Amzi

Find out wheather the given number is palindrome or not.

Here we are going to slightly modify the previous program of reversing the number. By adding the new variable and if else condition, here we are going to determine whether the given number is palindrome or not.

import java.util.Scanner;

class ReverseNumber {
    public static void main(String args[]) {
        int original, originalCopy, reverse = 0;

        System.out.print("Enter the number to reverse: ");
        Scanner in = new Scanner(System.in);
        original = in.nextInt();
        originalCopy = original;

        while (original != 0) {
            reverse = reverse * 10;
            reverse = reverse + original % 10;
            original = original / 10;
        }

        if (originalCopy == reverse)
            System.out.println("The entered number is palindrome.");
        else
            System.out
            .println("The entered number is not a palindrome! Please try with different value like 1221 etc...");
    }
}


Output:

Enter the number to reverse: 123
The entered number is not a palindrome! Please try with different value like 1221 etc...

How to reverse any number using core java concepts

In this example we are going to write a java program which will enable us to enter any integer value using Scanner object. In while loop we are going to reverse the entered number.


import java.util.Scanner;

class ReverseNumber {
    public static void main(String args[]) {
        int original, reverse = 0;

        System.out.print("Enter the number to reverse: ");
        Scanner in = new Scanner(System.in);
        original = in.nextInt();

        while (original != 0) {
            reverse = reverse * 10;
            reverse = reverse + original % 10;
            original = original / 10;
        }

        System.out.println("Reverse of entered number is: " + reverse);
    }
}


Output:

Enter the number to reverse: 123
Reverse of entered number is: 321

Encode Decode example using Base64

This example will illustrate how to encode/decode the string value using the Base64 java class which is a part of apache commons package. If you are using maven as a build tool you can add the following dependency into your pom.xml file in order to compile your java class.

      <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.6</version>
      </dependency>


Java class to encode and decode any String value:

import org.apache.commons.codec.binary.Base64;

public class EncodeDecode {

    public static String base64Encode(String stringToEncode) {
        byte[] stringToEncodeBytes = stringToEncode.getBytes();
        return Base64.encodeBase64String(stringToEncodeBytes);
    }

    public static String base64Decode(String stringToDecode) {
        byte[] decodedBytes = Base64.decodeBase64(stringToDecode);
        return new String(decodedBytes);
    }

    public static void main(String[] args) {
        String testString1 = "Amzi 123";
        // Total bytes=8 divide by 3 and 2
        // bytes are in remainder so '=' is
        // appended in the encoded string
        String testString2 = "Amzi_12345"; 
        // Total bytes=10 divide by 3 and 1
        // bytes are in remainder so '==' is
        // appended in the encoded string
        String testString3 = "Amzi..";
        // Total bytes=6 divide by 3 and 0
        // bytes are in remainder so
        // nothing is appended

        String encoded1 = EncodeDecode.base64Encode(testString1);
        String encoded2 = EncodeDecode.base64Encode(testString2);
        String encoded3 = EncodeDecode.base64Encode(testString3);

        System.out
        .println("******** Encoded Strings *****************");
        System.out.println(testString1 + " > Encoded > " + encoded1);
        System.out.println(testString2 + " > Encoded > " + encoded2);
        System.out.println(testString3 + " > Encoded > " + encoded3);

        System.out
        .println("\n********** Decoded Strings ***************");
        System.out.println(encoded1 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded1));
        System.out.println(encoded2 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded2));
        System.out.println(encoded3 + " > Decoded > "
                + EncodeDecode.base64Decode(encoded3));
    }
}


Output:

******** Encoded Strings *****************
Amzi 123 > Encoded > QW16aSAxMjM=
Amzi_12345 > Encoded > QW16aV8xMjM0NQ==
Amzi.. > Encoded > QW16aS4u

********** Decoded Strings ***************
QW16aSAxMjM= > Decoded > Amzi 123
QW16aV8xMjM0NQ== > Decoded > Amzi_12345
QW16aS4u > Decoded > Amzi..