|
| 1 | +/***************************************************************************** |
| 2 | + * PROJECT: Java-101 Comprehensive Programming Course |
| 3 | + * MODULE: Introduction to Java Fundamentals |
| 4 | + * LESSON: 1.3.1 - Integer Numeric Data Types |
| 5 | + * |
| 6 | + * AUTHOR: Dr. Saad Laouadi, Ph.D. |
| 7 | + * created: April 2025 |
| 8 | + * |
| 9 | + * Copyright © 2025 Dr. Saad Laouadi. All Rights Reserved. |
| 10 | + * See LICENSE file for complete terms. |
| 11 | + *****************************************************************************/ |
| 12 | + |
| 13 | +public class NumericIntegerTypes { |
| 14 | + public static void main(String[] args) { |
| 15 | + displayBanner('=', 50); |
| 16 | + System.out.println(" Integer Numeric Types in Java "); |
| 17 | + displayBanner('=', 50); |
| 18 | + |
| 19 | + // Integer numeric data types |
| 20 | + byte byteVar = 127; // 8-bit signed integer |
| 21 | + short shortVar = 32767; // 16-bit signed integer |
| 22 | + int intVar = 2147483647; // 32-bit signed integer |
| 23 | + long longVar = 9223372036854775807L; // 64-bit signed integer |
| 24 | + |
| 25 | + // Print out the integer type values and their ranges |
| 26 | + System.out.println("Integer Numeric Data Types:"); |
| 27 | + |
| 28 | + System.out.println("\nbyte:"); |
| 29 | + System.out.println("\tValue: " + byteVar); |
| 30 | + System.out.println("\tSize: 8 bits (1 byte)"); |
| 31 | + System.out.println("\tRange: -128 to 127"); |
| 32 | + System.out.println("\tUse: Memory-sensitive data, file I/O operations"); |
| 33 | + |
| 34 | + System.out.println("\nshort:"); |
| 35 | + System.out.println("\tValue: " + shortVar); |
| 36 | + System.out.println("\tSize: 16 bits (2 bytes)"); |
| 37 | + System.out.println("\tRange: -32,768 to 32,767"); |
| 38 | + System.out.println("\tUse: Memory conservation when range is sufficient"); |
| 39 | + |
| 40 | + System.out.println("\nint:"); |
| 41 | + System.out.println("\tValue: " + intVar); |
| 42 | + System.out.println("\tSize: 32 bits (4 bytes)"); |
| 43 | + System.out.println("\tRange: -2,147,483,648 to 2,147,483,647"); |
| 44 | + System.out.println("\tUse: Default integer type for most calculations"); |
| 45 | + |
| 46 | + System.out.println("\nlong:"); |
| 47 | + System.out.println("\tValue: " + longVar); |
| 48 | + System.out.println("\tSize: 64 bits (8 bytes)"); |
| 49 | + System.out.println("\tRange: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807"); |
| 50 | + System.out.println("\tUse: Very large numbers, timestamps, unique IDs"); |
| 51 | + System.out.println("\tNote: The 'L' suffix is required for long literals"); |
| 52 | + |
| 53 | + displayBanner('=', 50); |
| 54 | + } |
| 55 | + |
| 56 | + public static void displayBanner(char c, int length) { |
| 57 | + for (int i = 0; i < length; i++) { |
| 58 | + System.out.print(c); |
| 59 | + } |
| 60 | + System.out.println(); |
| 61 | + } |
| 62 | + |
| 63 | + // Numeric Integer Examples |
| 64 | + public static void numiericIntegerVariablesUsage(){ |
| 65 | + |
| 66 | + // Examples of using integer types |
| 67 | + System.out.println("\nExamples of Integer Operations:"); |
| 68 | + int sum = 25 + 50; |
| 69 | + System.out.println("\tAddition: 25 + 50 = " + sum); |
| 70 | + |
| 71 | + int difference = 100 - 30; |
| 72 | + System.out.println("\tSubtraction: 100 - 30 = " + difference); |
| 73 | + |
| 74 | + int product = 5 * 6; |
| 75 | + System.out.println("\tMultiplication: 5 * 6 = " + product); |
| 76 | + |
| 77 | + int quotient = 20 / 4; |
| 78 | + System.out.println("\tDivision: 20 / 4 = " + quotient); |
| 79 | + |
| 80 | + int remainder = 17 % 5; |
| 81 | + System.out.println("\tRemainder (modulus): 17 % 5 = " + remainder); |
| 82 | + } |
| 83 | +} |
0 commit comments