---
title: "How many ways we can create the string object?"  
description: "How many ways we can create the string object?"  
author: "Anonymous User"  
published: 2015-04-25  
updated: 2020-09-23  
canonical: https://www.mindstick.com/interview/2436/how-many-ways-we-can-create-the-string-object  
category: "java"  
tags: ["java", "string"]  
reading_time: 2 minutes  

---

# How many ways we can create the string object?

There are two ways to create String object:

**1)By string literal**

```
String s="welcome"; 
```

Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool.\
\
**2)By new keyword**In such case, JVM will create a new string object in normal(non pool) heap memory and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in heap(non pool).\
\

```
String s=new String("Welcome")
```

## Answers

### Answer by Anonymous User

There are two ways to create String object:

**1)By string literal**

```
String s="welcome"; 
```

Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool.\
\
**2)By new keyword**In such case, JVM will create a new string object in normal(non pool) heap memory and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in heap(non pool).\
\

```
String s=new String("Welcome")
```


---

Original Source: https://www.mindstick.com/interview/2436/how-many-ways-we-can-create-the-string-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
