1. Creating Java String
The first thing one should know is that Java Strings are not a primitive data type. They are objects in Java. The String objects once constructed can not be changed. Strings like other programming languages stores texts. In this article we will see basic String Examples.
There are many ways to create a Java String. The below Example shows three ways of creating a String and printing it in the console output window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Main { public static void main(String[] args) { //Declaring and Initializing String Str1 = "First String"; //Constructing the string String Str2 = new String(); Str2 = "Second String"; //Constructing string from Char Array char Chr1[] = {'T','h','i','r','d',' ', 'S','t','r','i','n','g'}; String Str3 = new String(Chr1); System.out.println(Str1); System.out.println(Str2); System.out.println(Str3); } } |
Explanation
Line 6:
Here, we create a string and got reference to it in the variable Str1.
Line 9-10:
Here, we construct the Java String Str2 as an empty string and the string literal and then assign “Second String” string literal to it.
Line 12-15:
In this lines of code we create the array of Characters and store the reference in a variable Chr1. Then, we use this array to create a third Java String object and store its reference in str2.
The output of this First Example is below:

2. Java String Length
The length() method of the String object will return the length of the string. Have a look at the below Example:
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { public static void main(String[] args) { //001.1 Declaring and Initializing String Str1 = "First String"; //001.2 Length of the String System.out.println("Length:" + Str1.length()); } } |
This will print “Length:12” in the console output window.
3. Concatenating Java Strings
3.1 Using concat() method
We can concatenate a String in Java using the + operator or using concat() method of the String Object. We will see an example for both. Have a look at the example below:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { //Sample 1.0: Join String by Concat method String S1 = "Java "; String S2, S3; S2 = S1.concat("String "); S3 = S2.concat("Example"); System.out.println(S3); } } |
Note, in the above code we created a string called “Java” and have reference S1. Then we created one more string S2 by appending the “String” with S1 by calling the concat() method. The same way string S3 is created. The string S3 when printed gives – Java String Example as output.
Note: Concatenating string means joining them together
3.2 Using ‘+’ operator
Java does not allow changing the string object content after its construction. Hence, we created three strings and printed S3. The more easy we to join string is shown in the below example:
1 2 3 4 5 6 7 8 9 10 |
public class Main { public static void main(String[] args) { //Sample 1.0: Join String by Concat method String S1 = "Java "; String S2 = "String "; System.out.println(S1 + S2 + "Example"); } } |
Here, the + operator is used to join the strings S1 and S2 with the string literal “Example”.
4. Getting String of Characters
4.1 Getting single character from a Java String
The string is a merger of characters packed together. The charAt() method will help to get a specific character from the string. Below is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { //Sample 01: Take single char @ position String S1 = "Example String"; char c1 = S1.charAt(2); char c2 = S1.charAt(10); //Sample 02: Print Char System.out.println("Character at position 2:" + c1); System.out.println("Character at position 10:" + c2); } } |
In the above code, at line 7 the character position at 2 is retrieved and stored in c1. The c1 will have the letter “a” and the same way c2 will have “r”. The below picture shows the output of this example:

4.2 Getting Range of Characters From A Java String
The getChars() method of java string is useful to grab range of characters from the string. Now have a look at the below example:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Main { public static void main(String[] args) { //Sample 01: Take single char @ position String S1 = "This is an example String"; char[] chrarr = new char[7]; S1.getChars(11, 18, chrarr, 0); System.out.println(chrarr); } } |
Explanation
Line 6:
A String
S1 is created.
Line 7:
We create a New character array which can hold 7 elements.
Line 8:
The
getChars() method of String is called to fill the
chrarr array. The first two parameters tell position start and end in the Source String. Third parameter is the destination array which we want to fill. In our case, it is
chrarr. The fourth parameter says start location in the destination array.
The program will output: example
5. Getting Sub-String of a Given Java String
The substring() method is used to get subset of the characters from the string. The example for this is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Main { public static void main(String[] args) { //Sample 01: Take Sub-String String S1 = "This is an example String"; //Sample 02: Sub-String 1 String S2 = S1.substring(8, 18); //Sample 03: Sub-String 2 String S3 = S1.substring(11); System.out.println("Main String " + S1); System.out.println("Sub-String 1: " + S2); System.out.println("Sub-String 2: " + S3); } } |
Explanation
In this example, at line 9, we are making a call to the substring() function. We are passing two parameters which denote the character position of the string. The first parameter is starting location of the sub-string and the second parameter specifies the stop location. With these two parameters, the retrieved sub-string is stored in String S2.
At Line 12, we just pass the start location of the string and sub-string function knows that it should stop at end-of the string. Output of this example is below:

6. Searching for String
There are two methods which we can use in java to search a string. The method indexOf() is useful to find the first occurrence of a string. The method lastIndexOf() will find the last occurrence of the string. Both these methods will give position based index which tells found location of the java string. Now, look at the example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Main { public static void main(String[] args) { //Sample 01: Take Sub-String String S1 = "He Draws nice Drawings" + "Through DrawPad Application"; //Sample 02: Find First and Last Occurrence of draw int pos1 = S1.indexOf("Draw"); int pos2 = S1.lastIndexOf("Draw"); //Sample 03: Output the positions System.out.println("First occurrence" + " of Draw: " + pos1); System.out.println("Last occurrence" + "of Draw: " + pos2); } } |
Below is the output of the Example:
First occurrence of Draw: 3
Last occurrence of Draw: 30
Categories: Java
Tags: charAt, getChars, String Concat, String Length, substring