DEV Community

Cover image for String in Java
Hariharan S J
Hariharan S J

Posted on

String in Java

1.What is mean by String in Java?

Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes (""):

(https://www.w3schools.com/java/java_strings.asp)

A String in Java is an object used to store a sequence of characters enclosed in double quotes. It uses UTF-16 encoding and provides methods for handling text data.

  • Each character in a string is stored using 16-bit Unicode (UTF-16) encoding.

  • Strings are immutable, meaning their value cannot be changed after creation.

(https://www.geeksforgeeks.org/java/strings-in-java/)

2.String Methods

String length()

It will Show the total characters in a one String Variable

the main work of the String length() method is to return the total length Characters present inside a String

the return datatype of the length method is int

Here by using the length() method we can calculate the total Characters of the variable food which is stored inside a datatype string

the output of this code is 8

to convert full String to a capital letter or full String to a small letter we have a method called

toUpperCase()

toLowerCase()

when we use toUpperCase() Method the String will be automatically converted into Capital Letters

when we use toLowerCase() Method the String will be automatically converted into Small Letters

the output of the code will be

BIRIYANI //toUpperCase()
biriyani //toLowerCase()
Enter fullscreen mode Exit fullscreen mode

Finding a Character in a String

The indexOf() method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace):

the return datatype of this method is int

Java counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third ...

the output of the code will be

5
Enter fullscreen mode Exit fullscreen mode

The return datatype of this method is char

Now in the String food = "Biriyani"; i want to take the word riya from this

Now here comes the super method

substring()

syntax of this method

substring(beginIndex , endIndex)
beginIndex -> included
endIndex -> excluded
Enter fullscreen mode Exit fullscreen mode

The substring() method is used to extract (or retrieve) a portion of a string.

It returns a new String containing the selected characters from the original string.

The return datatype of this method is String

the output of the code will be Biri

Now guess the output of this

There are two overloaded substring() methods.

one is substring(int beginIndex, int endIndex)

Starts from beginIndex and stops before endIndex.

the another one is substring(int beginIndex)

Starts from the given index and returns characters up to the end of the string.

So the output of the code is iyani

Comparing Strings

To compare two strings, you can use the equals() method:

the return datatype of this method is a boolean it tells true or false

The output of the code will be true

Removing Whitespace

The trim() method removes whitespace from the beginning and the end of a string:

the return datatype of this method is String

the output will be

Before: [   Hello World   ]
After: [Hello World]
Enter fullscreen mode Exit fullscreen mode

String Concatenation

The + operator can be used between strings to combine them. This is called concatenation

Output

Hariharan Sivasankar
Enter fullscreen mode Exit fullscreen mode

The concat() Method

You can also use the concat() method to concatenate strings:

the return datatype of the concat method is String

String firstName = "Hari ";
String lastName = "Bavan";
System.out.println(firstName.concat(lastName));
Enter fullscreen mode Exit fullscreen mode

output

Hari Bavan
Enter fullscreen mode Exit fullscreen mode

Adding Numbers and Strings

In Java uses the + operator for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

int x = 10;
int y = 20;
int z = x + y; 
System.out.println(z);
Enter fullscreen mode Exit fullscreen mode

output

30
Enter fullscreen mode Exit fullscreen mode

If you add two strings, the result will be a string concatenation:

String x = "45";
String y = "18";
String z = x + y;
System.out.println(z);
Enter fullscreen mode Exit fullscreen mode

output

4518// (a string)
Enter fullscreen mode Exit fullscreen mode

If you add a number and a string, the result will be a string concatenation:

String x = "7";
int y = 3;
String z = x + y;
System.out.println(z);
Enter fullscreen mode Exit fullscreen mode

output

73// (a string)
Enter fullscreen mode Exit fullscreen mode

(https://www.w3schools.com/java/java_strings_numbers.asp)

Top comments (2)

Collapse
 
buddingdeveloper profile image
Ebenezer

Thanks for this blog.can you explain why string uses utf 16 encoding? What is the need for that? Already there is + operator to join the strings what's the difference between + and concate operator?

Collapse
 
hariharan_sj_2003 profile image
Hariharan S J

Great Question and Thanks for noticing that point the first answer for your question is

Computers can only understand binary (0s and 1s), so every character needs to be converted into a numeric format. This process is called character encoding.

Earlier, ASCII was used, but it could represent only 128 English characters. As software started supporting multiple languages like Tamil, Hindi, Chinese, Japanese, and even emojis, a better standard was needed. That's why Unicode was introduced.

Java uses UTF-16 as the internal representation for String because it provides excellent support for Unicode characters. Most commonly used characters can be represented using a single 16-bit code unit, allowing Java applications to work with text from many languages without worrying about different encodings.

Although modern versions of Java optimize string storage internally, from a developer's perspective, Java Strings are still based on UTF-16.

the second answer for your question is

the difference between the + operator and the concat () method is in + operator it automatically converts other data types to string whereas the concat method accepts only string arguments

in + operator we can concatenate numberss , characters , boolean , etc whereas in concat method we can concatenate only string objects

on + opperator it will be More readable for simple concatenation whereas in concat method it is useful when you specifically want to concatenate strings