Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

convert String variable to same name int variable  RSS feed

 
Ranch Hand
Posts: 600
  • 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?
 
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.
 
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!