Java Strings and Command Line Arguments

In general, a string is a collection of characters. But in the case of Java, strings are objects of class String. We will discuss class and objects later. This makes strings so powerful in Java. We have used strings in all our previous programs in System.out.println function.

System.out.println("Java strings are powerful");

Defining Strings

To define a string we need to use the String class as its data type. Below is one of the ways to define strings.

String str1 = new String("How are you?");

The above statement creates a string str1 with value as ‘How are you?’. There is another way of defining a string and it is very easy one.

String str2 = "I am fine";

Operations on Strings

Java provides several methods to work on strings. These are defined in String class. It is not possible to explain all these methods here, but we will discuss frequently used methods.

boolean equals (str)

It checks whether invoking string and input string ‘str’ contains same characters or not. If both contains same characters, it returns true and otherwise it return false.

int length()

Returns the length of strings, that is the number of characters present in invoking string.

char charAt (index)

Returns the character at specified index in the string.

int compareTo (str)

It returns a negative integer if invoking string is less than input string str, returns positive number is invoking string is greater than input string str and returns zero if both are equal.

int indexOf (str)

This method searches for input string in invoking string and return starting index of input string if exists otherwise return -1.

int lastIndexOf (str)

It works same as indexOf method, the only difference is that it returns the index of the last occurrence of the substring. If substring not found returns -1.

Concatenating Strings

Sometimes we need to append one string to another string. This can be achieved by ‘+’ operator. Below program completely explains these operators on strings.

This is a simple Java program about strings in Java.
Call this file
import java.util.*;

public class KH_Strings {
	// A Java program begins with a call to main().
	public static void main(String args[]) 
		String str1 = new String("This is Java");
		String str2 = "Java strings are objects and Java strings are powerful";
		String str3 = str1 + str2;
		int idx = 0;
		int result = 0;
		System.out.println("Length of string: " + str1.length());
		System.out.println("Character at index 3: " + str1.charAt(3));
			System.out.println("Both strings are same.");
			System.out.println("Both strings are not same.");
		result = str2.compareTo(str1);
		if(result == 0)
			System.out.println("str1 and str2 are equal");
		else if(result < 0)
			System.out.println("str1 is less than str2");
			System.out.println("str1 is greater than str2");
		idx = str2.indexOf("Java");
		System.out.println("Index of first occurrence of Java: " + idx);
		idx = str2.lastIndexOf("Java");
		System.out.println("Index of last occurrence of Java: " + idx);

		System.out.println("Concatenated string: " + str3);

Download the code  Run the code


Length of string: 12
Character at index 3: s
Both strings are not same.
str1 is less than str2
Index of first occurrence of Java: 0
Index of last occurrence of Java: 29
Concatenated string: This is JavaJava strings are objects and Java strings are powerful

Using Command-line Arguments

Many programs accept inputs while executing along with program name. These inputs are called command-line arguments. Now we understood strings in Java, we can look into String args[] in main function. Where args is an array of strings. This holds inputs we passed with the program name. The below program demonstrates this.

This is a simple Java program about strings in Java.
Call this file
import java.util.*;

public class KH_CommandLineArguments {
	// A Java program begins with a call to main().
	public static void main(String args[]) 
		System.out.println("Total arguments passed are " + args.length);
		for(int i = 0; i < args.length; i++)
			System.out.println("Input " + (i+1) + ": " + args[i]);

Download the code Run the code

To execute above program, use below line, where after program name we have passed 3 inputs they are ‘one’, ‘two’, and ‘three’.

java KH_CommandLineArguments one two three

args will store these inputs. Hence, the out will be,

Input 1: one
Input 2: two
Input 3: three
<<< Operator Precedence and Operators ExampleControl Statements in Java >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .