java

How to convert variable values in java

In most cases we want to convert our variable values in to other format. We can use cast operator for do this task. You can see some examples below

public class StringToInt {
  public static void main(String[] args) {
    String mystring = "78"; // current variable have a string type value
    int myint = Integer.parseInt(mystring); // convert that value in to integer type

    System.out.println(myint);// print it
  }
}

Now we will see how to convert Double value to string type

public class DoubleToString {
  public static void main(String[] args) {
    double myDouble = 0.11;
    String myString = Double.toString(myDouble);

    System.out.println(myString);
  }
}

Same as you can use to convert any type value to another type

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s