Forums Register Login

how to subtract two strings

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Convert String to int [premitive]
+Pie Number of slices to send: Send
you can use


to parse the value..
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
 

return type of the valueOf is wrapper(Integer) .



if jdk1.5 is used it doesnt matter because of autoboxing
+Pie Number of slices to send: Send
 

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



yes. true...
+Pie Number of slices to send: Send
Still, Integer.parseInt is more efficient because it doesn't create a new Integer object, which Integer.valueOf will do.
+Pie Number of slices to send: Send
 

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...
My cellmate was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 13852 times.
Similar Threads
Date formatting and time calculations.
How to convert hours,minutes,seconds to seconds
Suggestions to exact variable length dates out of a string?
date arithmetic
Subtract Minutes from a DateTime object
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:44:19.