Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to concatenate two strings together in Java source code

Introduction :-
In Java, Concatenating two strings together is of high importance. Java programmer who is a beginner must know how to concatenated two string ?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary to get an idea for writing an efficient code. Our program code in this leaf will need Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.Java provides many mechanism to concatenated two strings. I will be discussing two of the mechanism here.

1. String concatenate operator (+).
2. String class method concat().

So to learn Java effectively , you should know these two mechanism to concatenate two strings. Let us begin with both the methods by an example and its explanation.
Concatenation of two Strings Explanation :-
By Concatenation of two Strings we mean that adding or appending one string to another string. We can also state that it is adding of two strings together. Concatenation of strings comes handy when dealing with the strings in a complex code. Generally concatenation of strings can occur in many ways. Most of the mechanism used is been coded into the sample code below. Let us look into the code step-by-step and see what it does?.


Program to demonstrate working of String Concatenation

public class Concatenation { 
    public static void main(String[ ] args) { 
        String ruler1 = " a ";
        String ruler2 = ruler1 + "b" + ruler1;
        String ruler3 = ruler2 + "c" + ruler2;
        String ruler4 = ruler3 + "d" + ruler3;
        
        System.out.println(ruler1);
        System.out.println(ruler2);
        System.out.println(ruler3);
        System.out.println(ruler4);
               
        String ruler5 = " 1 ";
        String ruler6 = "2";
        String ruler7 = ruler5.concat(ruler6);
        System.out.println(ruler7);
    }
}

Output and Explanation of working of above sample code :-
Above program shows us the working of string concatenation operator + and String class concat method. When we run above program we get output as:
a
a b a
a b a c a b a
a b a c a b a d a b a c a b a
1 2
Line 4 to 6:- All the three lines shows the code how + operator is used to concatenate the strings together. Line 4 code String ruler2 = ruler1 + "b" + ruler1; takes ruler1 string value which is " a " appends value "b" to it. So our new value becomes " a b". Now our code takes value " a b" and appends " a " to it. So after line 4 gets completely executed ruler2 gets value as " a b a ". Same execution is done for line 5 and 6.

Line 15:- This line shows us the code for the String method called as concat(). This method takes one string as an argument and appends it to the value of the string calling that method. So in our case ruler5.concat( ruler6 ) takes ruler5 value and appends ruler6 value to it and assign that value to ruler7. In our case it takes " 1 " and appends "2" to it. Finally ruler7 gets the value as " 1 2".

Line 8 to 11 and Line 16:- All these 5-6 lines just prints the values of ruler1, ruler2, ruler3, ruler4 and ruler7 on the console.

Command-Line Arguments in Java programming language


Introduction :-

Hello friends, having question in mind, How to use Command-Line Arguments in Java ?. In this hub I will be teaching you how to utilize java command-line utilities?. Learning Java by example is the most effective proven technique. Java fundamentals are necessary in order to get an idea for writing a efficient code. Our program code in this hub will require Java's basic knowledge such as : How to write your first Java program ?. and how to create, compile and run a java program ?.




Click here to download complete source code 

Command-Line Argument Explanation :-
 
A normal Java application before running, can accept multiple command line arguments from the programmer via console or IDE runtime configuration. The arguments are specified by the user at the command line which are required by the Java applications. The user enters command line arguments while launching the program by java command. This passing of arguments by java command is optional. The main method gets arguments from command line and stores it in an array of String. The array elements are initialized by whatever user types after class-name in java command. Let us take an example, suppose we want to launch a class say Hubpages.java with command line arguments, we type command after compilation as :

java Hubpages I am hubber
 
Here Hubpages is the name of class we want to launch, "I", "am", "hubber" would be three String passed to program's main method, which will store all three in the form of array of Strings. Generally, space between I, am and hubber makes them separate strings. If we want them to be one string we use double quotes "". In our example we can pass it as one single string as "I am hubber". Let us look at the below sample code and try to figure out what would be the output.

Program to demonstrate working of Command-Line Arguments

public class Argument {
   public static void main(String[ ] args) {
        System.out.print("Hi, ");
        System.out.print(args[0] + " " );
        System.out.print(args[1] + " " );
        System.out.println(". How are you?");
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument Java Programs We get the following output on the console window:
Hi, Java Programs . How are you?
Here we pass two command-line arguments "Java" and "Programs". These two strings are passed as an argument to main method and gets stored in the form of an array. Where args[0] is assigned "Java" value and args[1] is assigned "Programs" value. These values are printed on console by System.out.print method.

Parsing Numeric Command-Line Arguments :- 

Suppose the programmer wants to enter a numeric value on command-line and let say for an example he inputs 10. Now the problem in this case is that though he had input value as 10 which is a numeric value of data type int, main method will store it as a string which is equal to "10". In order to use numeric value in program we need to parse string tointeger value. To do parsing we use Integer class which has method called as parseInt. This is static method and has argument which is a String. This method takes the String value and parses to numeric type which we can use in our program numerically. Let us look into through sample code below :

public class Argument {
   public static void main(String[ ] args) {
        
       int number1 = Integer.parseInt(args[0]);
       int number2 = Integer.parseInt(args[1]);


       int sum = number1 + number2;
       
       System.out.print("The sum of two number is : " + sum);
       
    }
}


Output and Explanation of working of above sample code :-

When we run above program by command say : java Argument 10 20 We get the following output on the console window:
The sum of two number is : 30
Here we pass two command-line arguments "10" and "20". Remember these two values are strings and they need to be first convert into numeric value. For this we have used Integer class method called as parseInt. It takes string and parses it to integer value. If value provided to this method is not a number, we get an exception such as NumberFormatException.

How to convert Decimals to Hexadecimals in a Java program ?

Introduction :- 

Hello friends, Today let us begin with a new Java program. Many of serious programmers who are just beginning to code in Java, come across a problem that is, How to convert Decimals to Hexadecimals in a Java program. Learning Java by an example is the most effective technique to inculcate this versatile language. Generally, whatever you know about language its additional when you have power to code small snippets. Learning by coding will not only give you command over the language but also develop the skills in you to code successfully.Hexadecimals are often used in computer systems programming.

package com.hubberspot.conversion.example;

import java.util.Scanner;

public class DecimalToHexConversion {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter decimal number you like: ");
    int deci = input.nextInt();

    System.out.println("The hexadecimal number for decimal " + 
  deci + " is " + convert(deci));
  }

  public static String convert(int decimal) {
    String hex = "";

    while (decimal != 0) {
       int hexValue = decimal % 16; 
       hex = toHexChar(hexValue) + hex;
       decimal = decimal / 16;
    }

    return hex;
  }

  public static char toHexChar(int hexValue) {
    if (hexValue <= 9 && hexValue >= 0)
       return (char)(hexValue + '0');
    else  
       return (char)(hexValue - 10 + 'A');
  }
}

Output of the program : 






Video tutorial to demonstrate how to convert Decimal number to Hexadecimal number in Java.








 
© 2021 Learn Java by Examples Template by Hubberspot