• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to subtract two strings

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert String to int [premitive]
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use


to parse the value..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

return type of the valueOf is wrapper(Integer) .



if jdk1.5 is used it doesnt matter because of autoboxing
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



yes. true...
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still, Integer.parseInt is more efficient because it doesn't create a new Integer object, which Integer.valueOf will do.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic