BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is byte Keyword in Java

Last Updated: October 30, 2022 by Chaitanya Singh | Filed Under: java

The byte keyword has following two usages in Java:

  • byte data type
  • byte return type of a method

The byte keyword is used to define a primitive data type. A byte data type variable can hold values ranging from -128 to 127. The size of byte data type is 8 bit.

It can also be used as a return type of a method.

byte Keyword Examples

Example 1:

class JavaExample {
  public static void main(String args[]) {
    byte num = 100, num2 = -50;
    System.out.println(num);
    System.out.println(num2);
  }
}

Output:

100
-50

Example 2:

This program will throw error as the max value that a byte data type can handle is 127 and the minimum value it can handle is -128.

class JavaExample {
  public static void main(String args[]) {
    byte num = 200;
    System.out.println(num);
  }
}

Output:

byte keyword example output

Example 3:

In this example, we have a method with byte return type. This method takes int argument as input and returns byte equivalent, if input value falls in the valid byte range.

class JavaExample {
  public static byte myMethod(int num){
    byte bVar;
    if(num<=127 || num>=-128){
      bVar = (byte)num; //type casting
    }else{
      System.out.println("Byte can only hold values between -128 to 127");
      bVar = 0;
    }
    return bVar;
  }
  public static void main(String args[]) {
    int num = 101;
    System.out.println("Given int to byte: "+myMethod(num));
  }
}

Output:

Given int to byte: 101

Related guides:

  • Java Data Types Tutorial
  • Java Wrapper Class Tutorial
  • Java Type Casting Tutorial
❮ Java Keywords

Top Related Articles:

  1. Operators in Java With Examples
  2. Constructor Overloading in Java with examples
  3. What is char Keyword in Java
  4. Java Integer byteValue() Method
  5. Java Integer bitCount() Method

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap