×

How to Convert long to int in Java

How to Convert long to int in Java

When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion. The conversion from long to int is called explicit conversion or narrowing conversion. long is a larger data type than int.

How to Convert long to int in Java    

There are two ways to convert long to int.

  • Using Assignment Operator
  • Using intValue() method

Using Assignment Operator

Casting is used to perform the conversion between two incompatible types.  A cast is simply an explicit type conversion. The syntax of the casting is:

data type variable_name = (target data type) variable to convert;

Example

In the following example, num1 is a variable of type long contains value 10000. num2 is a variable of type int that stores the converted int value of num1. (int)num1 converts the long value to int value. The println statement prints the converted int value of the variable num1.

public class longTointExample
{ 
public static void main(String args[])
{ 
long num1 = 10000; 
int num2 = (int)num1;  //explicit conversion
System.out.println("Converted int value is: "+num2); 
}
}

Output

Converted int value is: 10000

Using intValue() method

intValue() is the built-in method of Integer wrapper class which belongs to java.lang package. This method does not accept any parameter. It returns the value which is represented by the object after conversion to the integer type. The signature of the method is given below:

public int intValue()

Example

In the following example, Long is the wrapper class and number is a reference variable of the class. num is the variable of type int that stores the converted value of the variable number. intValue() is the method of Integer wrapper class. The println statement prints the converted int value of the variable number.

public class longTointExample1
{ 
public static void main(String args[])
{ 
Long number = 789L;
int num = number.intValue();  //explicit conversion
System.out.println("Converted int value is: "+num); 
}
}

Output

Converted int value is: 789

Related Topics

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.