BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is long Keyword in Java

By Chaitanya Singh | Filed Under: java

The long keyword is a primitive data type in Java. Similar to int data type, it is used to store whole numbers. The range of long data type is much wider than int. A long data type has a range of -263(-9223372036854775808) to 263-1(9223372036854775807).

public class JavaExample {
  public static void main(String[] args) {
    long num = 12345678910L;
    System.out.println(num);
  }
}

Output:

12345678910

Points to Note:

  • The long values are ended with ‘L’ letter as shown in the above example.
  • The size of long is 64 bit (8 bytes), which is higher than int so you should use int for the values that are within int data type range.
  • The default value of long type variable is 0.

Example of long return type method

In this example, we have a method with long return type. Since we know, that the product of given numbers cannot be handled by an int data type so we have chosen long return type instead of int. As the result is out of range of int max value.

You should use long data type for such instances, where you expect that result would be out of range of an int.

public class JavaExample {
  public static long multiply(int a, int b){
    return a*b;
  }
  public static void main(String[] args) {
    long product = multiply(10, 123456789);
    System.out.println(product);
  }
}

Output:

1234567890

Recommended Posts

  • Java Wrapper Class Tutorial
  • Java Data Types Tutorial
  • Java Convert int to long
  • Java Type Casting Tutorial
❮ Java Keywords

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap