- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Long to numeric primitive data types in Java
Let’s say we have Long object here.
Long myObj = new Long("9879");
Now, if we want to convert this Long to short primitive data type. For that, use the in-built shortValue() method −
// converting to short primitive types short shortObj = myObj.shortValue(); System.out.println(shortObj);
In the same way convert Long to another numeric primitive data type int. For that, use the in-built intValue() method −
// converting to int primitive types int intObj = myObj.intValue(); System.out.println(intObj);
The following is an example wherein we convert Long to numeric primitive types short, int, float, etc −
Example
public class Demo { public static void main(String[] args) { Long myObj = new Long("9879"); // converting to numeric primitive types short shortObj = myObj.shortValue(); System.out.println(shortObj); int intObj = myObj.intValue(); System.out.println(intObj); float floatObj = myObj.floatValue(); System.out.println(floatObj); double doubleObj = myObj.doubleValue(); System.out.println(doubleObj); } }
Output
9879 9879 9879.0 9879.0
- Related Articles
- Convert Byte to numeric primitive data types in Java
- Convert Short to numeric primitive data types in Java
- Java Program to convert Double to numeric primitive data types
- Java Program to convert Java Float to Numeric Primitive Data Types
- Convert long primitive to Long object in Java
- List out the default values of numeric and non-numeric primitive data types in Java?
- Java primitive data types
- What are primitive data types in Java?
- How to convert JavaScript objects to primitive data types manually?
- Default value of primitive data types in Java
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()
- How to convert primitive data into wrapper class using Java?
- C++ Program to convert primitive types to objects
- Golang program to convert primitive types to objects
- What are Primitive and Non-Primitive Data Types in JavaScript?

Advertisements