How do you use the Java short primitive? – J007

by | Apr 20, 2015




DeegeU Course

This video “How do you use the Java short 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 short types
  2. Try creating errors in your program and compiling it

Transcript – How do you use the Java short primitive?

Next we are going to see how to represent values in Java using the short. This isn’t a commonly used data type. You’ll see it more often in Java programs for embedded devices, or maybe sound processing? You not going to see it in a typical program. On the other hand, we’re seeing more and more devices in the internet of things, so maybe the short will make a comeback. I’m including it for completeness. It’s good to understand all the primitive data types, so if you see it used, you know what it is.

The goal for this lesson is to look at the short data type, it’s size, and how we create it in Java.

A short is a number represented by 16 bits, or 2 bytes. Again the short is a two’s complement number. This means half of the representable numbers in 16 bits are negative. This gives the short a range between -32,768 and 32,767.

Java short primitive - Free Java Course Online

The Java short primitive is 16 bits, and isn’t used often.

And you can create shorts using decimal literals, binary literals, or hexadecimal literals, just like the byte. If you store a number larger than 32767 or smaller than -32768, the compiler will give us the incompatible type error we saw in the last lesson.

So to create shorts, we use the keyword short. Give it a name, and possibly a literal value. In this example, we’re creating the number 10794.

Back in our program 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.

So that’s the short. We’ll cover the most used Java primitive next.







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 “How do you use the Java short primitive?” 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.

Don’t miss another video!

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

Comments

comments

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