Technical

What is a Java String?

Java String


Every person needs a language to communicate with other people. Similarly, computers also need a language to communicate with the system. A computer programming language is a way of communication for instructing a computer or computing device to perform some specific tasks. The programming language refers to high-level languages such as C, C++, HTML, Java and so on.

Java is a popular programming language, invented in 1995. It is owned by Oracle.

The applications in which Java is used are as follows:

  • Mobile applications
  • Desktop applications
  • Games
  • Webserver
  • Web applications and so on.

Java works on different platforms such as Windows, Linux, Mac, Raspberry Pi, etc. It is easy and simple to learn and use. It is an open and free source programming language. It is a secure, fast and powerful language. It is an object-oriented language that gives a clear structure to programs and allows code to be reused, lowering development cost.

In Java, String is actually an object which contains methods that can perform some operations. Strings are also used for storing the text.

A variable ‘String’ contains a collection of characters that are surrounded by double-quotes.

For Example String greeting = “ Hello World”;

 

How to find the length of a string?


In Java, we can also find the length of strings by using ‘ length()’.

For Example : String txt = “ xyzlmnopabcqrst”;

System.out.println(“ The length of txt string is : + txt.lenght()”);

 

String Methods


There are many string methods available such as, toUpperCase() and toLowerCase().

The toUpperCase() will be used for all capital alphabets and toLowerCase() will be used for all smaller alphabets.

For Example : String txt = “ Java Learning”;

System.out.println(txt.toUpperCase()); // Output : “JAVA LEARNING”

System.out.println(txt.toLowerCase());  // Output : “java learning”

 

How to find a Character in a String?


The indexOf() method returns the position of the first occurrence of a specified text in a string. (including white space)

For Example String txt = “ Please locate where ‘find’ occurs”;

System.out.println(txt.indexOf(“find”));

Outputs: 7

Java counts position from zero. 0 is the first position of String, 1 is the second position and so on.

 

String Concatenation


In this method, we will use the ‘+’ operator between strings to combine them.

For Example : String firstname = “ xyz” ;

String lastname = “ pqr”;

System.out.println(firstname + “ “ + lastname);

 Output = xyz pqr

 

Addition of Strings


In Java, + operator used in both addition and concatenation. But, if you add two numbers the addition will take place and if you add two strings the concatenation will take place.

For Example : For numbers,

int x = 10;

 int y = 30;

int z = x + y;

Output = 40

For Strings,

String x= “10”;

String y =”30”;

String z= x + y;

Output = 1030

About the author

Mayuri Bhoyar

Leave a Comment