Copyright

Java Data Types: Byte

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Data Types: Short, Int & Long

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Byte Data Type
  • 0:47 Examples & Uses
  • 1:51 Inputting a Byte as an Array
  • 2:34 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java's got byte: One of the basic data types available in Java is the byte, the smallest sized data type available. As we will show in this lesson, it is still a useful data type for programmers.

Byte Data Type

Bytes are 1s and 0s
Byte Image

The byte is one of the primitive data types in Java. This means that the data type comes packaged with the same programming language and there is nothing special you have to do to get it to work. A Java byte is the same size as a byte in computer memory: it's 8 bits, and can hold values ranging from -128 to 127.

Be careful when using byte, especially if there is the possibility of a number greater than 127 (or less than -128). There isn't a way to store a larger value into a byte: the program will overflow. Look at the next example, where a byte is declared, but the user enters 525.

Here's the code:


Java Byte Syntax


And the result:


Java Byte Compiler Error


Examples/Uses

There are a few times when the byte data type is useful; by itself and as an array (think of a list of values) to input a value. An array is a variable that holds a list of values. We'll cover using a byte as an array later on. Let's look at some of these circumstances:

Declaring a Byte

As a primitive data type, there's nothing special you need to declare a byte value. The simplest syntax is to declare a byte as:


Java Declare Byte


This creates a new byte and assigns it a value of zero. In fact, you don't even need to do this, although it is good practice: The default value for a byte is 0. Remember, that the byte only allows for values between -128 and 127; if you try to put a larger number into the byte, there will be errors.

Other Uses

Bytes are useful when creating large arrays of data since a byte is so much smaller than an int. A practical use for bytes is in file manipulation and reading: there are methods to read files byte-by-byte, and so this is a useful option. Another option is to read in user input byte-by-byte. Let's look at that next.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support