This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to subtract two strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to subtract two strings" Watch "how to subtract two strings" New topic
Author

how to subtract two strings

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I am having a string variable,say
String a="30"
and another string variable
String b="1"
.

Now i need to subtract 1 from 30.How do i subtract these two things.
please assist me to do this.

Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Convert String to int [premitive]
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
you can use


to parse the value..


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Harshit Rastogi wrote:you can use


to parse the value..


return type of the valueOf is wrapper(Integer) . this wont be much flexible in terms of mathematical operations .

if i were in your place, i would go for

Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
return type of the valueOf is wrapper(Integer) .


if jdk1.5 is used it doesnt matter because of autoboxing
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Harshit Rastogi wrote:if jdk1.5 is used it doesnt matter because of autoboxing


yes. true...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Still, Integer.parseInt is more efficient because it doesn't create a new Integer object, which Integer.valueOf will do.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Rob Prime wrote:Still, Integer.parseInt is more efficient because it doesn't create a new Integer object, which Integer.valueOf will do.

The method Integer.valueOf(int i) uses a cache if i is between -128 and 127, so it doesn't create a new object if the value is between those boundaries. So I thought you were incorrect when you said that Integer.valueOf(String s) always creates a new object. I checked out the source code of class Integer (available in src.zip in the JDK directory), and to my surprise saw this:

So indeed, the method that takes a String always creates a new Integer object - you were right.

I wonder why this is - why did the people who programmed the JDK not write it like this?

Strange! But you know, the standard Java library isn't perfect...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to subtract two strings
 
Similar Threads
date arithmetic
How to convert hours,minutes,seconds to seconds
Subtract Minutes from a DateTime object
Suggestions to exact variable length dates out of a string?
Date formatting and time calculations.