|
| 1 | +public class IntExample { |
| 2 | + public static void main(String[] args) { |
| 3 | + // Print ranges of integer types (Byte, Short, Int, Long) |
| 4 | + System.out.println("Byte range: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE); |
| 5 | + System.out.println("Short range: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE); |
| 6 | + System.out.println("Int range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE); |
| 7 | + System.out.println("Long range: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE); |
| 8 | + System.out.println(); // Print an empty line for better readability |
| 9 | + |
| 10 | + // Initialize integer variables for different data types |
| 11 | + byte byteValue = 10; // A byte value |
| 12 | + short shortValue = 32500; // A short value |
| 13 | + int intValue = 2147483640; // An int value |
| 14 | + long longValue = 1234567890123456789L; // A long value with 'L' at the end to indicate it's a long literal |
| 15 | + |
| 16 | + // Demonstrate arithmetic operations |
| 17 | + demonstrateArithmeticOperations(intValue, byteValue, shortValue, longValue); |
| 18 | + |
| 19 | + // Demonstrate relational operations |
| 20 | + demonstrateRelationalOperations(intValue, longValue); |
| 21 | + |
| 22 | + // Demonstrate type casting |
| 23 | + demonstrateTypeCasting(byteValue, intValue); |
| 24 | + |
| 25 | + // Demonstrate overflow and underflow in byte type |
| 26 | + demonstrateOverflowAndUnderflow(); |
| 27 | + } |
| 28 | + |
| 29 | + // Method to demonstrate arithmetic operations on integer types |
| 30 | + private static void demonstrateArithmeticOperations(int intValue, byte byteValue, short shortValue, |
| 31 | + long longValue) { |
| 32 | + System.out.println("Arithmetic Operations:"); |
| 33 | + // Addition of intValue and byteValue |
| 34 | + System.out.println("Addition: " + (intValue + byteValue)); |
| 35 | + |
| 36 | + // Subtraction of shortValue and byteValue |
| 37 | + System.out.println("Subtraction: " + (shortValue - byteValue)); |
| 38 | + |
| 39 | + // Multiplication of intValue and shortValue |
| 40 | + System.out.println("Multiplication: " + (intValue * shortValue)); |
| 41 | + |
| 42 | + // Division of longValue by intValue |
| 43 | + System.out.println("Division: " + (longValue / intValue)); |
| 44 | + |
| 45 | + // Modulo operation (remainder of shortValue divided by byteValue) |
| 46 | + System.out.println("Modulo: " + (shortValue % byteValue)); |
| 47 | + System.out.println(); // Empty line for separation |
| 48 | + } |
| 49 | + |
| 50 | + // Method to demonstrate relational operations between int and long |
| 51 | + private static void demonstrateRelationalOperations(int intValue, long longValue) { |
| 52 | + System.out.println("Relational Operations:"); |
| 53 | + // Check if intValue is equal to longValue |
| 54 | + System.out.println("Equal to: " + (intValue == longValue)); |
| 55 | + |
| 56 | + // Check if intValue is not equal to longValue |
| 57 | + System.out.println("Not equal to: " + (intValue != longValue)); |
| 58 | + |
| 59 | + // Check if intValue is greater than longValue |
| 60 | + System.out.println("Greater than: " + (intValue > longValue)); |
| 61 | + |
| 62 | + // Check if intValue is less than longValue |
| 63 | + System.out.println("Less than: " + (intValue < longValue)); |
| 64 | + |
| 65 | + // Check if intValue is greater than or equal to longValue |
| 66 | + System.out.println("Greater than or equal to: " + (intValue >= longValue)); |
| 67 | + |
| 68 | + // Check if intValue is less than or equal to longValue |
| 69 | + System.out.println("Less than or equal to: " + (intValue <= longValue)); |
| 70 | + System.out.println(); // Empty line for separation |
| 71 | + } |
| 72 | + |
| 73 | + // Method to demonstrate type casting between byte and int |
| 74 | + private static void demonstrateTypeCasting(byte byteValue, int intValue) { |
| 75 | + System.out.println("Type Casting:"); |
| 76 | + // Implicit casting (byte to int) is done automatically by Java |
| 77 | + int implicitCast = byteValue; |
| 78 | + System.out.println("Implicit cast from byte to int: " + implicitCast); |
| 79 | + |
| 80 | + // Explicit casting (int to byte) requires type casting because it may result in |
| 81 | + // data loss |
| 82 | + byte explicitCast = (byte) intValue; |
| 83 | + System.out.println("Explicit cast from int to byte: " + explicitCast); |
| 84 | + System.out.println(); // Empty line for separation |
| 85 | + } |
| 86 | + |
| 87 | + // Method to demonstrate overflow and underflow behavior in byte type |
| 88 | + private static void demonstrateOverflowAndUnderflow() { |
| 89 | + System.out.println("Overflow and Underflow:"); |
| 90 | + // Overflow: Byte.MAX_VALUE + 1 causes an overflow, wrapping around to |
| 91 | + // Byte.MIN_VALUE |
| 92 | + byte overflowExample = (byte) (Byte.MAX_VALUE + 1); |
| 93 | + System.out.println("Overflow in byte (Byte.MAX_VALUE + 1): " + overflowExample); |
| 94 | + |
| 95 | + // Underflow: Byte.MIN_VALUE - 1 causes an underflow, wrapping around to |
| 96 | + // Byte.MAX_VALUE |
| 97 | + byte underflowExample = (byte) (Byte.MIN_VALUE - 1); |
| 98 | + System.out.println("Underflow in byte (Byte.MIN_VALUE - 1): " + underflowExample); |
| 99 | + System.out.println(); // Empty line for separation |
| 100 | + } |
| 101 | +} |
0 commit comments