• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert String variable to same name int variable  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
i wander if there any method to deal with this kind of dilemma
say i have :


i like to add boo to foo and keep the value in foo BUT also that foo will become int variable data type
and hold the numeric value .
can it be done in java?
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this:

String s = String.valueOf(4);
System.out.println(s);
int i = Integer.parseInt(s);
System.out.println(i);


But why don't you just keep all of your variables as ints? They can be printed as strings.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, in Java variable types can't be changed, they are fixed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!