How to Convert Double Object to primitive data types

/*
 * Here we will learn to convert Double Object to primitive data type.
 */
public class ConvertDoubleObjToPri {
	public static void main(String[] args) {

		String strObj = "125478";

		/**
		 * Double class has a overloaded constructor with string as argument
		 * which constructs a newly allocated Double object that represents the
		 * floating-point value of type double represented by the string. The
		 * string is converted to a double value as if by the valueOf method.
		 */

		Double doubleObj = new Double(strObj);
		/*
		 * Double class extends Number abstract class and overrides methods from
		 * Number class which returns the different type of primitives
		 * datatype().
		 *
		 * 1- byteValue()    : Returns byte
		 * 2- shortValue()   : Returns short
		 * 3- intValue()     : Returns int
		 * 4- longValue()    : Returns long
		 * 5- floatValue()   : Returns float
		 * 6- doubleValue()  : Returns double
		 */
		System.out.println("byte valye :"+doubleObj.byteValue());
		System.out.println("short valye :"+doubleObj.shortValue());
		System.out.println("int valye :"+doubleObj.intValue());
		System.out.println("long valye :"+doubleObj.longValue());
		System.out.println("float valye :"+doubleObj.floatValue());
		System.out.println("double valye :"+doubleObj.doubleValue());
	}
}

 

By | 2017-07-24T12:33:04+00:00 October 1st, 2012|Code Base|2 Comments

About the Author:

2 Comments

  1. Liss August 12, 2013 at 9:14 am - Reply

    You spelled value wrong 5 times 😉

  2. sengu March 11, 2015 at 9:45 am - Reply

    You spelled value wrong 6 times 😉

Leave A Comment