Why Does Java Output -124 When Converting int to byte?

Question

What is the reason behind Java producing -124 when converting an int value of 132 to a byte?

int i = 132;
byte b = (byte)i; 
System.out.println(b);

Answer

When converting an integer value to a byte in Java, if the integer exceeds the range of a byte, Java applies truncation, resulting in unexpected values. This explanation clarifies why converting the int value 132 yields -124 in the byte representation.

int i = 132;
byte b = (byte) i; // This results in -124 due to truncation of higher bits
System.out.println(b); // Output will be -124

Causes

  • A byte in Java is an 8-bit signed integer with a range of -128 to 127.
  • When the int value 132 is converted to byte, it exceeds the maximum byte range of 127.
  • The conversion process truncates the higher bits, resulting in a negative byte value due to the two's complement representation.

Solutions

  • To avoid unexpected results, ensure that the integer value falls within the byte range before conversion.
  • Use proper casting and validation logic to audit the values being cast to prevent data loss.

Common Mistakes

Mistake: Assuming all integers can be safely converted to byte without verification.

Solution: Always check the integer value against the byte range before converting.

Mistake: Not understanding how two's complement works, leading to confusion about negative values.

Solution: Familiarize yourself with the concept of two's complement and how it affects binary representations in Java.

Helpers

  • Java int to byte conversion
  • Java byte range
  • Java casting int to byte
  • Java negative byte values
  • Java data type casting

Related Questions

⦿How to Create a Java Date Object from Year, Month, and Day Correctly

Learn how to accurately create a Java date object from year month and day fixing common mistakes involving month indexing.

⦿How to Create a Unicode Character in Java from Its Numeric Code

Learn how to generate Unicode characters in Java from numeric codes dynamically. Effective solutions and common pitfalls included.

⦿How to Override RestTemplate Bean for Integration Tests in Spring Boot

Learn how to successfully override the RestTemplate bean in your Spring Boot integration tests to avoid external service calls.

⦿Understanding Circular Dependencies in Spring Framework

Learn how Spring resolves circular dependencies between beans A and B with indepth explanation and solutions.

⦿Understanding Short-Circuiting with Logical Operators in Java

Learn what shortcircuiting means in Java logical operators and how it affects conditional expressions. Explore examples and best practices.

⦿How to Access Files from a JAR in Java Using ClassLoader

Discover how to load files from JARs and the filesystem in Java using ClassLoader with code examples and best practices.

⦿How to Create an Executable JAR with Dependencies using Maven

Learn how to build an executable JAR file with dependencies in Maven troubleshoot common issues and optimize your project.

⦿How to Synchronize a Static Variable Across Multiple Thread Instances in Java?

Learn how to synchronize a static variable among threads for different instances of a class in Java with practical examples and best practices.

⦿Which Java Concurrency List is Best for a Fixed Thread Pool?

Discover the best monitorfree List from java.util.concurrent for a fixed thread pool. Optimize readwrite operations with expert insights.

⦿Why Do Critics Argue That Java's Implementation of Generics Is Flawed?

Explore criticisms of Javas generics their limitations and suggestions for improvement. Learn why Javas generics impact type safety and usability.

© Copyright 2025 - CodingTechRoom.com