Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Variable Conversions

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 294 Times in 227 Posts
    Blog Entries

    Post Java Variable Conversions

    Here is a handy reference guide to variable conversions. Feel free to add your own.

    String to int
    String myString = "420";
    int myInt = Integer.parseInt(myString);
    String to double
    String myString = "42.0";
    double myDouble = Double.parseDouble(myString);
    String to long
    String myString = "1000000000";
    long myLong = Long.parseLong(myString);
    String to boolean
    String myString = "true";
    boolean myBoolean = Boolean.parseBoolean(myString);
    int to String
    int myInt = 420;
    String myString = new Integer(myInt).toString();
    int to double
    int myInt = 420;
    double myDouble = (double)myInt;
    int to boolean
    int myInt = 0;
    boolean myBoolean = (myInt != 0);
    ASCII code to String
    int myInt = 100;
    String myString = new Character((char)myInt).toString();
    double to String
    double myDouble = 0.42;
    String myString = Double.toString(myDouble);
    double to int
    double myDouble = 420.5;
    int myInt = (int)myDouble;
    char to String
    char myChar = 'A';
    String myString = Character.toString(myChar);
    boolean to String
    boolean myBoolean = true;
    String myString = new Boolean(myBoolean).toString();
    byte Array to String
    byte[] byteArray = new byte[] {74, 65, 86, 65};
    String value = new String(byteArray);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    Json (July 9th, 2009)

  3. #2
    Junior Member intelrate's Avatar
    Join Date
    Jun 2009
    Thanked 1 Time in 1 Post

    Lightbulb Re: Java Variable Conversions


    String.valueOf(...) is better for primitive types because there is no need to create new instance of wrapper Object.

  4. The Following User Says Thank You to intelrate For This Useful Post:

    JavaPF (June 11th, 2009)

  5. #3
    Junior Member
    Join Date
    May 2009
    Thanked 1 Time in 1 Post

    Default Re: Java Variable Conversions

    Very very very very very helpful thank you

    Here's my byteArray to string ^^
    byte[] byteArray = new byte[] { put, your, bytes, here };
    		String value = new String( byteArray );

    here's one to try:
    byte[] byteArray = new byte[] { 75, 111, 97, 107, 32,
    					       79, 119, 110, 115, 32,
    					       65, 108, 108 };
    		String value = new String( byteArray );
    		System.out.println( value );
    Last edited by Kok; June 23rd, 2009 at 05:07 AM.

  6. The Following User Says Thank You to Kok For This Useful Post:

    JavaPF (June 23rd, 2009)

Similar Threads

  1. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  2. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  3. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  4. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM

Tags for this Thread