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 int to long with examples

By Chaitanya Singh | Filed Under: Java Conversion

In this tutorial, we will see how to convert int to long with examples.

Since int is smaller data type than long, it can be converted to long with a simple assignment. This is known as implicit type casting or type promotion, compiler automatically converts smaller data type to larger data type. We can also convert int to long using valueOf() method of Long wrapper class.

Java int to long using implicit type casting

In the following example, we are simply assigning 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 type promotion.

public class JavaExample{  
   public static void main(String args[]){  
	int num = 10;  
		
	/* Implicit type casting, automatic
	 * type conversion by compiler
	 */
	long lnum = num;  
	System.out.println(lnum);  
   }
}

Output:
Java int to long conversion

Java int to long Example using Long.valueOf() method

In the following example, we are converting int to long using valueOf() method of Long Wrapper class. The valueOf() method accepts integer as an argument and returns a long value after the conversion.

public class JavaExample{  
   public static void main(String args[]){  
	int inum = 99;  
	Long lnum = Long.valueOf(inum);  
	System.out.println("Converted long value is: "+lnum);  
   }
}

Output:
Java int to long conversion using valueOf() method of Long class

❮ 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