This chapter provides a foundational understanding of computer programming, covering the core concepts of languages, output, variables, and data types, using the Java programming language.
1. The Computer, Language, and Execution
A computer is often described as a “super dumb” machine—it cannot perform any task without being given explicit, step-by-step instructions.
| Term | Definition |
| Software/Application | A set of programs. |
| Program | A set of instructions. |
| Programming Language | The medium (like Java, Python, C++) used to write instructions that humans can read. |
| Compiler | A utility that translates the human-readable Programming Language (e.g., Java) into Machine Language (0/1), which the computer executes. |
| Syntax | The set of rules or grammar that governs how instructions must be written in a specific programming language. |
Key Rule: Java is a case sensitive language, meaning that
Variableis different fromvariable.
The environment used for writing, compiling, and running code is called an IDE (Integrated Development Environment).
Code Comments
Comments are notes within the code that are ignored by the compiler. They are used solely by developers for documentation or temporary code exclusion.
| Type | Syntax | Example |
| Single-line | // | // This line is a comment |
| Multi-line | /* ... */ | /* This is a multi-line comment */ |
2. Output and the + Operator
The most basic function in any program is displaying output to the user.
| Function | Behavior | Example | Output |
System.out.print() | Prints content and keeps the cursor on the same line. | System.out.print("A"); System.out.print("B"); | AB |
System.out.println() | Prints content and moves the cursor to the next line (ln stands for “line”). | System.out.println("A"); System.out.print("B"); | A (Line 1), B (Line 2) |
The Concatenation Rule
The + operator has two behaviors based on its operands:
- Arithmetic: If both operands are numbers, it performs standard addition.
- Concatenation: If one or both operands are strings (text enclosed in quotes), it joins them together.
Important: Expressions are evaluated strictly from left to right.
| Code Snippet | Step-by-Step Evaluation | Resulting Output |
System.out.print(10 + 20); | (10 + 20) is arithmetic. | 30 |
System.out.print(105 + "Hello"); | + acts as concatenation because of the string. | 105Hello |
System.out.print(5 + 105 + "Hello"); | 1. (5 + 105) is arithmetic (110). 2. 110 + "Hello" is concatenation. | 110Hello |
System.out.print(105 + "Hello" + 45); | 1. (105 + "Hello") is concatenation (“105Hello”). 2. "105Hello" + 45 is concatenation. | 105Hello45 |
3. Data Types and Variables
Variables are named containers in memory used to store data. Before using a variable, you must declare its Data Type to tell the compiler how much memory to reserve and what kind of data it will hold.
Core Primitive Data Types
| Data Type | Description | Size (Bits) | Approx. Range/Notes | Example Declaration |
int | Stores whole numbers (integers). | 32 bits. | ≈ -2 × 10⁹ to 2 × 10⁹ – 1 | int x1 = 30; |
long | Stores very large whole numbers. | 64 bits. | ≈ -8 × 10¹⁸ to 8 × 10¹⁸ – 1 | long bigNum = 14751L; |
float | Stores decimal numbers (single precision). | – | Requires the f suffix for literals (e.g., 4.57f). | float f = 4.57f; |
double | Stores decimal numbers (high precision). | – | This is the default for decimals. | double d = 1.7652; |
3.5. Understanding Data Storage and Range
The Fundamental Unit: The Bit
A computer stores all information using electricity, which can only be in one of two states: on or off. This is represented by a Bit (Binary Digit), which holds a value of either 0 or 1.
- 1 Bit is the minimum storage unit.
- The size of a data type (e.g., 32 bits for an
int) determines the range of possible unique values it can store.
Integer Range (32-bit int)
The standard Java int type uses 32 bits of memory. One bit is reserved to indicate the sign (positive or negative).
| Data Type | Size | Exact Range (Inclusive) | Approximate Range |
int | 32 Bits | -2³¹ to -2³¹ – 1 | ≈ -2 × 10⁹ to 2 × 10⁹ – 1 |
The Range Approximation for int
The large range is derived from the fundamental relationship that 2¹⁰ is approximately 10³ (since 2¹⁰ = 1024).
- We start with the closest power of 2: 2¹⁰ ≈ 10³
- Take the cube of both sides: (2¹⁰)³ ≈ (10³)³.
- This gives: 2³⁰ ≈ 10⁹.
- Multiply by 2 on both sides to reach 2³¹ : 2³¹ ≈ 2 × 10⁹.
This confirms that the maximum positive value for an int is 2³¹ – 1, or approximately 2 × 10⁹.
Extended Range (long)
The long data type uses 64 bits to store whole numbers, offering a significantly larger range.
| Data Type | Size | Exact Range (Inclusive) | Approximate Range |
long | 64 Bits | -2⁶³ to -2⁶³ – 1 | ≈ -8 × 10¹⁸ to 8 × 10¹⁸ – 1 |
The Range Approximation for long
Using the same approximation rule (2¹⁰ ≈ 10³):
- We start with the closest power of 2: 2¹⁰ ≈ 10³.
- Take the 6th power of both sides: (2¹⁰)⁶ ≈ (10³)⁶.
- This gives: 2⁶⁰ ≈ 10¹⁸.
- Multiply by 8 (or 2³) on both sides to reach 2⁶⁰ × 2³ ≈ 8 × 10¹⁸
- This will give 2⁶³ ≈ 8 × 10¹⁸
This confirms that the maximum positive value for a long is 2⁶³ – 1, or approximately 8 × 10¹⁸ – 1.
Floating-Point Precision
For decimal numbers, the focus shifts from a maximum range to precision (how many digits after the decimal point can be accurately represented).
float: Has low precision.double: Has high precision, making it the preferred choice for most decimal calculations.
4. Advanced Concepts and Input
Typecasting and Overflow
Typecasting is the process of converting a value from one data type to another.
- Safe Casting: Converting a value from a smaller type (e.g.,
int) to a larger type (e.g.,long) is safe, like pouring water from a small cup into a large mug. - Unsafe Casting: Converting a value from a larger type to a smaller type (e.g.,
longtoint) can lead to data loss.
Overflow: This occurs when a value exceeds the maximum capacity of its container (data type). For example, trying to store a number larger than the int limit will cause the value to “wrap around,” resulting in a random negative number.
Taking User Input
To read input from the console in Java, the Scanner class is used. It must be imported before use.
Input Code Structure:
import java.util.Scanner; // Must be imported at the top
class Main {
public static void main(String args[]) {
// 1. Create a Scanner object to handle input
Scanner sc = new Scanner(System.in);
// 2. Use appropriate methods to read data:
// Reads an integer
int x = sc.nextInt();
System.out.println(x);
// Reads a long integer
long y = sc.nextLong();
System.out.println(y);
// Reads a float
float f = sc.nextFloat();
System.out.println(f);
// Reads a double
double d = sc.nextDouble();
System.out.println(d);
}
}
