Slide 1
Slide 2
Slide 3

Byte data type in java programming (V9)

No Comments

 
Data Types

Java data types determine the type and range of values for data within different types of variables, constants, method parameters, return types, etc. The data type tells the compiler about the type of data to be stored and the amount of memory required. To store and organize different data types, all variables must have specified data types.

Based on the data type of the variable, the operating system allocates memory and decides what can be stored in the reserved memory. Thus, by assigning different data types to variables, you can store integers, decimals (floating-point numbers), or characters in those variables.

Java data types are classified into two main categories:

  • Primitive Data Types

  • Reference/Object Data Types

Primitive Data Types

        Primitive data types are predefined by the language and named by a keyword. There are eight primitive data types supported by Java. Below is the list of primitive data types:

  • byte

  • short

  • int

  • long

  • float

  • double

  • Boolean

  • char

Byte Data Type

The byte data type is an 8-bit two's complement integer. It has a minimum value of -128 $(-2^7)$ and a maximum value of 127 (inclusive) $(2^7 - 1)$.

The default value of a byte variable is 0. It is primarily used to save space in large arrays of integers, since a byte is four times smaller than an int.

Syntax:

Source Code:

Copied!
public class ByteHourExample {
    public static void main(String[] args) {
        // Step 1: Declare byte variables for hours
        byte morningStart = 6;  // Morning starts at 6 AM
        byte afternoonStart = 12; // Afternoon starts at 12 PM
        byte eveningStart = 18; // Evening starts at 6 PM
        byte currentHour = 3;  // Example: Current time is 3 AM

        // Step 2: Display current time period based on conditions
        if (currentHour >= morningStart && currentHour < afternoonStart) {
            System.out.println("Good Morning! It's " + currentHour + ":00 hours.");
        } else if (currentHour >= afternoonStart && currentHour < eveningStart) {
            System.out.println("Good Afternoon! It's " + currentHour + ":00 hours.");
        } else if (currentHour >= eveningStart && currentHour < 24) {
            System.out.println("Good Evening! It's " + currentHour + ":00 hours.");
        } else if (currentHour >= 0 && currentHour < morningStart) {
            System.out.println("It's Night! It's " + currentHour + ":00 hours.");
        } else {
            System.out.println("Invalid hour! Hours must be between 0 and 23.");
        }

        // Step 3: Adding hours to simulate future events
        byte hoursToAdd = 5;
        // Handle wrapping around the 24-hour clock
        byte futureHour = (byte) ((currentHour + hoursToAdd) % 24); 
        
        // Step 4: Display future time
        System.out.println("In " + hoursToAdd + " hours, it will be " + futureHour + ":00 hours.");
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top