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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Casting and Data Type Conversion examples

  1. #1
    Junior Member tonu's Avatar
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Casting and Data Type Conversion examples

    Casting and Data Type Conversion examples
    	String s = "56"; // no letters inside
    	byte by = Byte.parseByte(s); //56
    	short sh = Short.parseShort(s); //56	
    	int i = Integer.parseInt(s); //56	
    	Integer i = Integer.valueOf(s);
    	long lo = Long.parseLong(s); //56		
    	float f = Float.parseFloat(s); //56.0
    	double d = Double.parseDouble(s); //56.0
    	char c = s.charAt(0); //5
    	boolean bo = Boolean.parseBoolean(s); //false

    	byte by = 1; 
    	short sh = 2; 
    	int i = 3; 
    	long lo = 4; 	
    	float f = 5.6F; 
    	double d = 66.8F;
    	char c = 's';
    	boolean bo = false;
     
    	String s = by+""; // 1
    	String s = Byte.toString(by);
    	String s = String.valueOf(by); // (i) ,(sh), (d) etc
    	String s = Short.toString(sh);
    	String s = Integer.toString(i);	
    	String s = Long.toString(lo);
    	String s = Float.toString(f); // 5.6
    	String s = Double.toString(d); // 66.80000305175781
    	String s = Character.toString(c); // s
    	String s = Boolean.toString(bo); // false

    long x = (long)i;
    float ff = (float)5;
    double ff = (double)3.55;
    char ff = (char)3.55;


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Casting and Data Type Conversion examples

    What's the question? Or is this a tutorial?

    Edit: Duh! I see I just woke up in the tutorial section. It might be helpful if you were to explain what the above examples do or show and how you think that might be helpful to someone else.

  3. #3
    Junior Member tonu's Avatar
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Casting and Data Type Conversion examples

    it is a tutorial, just summerized to learn by heart

Similar Threads

  1. Object type casting in java
    By kinu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2013, 04:06 PM
  2. type conversion error
    By Xeenix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 07:29 PM
  3. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  4. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM

Tags for this Thread