How to use the Java long primitive – J009

by Apr 22, 2015

DeegeU Java Course

This video “How to use the Java long primitive” is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

  1. Type in a program with different Java long primitives
  2. Try creating errors in your program and compiling it

Transcript – How to use the Java long primitive

Last of the integers. We’re going to look at how to represent values in Java using the long. The long is probably the second most used primitive in Java, second only to the int. This type is used for database ids, or things where you’ll have more than 2 billion.

The goal for this lesson is to learn about the long data type, it’s size, and how we create it in Java.

Java long primitive - Free Java Course Online - How to use the Java long primitive video

The Java long primitive can hold VERY large integers.

So if for some reason 4 billion isn’t large enough, say you’re trying to represent how much money Bill Gates has, there’s one bigger. It’s called a long and it is 8 bytes, or 64 bits. It can represent a number larger than 9 quintillion plus or minus a few quadrillion. Needless to say, it’s a big number. It would take 5 billion years to count that high. Better get started!

To create a long, we use the keyword long. We give it a name, and possibly a value. In this video I’m using this value. I’m not even going to try to say it. It’s big.


We can give the value as a decimal value, binary value, or hexadecimal value.

The first example is a decimal literal. The second is a binary literal. And the third is a hexadecimal literal. We run it, and again, the values are returned in decimal. By now this should look really familiar. We’re just changing the data type and values, but the code is pretty much the same as it was for bytes, shorts, and ints.

You’re probably wondering, should I use a int or a long. If there’s a doubt, you probably want to use an int. Yes, the long matches the word size on your computer, 64 bits, but it will also take a ton of room in memory. You’re better off using an int. Longs are good for object identifies.

So that concludes the integer types. What happens when you want to store real numbers? Like 26.4? That’s up next when we cover the floating point number types.

Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • No infringement intended

Get the code

The source code for “Java Long Tutorial” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!



Pin It on Pinterest

Share This