What do you need to know about Java int primitives? – J008

by Apr 22, 2015

DeegeU Java Course

This video “What do you need to know about Java int primitives?” 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 int primitives
  2. Try creating errors in your program and compiling it

Transcript – What do you need to know about Java int primitives?

We’re finally going to look at how to represent values in Java using an integer. This is the most commonly used Java primitive. The integer is big enough to hold most numbers that you will use.

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

Java int primitives - Free Java Course Online - What do you need to know about Java int primitives? video

The Java int primitive is the most commonly used primitive in the language.

An integer is represented by 32 bits, or 4 bytes. In Java we use the keyword int to declare integers. An integer is 4 bytes, or 32 bits. This is a number from approximately negative 2 billion to positive two billion. The actual number is 2 to the power 31, but this number is large enough to just call it 2 GB.

On a side note, 32 bit operating systems like Windows, can not have a file or memory larger than 4 GB. This is because your computer needs one number for each possible
addressable memory location. There are only 4 billion unsigned possible locations on a 32 bit operating system. Or 4 GB.

To create an integer primitive we use the keyword int, not integer. This comes from C, and for some reason someone thought integer was too long to type out. We give it a name, and possibly a value. An in this example, we’re using a number I picked out of a hat. 2,763,306. We can give the value as a decimal value, binary value, or hexadecimal value.

Open our program again. I’ll add this. 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. This is because we are not formatting the value when we print it back out.

And that is the integer. When you’re doing any integer math, this is the type you’ll want to use. There is nothing faster or more efficient than the int.

Next we’ll cover the last integer primitive in Java. The long.

<p><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
(adsbygoogle = window.adsbygoogle || []).push({});

Tools Used

  • Java
  • NetBeans

Media Credits

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

  • No infringement intended

“How it Begins”
Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 3.0

Get the code

The source code for “What you need to know about Java int primitives ” 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 https://github.com/deege/deegeu-java-intro.git

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.

<h2>Don't miss another video!</h2> <p>New videos come out every week. Make sure you subscribe!<br><script src="//apis.google.com/js/platform.js"></script></p> <div class="g-ytsubscribe" data-channel="deegeu" data-layout="full" data-count="default"></div> <p></p>



DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This