Java Program to Convert int to long Last Updated : 10 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 64-bit integer. They both are primitive data types and the usage depends on how large the number is. Java int can be converted to long in two simple ways: Using a simple assignment. This is known as implicit type casting or type promotion, the compiler automatically converts smaller data types to larger data types.Using valueOf() method of the Long wrapper class in java which converts int to long. 1. Implicit type casting: In this, we are simply assigning an integer data type to a long data type.Since integer is a smaller data type compared to long, the compiler automatically converts int to long, this is known as Implicit type casting or type promotion. Java // Java program to demonstrate // use of implicit type casting import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { int intnum = 5; // Implicit type casting , automatic // type conversion by compiler long longnum = intnum; // printing the data-type of longnum System.out.println( "Converted type : " + ((Object)longnum).getClass().getName()); System.out.println("After converting into long:"); System.out.println(longnum); } } OutputConverted type : java.lang.Long After converting into long: 5 2. Long.valueOf() method: In this, we are converting int to long using the valueOf() method of the Long Wrapper class.The valueOf() method accepts an integer as an argument and returns a long value after the conversion. Java // Java program to convert // int to long using valueOf() method import java.io.*; class GFG { public static void main(String[] args) { int intnum = 56; Long longnum = Long.valueOf(intnum); // printing the datatype to // show longnum is of type // long contains data of intnum System.out.println( "Converted type : " + ((Object)longnum).getClass().getName()); // accepts integer and // returns a long value System.out.println("After converting into long:" + "\n" + longnum); } } OutputConverted type : java.lang.Long After converting into long: 56 Comment More infoAdvertise with us Next Article Java Program to Convert long to int C chatpatisizzy30 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Convert long to int Long is a larger data type than int, we need to explicitly perform typecasting for the conversion. Typecasting is performed through the typecast operator. There are basically three methods to convert long to int: By type-castingUsing toIntExact() methodUsing intValue() method of the Long wrapper cla 3 min read Java Program to Convert Double to Long Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java. Examples: Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21 Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value rang 4 min read Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s 4 min read Java Program to Convert Long to String The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a 4 min read Java Program to Convert String to Long To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a 3 min read Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types 4 min read Like