Java String, Methods And Constructor – Syntax and Example

1. Objective

In our last session, we discussed Java Character Class. Here, we will explore the three terms of Java called, Java String, Java Methods and Java Constructor. Moreover, we will learn various types of strings in Java and how to declare java string. Along with this, we cover other two terms with their syntax and examples.

So, let’s start Java Tutorial.

Java String - Introduction

Java String, Methods And Constructor – Syntax and Example

2. What is the Java String?

Strings in Java are a collection of Java characters and they are immutable, i.e. they cannot be changed once created.

There are two ways to create a Java string.

a. Using a string literal in Java

String s = “DataFlair”;

b. Using a new Java keyword

String s = new String (“DataFlair”);

Do you what are literals in Java?

3. Constructor in Java

There are various types of Java constructors. Let us see them one by one:

Constructor in Java - Java String

Java String Tutorial – What is Constructor in Java

a. Java String(byte[] byte_arr)

It constructs a new string by decoding of the byte array using the default Java character set of the platform for decoding.
Example

byte[] b_arr = {81, 101, 101, 107, 115};
String s_byte =new String(b_arr);

b. Java String(byte[] byte_arr, Charset char_set)

It constructs a new string by decoding of the byte array, it uses the char_set for decoding.
Example

byte[] b_arr = {81, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s_byte_char = new String(b_arr, cs);

c. Java String(byte[] byte_arr, String char_set_name)

It constructs a new string by decoding of the byte array using the char_set_name of the platform for decoding.
Example

byte[] b_arr = {68, 101, 101, 107, 115};
String s = new String(b_arr, "US-ASCII");

d. Java String(byte[] byte_arr, int start_index, int length)

It constructs a new string by decoding of the byte array depending on the start_index(Starting location) and length(number of characters from starting location), it used char_set for decoding.
Follow this link to know more about Constructors in Java Programming Language
Example

byte[] b_arr = {68, 101, 101, 107, 115};
Charset cs = Charset.defaultCharset();
String s = new String(b_arr, 1, 3, cs);

e. Java String(byte[] byte_arr, int start_index, int length, String char_set_name)

It constructs a new string by decoding of the byte array depending on the start_index(Starting location) and length(number of characters from starting location), it uses char_set_name for decoding.
Example

byte[] b_arr = {71, 101, 101, 107, 115};
String s = new String(b_arr, 1, 4, "US-ASCII");

f. Java String(char[] char_arr)

This method allocates a new string from the Java character array.
Example 

char char_arr[] = {'d', 'o', 'g', 'k', 's'};
String s = new String(char_arr);

g. Java String(char[] char_array, int start_index, int count)

This method allocates a string to the given character array, the only difference is that it chooses count characters from the start index.
Example 

char char_arr[] = {'w', 'e', 'e', 'k', 's'};
String s = new String(char_arr , 1, 3);

h. Java String(int[] uni_code_points, int offset, int count)

This method allocates a string from a uni_code_array but chooses the character from start_index.
Example

int[] uni_code = {61, 101, 101, 107, 115};
String s = new String(uni_code, 1, 3);

i. Java String(StringBuffer s_buffer)

This method allocates a new method to the java string in s_buffer.
Example

String s_buffer = "dogs";
String s = new String(s_buffer);

j. Java String(StringBuilder s_builder)

This method allocates a new method to the string in s_builder.
Example

String s_builder = "dogs";
String s = new String(s_builder);

Follow this link to know about Java Operators

4. Java String Methods

Java String Methods

Java String Methods

a. Java int length()
This method returns the number of character in the string.
Example

"Shadow".length();

b. Java Char charAt(int i)
This java method returns the character at Ith index.
Let’s See StringBuffer In Java | StringBuffer Constructor and Methods
Example

"Shadow".length();

c. Java String substring (int i)
This method returns the ith index substring.
Example

"Shadow".substring(3);

d. Java String Substring (int i, int j)
This method in java returns the substring from i to j-1 index.
Example

"Shadow".substring(2, 5);

e. Java String concat( String str)
This method concatenates the specified string to the end of this string.
Example

String s1 = ”Shadow”;
String s2 = ”isfive”;
String output = s1.concat(s2);

f. Java int indexOf (String s)
This method in java returns the index within the string of the first occurrence of the specified string.
Example

String s = ”Shadow is Five”;
int output = s.indexOf(“Five”);

g. Java int indexOf (String s, int i)
This method returns index within the string of the first occurrence of the specified string, starting at the specified index.
Example

String s = ”Shadow is Five”;
int output = s.indexOf(‘a’,3);

h. Java Int lastindexOf( int ch)
This java method returns the index within the string of the last occurrence of the specified string.
Let’s See StringTokenizer In Java | Constructors, and Methods
Example

String s = ”Shadow is five”;
int output = s.lastindexOf(‘a’);

i. Java boolean equals( Object otherObj)
This method compares this string to the specified object.
Example 

Boolean out = “shadow”.equals(“shadow”); // returns true
Boolean out = “shadow”.equals(“Shadow”); // returns false

j. Java boolean equalsIgnoreCase (String anotherString)
This method compares string to another string, ignoring case considerations.
Example

Boolean out= “shadow”.equalsIgnoreCase(“shadow”); // returns true
Boolean out = “Shadow”.equalsIgnoreCase(“shadow”); // returns true

k. Java int compareTo( String anotherString)
This method compares two string lexicographically.
Example 

int out = s1.compareTo(s2);
// where s1 ans s2 are strings to be compared
This returns difference s1-s2. If :
out < 0  // s1 comes before s2
out = 0  // s1 and s2 are equal.
out >0   // s1 comes after s2.'

l. Java int compareToIgnoreCase( String anotherString)
This method compares two string lexicographically, ignoring case considerations.
Example

int out = s1.compareToIgnoreCase(s2);
// where s1 ans s2 are strings to be compared
This returns difference s1-s2. If :
out < 0  // s1 comes before s2
out = 0   // s1 and s2 are equal.
out >0   // s1 comes after s2.

Have a look at Java Functional Interface | Lambda Expression in Java
m. String toLowerCase()
This method converts all the characters in the String to lower case.
Example

String word1 = “Hi”;
String word3 = word1.toLowerCase();

n. Java String toUpperCase()
This method converts all the characters in the String to upper case.
Example 

String word1 = “Hi”;
String word2 = word1.toUpperCase();

o. Java String trim()
This method returns the copy of the String, by removing whitespaces at both ends. It does not affect whitespaces in the middle.
Example

String word1 = “ shadow is five “;
String word2 = word1.trim();

p. Java String replace (char oldChar, char newChar)
This method returns new string by replacing all occurrences of oldChar with newChar.
Example

String s1 = “shadowisfive “;
String s2 = “shadowisfive”.replace(‘f’ ,’g’);

So, this was all about Java String Tutorial. Hope you like our explanation on String Methods and Constructor in java.

5. Conclusion

In this tutorial for Java String, we learned the basics of strings in Java, various constructors in Java and various Java string methods used to declare java strings with examples. This will help understand the Java language better and program better. Furthermore, if we miss something? Feel free to share with us!
See Also- POJO Class in Java
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *