String class in Java
Tags: beginners, java, discuss, codenewbie
Published: 2021-07-11 09:43:49

Hello People😀 I hope you all are doing well. This short article will be introduction about String class in java.

## Let's begin...
### What is a String?
String is a collection of characters in sequence. In programming it is used to represent text rather than numbers. Numbers can also be considered as a string if they are specified correctly. To represent a string we enclose it in within quotes.

```java
phrase = Hello
``` 
This is not considered as a string.

```java
phrase = "Hello"
``` 
Since it is enclosed by quotes here this is a string.

```java
number = 1234
``` 
This is not considered as a string, this is considered as a number or to be more precise in programming this is considered as integer vale or **int**. All arithmetic operations can be performed on it.

```java
number = "1234"
``` 
Since it is enclosed by quotes so here this will be considered as a string.

**Remember we are talking about Java so please do not forget :-**
![semi-colon.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1625991084563/ZhRZONZJm.jpeg)
So in Java all those examples will look like:-

```java
phrase = "Hello";
//and
number = "1234";
``` 

### What is a String class?
In Java String is a class found in the java.lang package. String is a class and not a data type like int and char. The String class represents character strings. In Java Strings are treated as objects and all string objects in Java are the instance of this String class. Strings are immutable, once it is created a String object can not be changed. The String objects are backed internally by a char array it means an array of character in Java works same as a Java String. 

This will be more clear from the example:-
```Java
char[] ch = {'H','e','l','l','o'};  
String st = new String(ch);  
``` 
This works same as:-

```java
String st = "Hello";  
``` 
### How String is created?
There are two ways String is created:-

**1. Using literal syntax**

Create a variable of type String and directly assign it a value in quotes:-
```Java
String phrase = "Hello People";
``` 
Complete Code:-
```Java
public class StringDemo {
    public static void main(String []args) {
        String phrase = "Hello People";
        System.out.println(phrase);
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***
### Output:-
![string-in-java.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625994994630/3oBqSLgdp.png)
**2. Using new keyword syntax**

Create a new String object and add the literal in quotes.
```Java
String phrase = new String("Hello People");
``` 
Complete Code:-
```Java
public class StringDemo {
    public static void main(String []args) {
        String phrase = new String("This one using new keyword");
        System.out.println(phrase);
    }
}
```  
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***
### Output:-
![string-in-java-using-new.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1625995476190/uC1fFQ3TO.png)
#### There are many String methods which can be used to find length of string or to add two strings or to split a string or to change it's case etc, in next article we will talk about some most used String methods. Also there's lot left to explore about String but I wanted to keep it and introductory article, so ending it here. 

### Okay so that's enough for now follow my this journey to learn more about Java.
#### *Thank you for reading.*
#### *Please share your thoughts about it and correct me if I'm wrong.*
#### *I hope you liked it and found it helpful.*
#### ***Cover:- [Rajat Gour](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*