---
title: "Converting String to int in Java?"  
description: "Converting String to int in Java?"  
author: "Manoj Bhatt"  
published: 2015-05-07  
updated: 2015-05-07  
canonical: https://www.mindstick.com/forum/23195/converting-string-to-int-in-java  
category: "java"  
tags: ["java", "string"]  
reading_time: 1 minute  

---

# Converting String to int in Java?

How can a String be converted to an [int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?\
My [String contains](https://www.mindstick.com/forum/159365/how-to-check-if-a-string-contains-letters-only-in-sql) only numbers and I want to return the number it represents.\
For example, given the string "1234" the [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency) should be the number 1234.

## Replies

### Reply by Anonymous User

```
Integer x = Integer.valueOf(str);// orint y = Integer.parseInt(str);
```

There is a slight difference between these methods:\
valueOf returns new instance of java.lang.IntegerparseInt returns primitive int.The same is for all cases: Short.valueOf/parseShort, Long.valueOf/parseLong, etc.


---

Original Source: https://www.mindstick.com/forum/23195/converting-string-to-int-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
