beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Convert long to int with examples

By Chaitanya Singh | Filed Under: Java Conversion

In this guide, we will see how to convert long to int with examples. Since long is larger data type than int, we need to explicitly perform type casting for the conversion.

Java long to int example

In the following example we are converting long data type to int data type using explicit typecasting. Here we have a variable lnum of long data type and we are converting the value of it into an int value which we are storing in the variable inum of int data type.

public class JavaExample{  
   public static void main(String args[]){  
	long lnum = 1000;  
		
	//type casting - long to int
	int inum = (int)lnum;  
	System.out.println("Converted int value is: "+inum);  
   }
}

Output:
Java long to int example

Java long to int example using intValue() method of Long wrapper class

In the following example, we have a Long object lnum and we are converting it into an int primitive type using intValue() method of Long class.

public class JavaExample{  
   public static void main(String args[]){  
	//Long object
	Long lnum = 99L;
		
	//Converting Long object to int primitive type
	int inum = lnum.intValue();  
	System.out.println("Converted int value: "+inum);  
   }
}

Output:
Java long object to int primitive type

❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap