Java Tutorials - Herong's Tutorial Examples - v8.21, by Dr. Herong Yang
Operations on "byte" Data Type Values
This section provides a tutorial example on how 'byte' values are casted to 'int' values when they are involved in arithmetic, comparison and bitwise operations.
Actually, there is no operations defined in Java that works directly on "byte" data type values.
But "byte" data type values can participate all operations defined for integer values. More precisely:
To validate those rules mentioned above, I wrote the following sample program, ByteOperations.java:
/* ByteOperations.java * Copyright (c) HerongYang.com. All Rights Reserved. */ public class ByteOperations { public static void main(String[] arg) { byte b1 = 127; int o1 = (b1+b1-0)*5/10; // arithmetic operations byte b2 = 0x0000007F; boolean o2 = b2 > b2 || b2 != b2 || b2 == b2; // comparisons byte b3 = (byte) 127; int o3 = b3 & 0x0000000F | 0x00000070; // bitwise operations byte b4 = (byte) 0x0000007F; int o4 = b4 >> 4 << 4; // shift operations System.out.println("o1: "+o1); System.out.println("o2: "+o2); System.out.println("o3: "+o3); System.out.println("o4: "+o4); } }
After executing this program, I got this output with no surprises:
o1: 127 o2: true o3: 127 o4: 112
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
►Bits, Bytes, Bitwise and Shift Operations
"byte" Data Type and Implicit Casting
►Operations on "byte" Data Type Values
Bitwise Operations on "byte" Values
Bitwise Operations on "byte" Values - Example Program
Shift Operations - Left, Right or Unsigned Right
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
Generic Methods and Type Inference
Lambda Expressions and Method References
Java Modules - Java Package Aggregation
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs
Garbage Collection and the gc() Method
Assert Statements and -ea" Option