What are Strings in Java?
The Strings in Java are used for storing the text.
A String isn't a primitive data type in a Java as i told you in my previous tutorial.It is the class in the java.
A String variables contains a collection(or an array) of characters surrounded by double quotes.
In this tutorial, you will learn-
- What are Strings in Java?
- String Syntax
- String length
- String Concatenation
- Important Java string methods
String Syntax :
<String> <variable_name> = "string or sequence_of_characters";
NOTE In java,objects of string are immutable(constant) ,which means string cannot be changed once it's created.
There are two ways to create string in java :
There are two ways to create string in java :
1. String literal
String s = “CodingAliens”;
2. Using new Keyword
String s = new String (“CodingAliens”);
String Length :
A String in Java is an object of string class as i told you, which contains methods that can perform certain operations on strings.The length of a string can be found with the
length() method present inside the string class:String s = “CodingAliens”;
int length = s.length();
String Concatination:
Concatination = joining of 2 or more strings.
The concat() method is used to combine or join the strings.
NOTE In java, + operator is used for both adding the values as well as concatinate the strings.
String s1 ="Coding"; String s2 = “Aliens”;
String s3 = s1.concat(s2); //or s3 = s1 + s2 ;
0 Comments