How do you use Java byte primitives? – J006

DeegeU Course

This video “Java Byte Tutorial” 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

Create a program that prints a byte

Try storing a value larger than a byte

Transcript – Java byte primitives

A computer byte is more than a just computer joke about something more than a bit. In this lesson, we’ll take a deeper look at the first Java primitive. We are going to see how to represent values in Java using a byte.

The goal for this lesson is to understand what a byte is in Java, how big is it, and how we create bytes in Java.

In Java, a byte primitive is 8 bits. This means the Java byte primitive can only hold a number that fits into 8 bits. Any larger number will not fit into a byte, and likely will break your program.

A Java byte consists of 8 bits.

You might think this means any number that is 2 to the power 8, and you’d be forgiven because this is a rational thought. However, these numbers are 2’s compliment numbers. This means it includes the negative numbers. You can store only 2 to the power of seven positive and negative digits. Zero counts as a positive number. This means a byte can represent a number from -128 to 127. Anything outside that range will not fit in a byte.

To create a byte in Java, we use the keyword byte, give it a name, and optionally we give it an initial value. We can give the value as a decimal value, binary value, or hexadecimal value.

The green examples are how you define a byte in Java. The red line is an example that will not compile. 128 is too large for a byte. Now if you’re working with a version of Java before version 7, you can’t use the binary literal version. You need to do this. We’ll use literals, because this course is using Java 8.

So let’s go open up our application that we created before.

And inside it, we’ll add some lines to create some bytes. There’s no surprise to any of these. Each of the bytes creates the number 42. The first one uses a decimal literal, the second one uses a binary literal, and the third one uses a hexadecimal literal. When we
run it they all print out 42. The reason why it prints out 42 and not the binary form or the hexadecimal form, is that we’re not formatting our outputs. We’re just saying give us back the number, and that number is 42 regardless of how we originally defined it.

Now I said earlier that a byte can only go from -128 to 127. What happens if we try to put something in bigger anyway? So if we change it to 420. It’ll immediately give us an error. It’ll say “incompatible types possible lossy conversion from int to byte”. Basically it’s saying that 420’s type is an int, and is trying to stuff it into a byte and it won’t work. Even if we try to compile this, it’ll give us the same error when we compile.

So to get rid of the error, go back to byte forty-two and we’re good. Hey! Thanks for watching the video.

There is a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.

If you like the videos you are seeing please let me know by liking the video and hitting the subscribe button for the DeegeU channel on YouTube. I’d really appreciate that. If you have concerns or questions please leave them in the comments below or on DeegeU.com. There’s a poll on the front page of DeegeU.com so you can let me know what topic is covered next.

Thanks for watching and see you in the next video!



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 Byte Tutorial” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

Don’t miss another video!

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

Comments

comments

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.