Convert long value to primitive types
In this chapter you will learn:
Convert long value to byte, double, float, int, long, short
Long
class has the following methods for converting long type value to other primitive types.
byte byteValue()
returns the value of this Long as a byte.double doubleValue()
returns the value of this Long as a double.float floatValue()
returns the value of this Long as a float.int intValue()
returns the value of this Long as an int.long longValue()
returns the value of this Long as a long value.short shortValue()
returns the value of this Long as a short.
The code below creates a long
type value from string and then converts it other primitive types.
public class Main {
public static void main(String[] args) {
Long longObject = new Long("1234567");
byte b = longObject.byteValue();
System.out.println("byte:"+b);
/*from j av a2 s . c o m*/
short s = longObject.shortValue();
System.out.println("short:"+s);
int i = longObject.intValue();
System.out.println("int:"+i);
float f = longObject.floatValue();
System.out.println("float"+f);
double d = longObject.doubleValue();
System.out.println("double:"+d);
long l = longObject.longValue();
System.out.println("long:"+l);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to decode a string to create long value
- How to convert and parse long value from string
- How to convert or parse string to long with radix
Home » Java Tutorial » Primitive Data Types