long keyword in java

" long " is a Java primitive type.

A long variable can store a 64−bit signed integer.

Example

long amount=4560;
long randomNumber=45617l;

We can specify long number either with suffix “l” or “L” otherwise its considered as integer value.

Note:
In the first example value 4560 is considered as integer value but its assigning to long value and its allowed in Java through auto widening.


The long keyword can be used as a method parameter as below:

  1. public void displayAmount(long amount) {
  2.  
  3.     System.out.println("Amount is "+amount);
  4. }
public void displayAmount(long amount) {

    System.out.println("Amount is "+amount);
}


The long keyword can be used as a return type of a method as well:

  1. public long getAmount() {
  2.  
  3.              return 4560L; // even 4560 also works fine as auto widening is allowed in Java
  4. }
public long getAmount() {
 
             return 4560L; // even 4560 also works fine as auto widening is allowed in Java
}

Note:
All integer literals in Java are 32−bit int values unless the value is followed by l or L as in 4560L,indicating the value should be interpreted as a long.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on