How to declare a String in Java? Example Tutorial

Hello guys, we are again with new article that is on Declaring String in Java. The main aim of this article is to give you idea about how to declare a string in java and about different ways of declaring.  Since String is a very common class in Java, as a Java developer you must know how to declare String variables and also understand the difference between String variable, String object, and String literals. In the past, I have explained difference between String object and String literal and in this article I am going to share multiple ways to declare String object in Java like creating a new String object using new() operator, concatenating String and using String literal to declare String variables in Java. 



How to declare String in Java?

Declaring a String in Java is a common task that is often done in many programming projects. Strings are used to represent text and are stored in objects. There are several  ways to declare a string in Java, but the most common method is to use the String class.


String class:
Here is how you can declare a string in Java:



Syntax

String str = "Hello World";




This creates a String object called str with the value of "Hello World."




New operator:

You can also declare a string using the new operator:




Syntax

String str = new String("Hello World");




This creates a new String object and assigns it to the variable str.


Concatenating strings in Java

In addition to declaring strings, you can perform operations on them such as concatenating strings, comparing strings, and finding the length of a string.



Examples:

String str1 = "Hello";

String str2 = "World";




// Concatenating strings

String str3 = str1 + " " + str2; // str3 = "Hello World"




// Comparing strings

boolean b = str1.equals(str2); // b = false




// Finding the length of a string

int length = str1.length(); // length = 5




It's important to note that strings are immutable in Java, meaning that once a string  is created, its value cannot be changed. However, you can create a new string  with a modified value.


How to declare a String in Java? Example Tutorial




Methods in String Class:

The String class has many methods for working with strings in Java. Some of the

most commonly used methods include:

charAt(int index): Returns the character at the specified index in the string.


concat(String str): Concatenates the specified string to the end of this string.


endsWith(String suffix): Returns true if the string ends with the specified suffix, false otherwise.


indexOf(int ch): Returns the index of the first occurrence of

the specified character in the string.

lastIndexOf(int ch): Returns the index of the last occurrence of the

specified character in the string.

length(): Returns the length of the string.


replace(char oldChar, char newChar): Returns a new string resulting from

replacing all occurrences of the specified character in this string with the new character.


startsWith(String prefix): Returns true if the string starts with the specified

prefix, false otherwise.

substring(int beginIndex): Returns a new string that is a substring of this

string. The substring begins at the specified beginIndex and extends to the end of the string.


toLowerCase(): Returns a new string that is a copy of this string with all

uppercase characters converted to lowercase.

toUpperCase(): Returns a new string that is a copy of this string with all

lowercase characters converted to uppercase.



Examples:

String str = "Hello World";




// Get the character at the specified index

char c = str.charAt(0); // c = 'H'




// Concatenate two strings

String str2 = str.concat("!!!"); // str2 = "Hello World!!!"




// Check if the string ends with a specific suffix

boolean b = str.endsWith("World"); // b = true




// Find the first occurrence of a specific character

int index = str.indexOf('o'); // index = 4




// Find the last occurrence of a specific character

int lastIndex = str.lastIndexOf('o'); // lastIndex = 7




// Get the length of the string

int length = str.length(); // length = 11




// Replace all occurrences of a specific character

String newStr = str.replace('o', '0'); // newStr = "Hell0 W0rld"




// Check if the string starts with a specific prefix

boolean startsWith = str.startsWith("Hello"); // startsWith = true




// Get a substring of the string

String substring = str.substring(6); // substring = "World"




// Convert the string to lowercase

String lowercase = str.toLowerCase(); // lowercase = "hello world"




// Convert the string to uppercase

String uppercase = str.toUpperCase(); // uppercase = "HELLO WORLD"



Visual Representation:


Here's a visual representation of how to declare a string in Java:

+---------+ +---------+

| "Hello" |---->| str |

+---------+ +---------+





In this diagram, "Hello" is the string literal, which is assigned to the variable str of

type String. The str variable is now a reference to the String object that contains

the string literal "Hello." It's also possible to declare a string using the new operator:

+---------+ +---------+

| "Hello" |---->| str |

+---------+ +---------+

|

| +---------+

| | "Hello" |

+--->+---------+





In this case, the new operator creates a new String object with the value "Hello," and the reference to that object is assigned to the variable str. It's important to note that strings are immutable in Java, meaning that once a string is created, its value cannot be changed. However, you can create a new string with a modified value.

For example, if you concatenate two strings:

+---------+ +---------+

| "Hello" |---->| str1 |

+---------+ +---------+




+---------+ +---------+

| "World" |---->| str2 |

+---------+ +---------+






+-----------------------+

| str3 |

+-----------------------+

| "Hello World" |

+-----------------------+





In this example, the str3 variable is assigned the result of concatenating the str1 and

str2 variables. This creates a new String object with the value "Hello World," and

the reference to that object is assigned to the variable str3.



Conclusion:
This visual representation helps to understand how strings are declared and manipulated in Java. The String class is a powerful tool for working with text in Java, and its methods provide a wide range of functionality for string manipulation. The String class provides a rich set of methods for working with strings in Java. Whether you need to manipulate strings, compare strings, or find information about strings, the String class has you covered. It is an essential part of Java programming and is widely used in many applications. Strings are an important aspect of Java programming and are widely used in many applications. Understanding how to declare and manipulate strings is an essential skill for any Java developer.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.