Java Primitive Data Types and Control Structures Fundamentals
Data Types:
Basic Data Types
Numeric Types
Integer types (byte, short, int, long)
| Type | Bits | Bytes | Notes | Range |
|---|---|---|---|---|
| byte | 8 | 1 | Automatically promotes to int during operations | -128 to +127 |
| short | 16 | 2 | Cannot convert directly to byte, promotes to int in calculations | -32768 to +32767 |
| int | 32 | 4 | Default integer type | -2^31 to +2^31-1 |
| long | 64 | 8 | Requires L suffix for literals | -2^63 to +2^63-1 |
Floating-point types (float, double)
| Type | Bits | Bytes | Notes |
|---|---|---|---|
| float | 32 | 4 | Requires F suffix for literals |
| double | 64 | 8 | Default floating-point type |
Character Type (char)
char uses 16 bits and 2 bytes. Values must be enclosed in single quotes and can hold a single character or Unicode value.
Boolean Type (boolean)
Contains only true and false values used for conditional logic. Does not participate in arithmetic operations.
Reference Data Types
Classes (class):
String behaves similarly to basic types but is a reference type.
Interfaces
Arrays
As reference types, arrays prevent passing parameters by value.
Type conversion requires compatibility between types.
Operators:
Arithmetic Operators
-
Addition (+)
When both operands are numeric or convertible to numeric, performs addition.
char letter = 'a'; int value = 4; int result = letter + value; // 101When one operand is String, acts as concatenation operator, returning String
int personAge = 20; String message = "Age: "; String combined = message + personAge; // "Age: 20" -
Division (/)
Integer division results in integer.
If either operand is floating-point, result is floating-point.
Integer division by zero causes arithmetic exception. When dividend is floating-point, result is infinity.
Applications:
Extracting digits from numbers.
// Extract each digit from a three-digit number int number = 123; int hundreds = number / 100; // hundreds place int tens = number % 100 / 10; // tens place int units = number % 10; // units place -
Modulus (%)
The sign of the result depends solely on the dividend: (-5) % 3 = -2, 5 % (-3) = 2.
-
Increment/Decrement (++/--)
Performs variable increment and decrement.
Operator greed rule:
When an operator detects a similar symbol immediately following, they form a pair.
int x = 10; int y = 10; int z = x+++y; // equivalent to x++ + y // x=11, y=10, z=20 int w = x+(++y); // x=11, y=11, w=22
Assignment Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
| = | a=b | a=2, b=3 | a=3, b=3 |
| += | a=a+b | a=2, b=3 | a=5, b=3 |
| -= | a=a-b | a=2, b=3 | a=-1, b=3 |
| *= | a=a*b | a=2, b=3 | a=6, b=3 |
| /= | a=a/b | a=2, b=3 | a=0, b=3 |
| %= | a=a%b | a=2, b=3 | a=2, b=3 |
Beyond direct assignment, compound assignment operators automatically perform narrowing conversions when operating with larger data types.
Comparison Operators
| Operator | Example | Result |
|---|---|---|
| == (equals) | 4==3 | false |
| != (not equals) | 4!=3 | true |
| > | 4>3 | true |
| < | 4<3 | false |
| >= | 4>=3 | true |
| <= | 4<=3 | false |
No spaces allowed within ==, !=, >=, <=, to prevent errors with ==, write constants on the left side.
Logical Operators
| Operator | Name | Function |
|---|---|---|
| && | AND | Returns true only if both sides are true |
| ! | NOT | Negates the value |
Bitwise Operators
| Operator | Operation | Purpose |
|---|---|---|
| & | Bitwise AND | Performs AND operation on each bit (both 1 yields 1) |
| Bitwise OR | ||
| << | Left shift | Converts to binary then shifts all digits left, fills with 0 |
| >> | Right shift | Converts to binary then shifts all digits right, discards excess |
| ^ | XOR | Performs exclusive OR on each bit (same = 0, different = 1) |
Similarities and Differences Between Logical and Bitwise Operators (&&, &, |, ||)
-
Both produce boolean results
-
Differences
- Logical operators work with boolean values, bitwise operators work with binary data
- Logical operators short-circuit, bitwise operators do not
-
Logical operators are more commonly used in practical development
System Interaction
- Import required packages first
import java.util.Scanner;
- Instantiate scanner object
Scanner input = new Scanner(System.in);
- Select scanning method
String userName = input.next(); // stops at whitespace
String fullText = input.nextLine(); // reads entire line
int count = input.nextInt(); // reads first integer
double amount = input.nextDouble(); // reads first floating-point number
Flow Control
Selection Structures
If Statements
Single condition
// Bonus system: everyone gets $20K, those with 5+ years get additional $20K
// Display bonus amount
public static void example1(){
Scanner reader = new Scanner(System.in);
System.out.print("Enter your years of service: ");
int experience = reader.nextInt();
double bonus = 2;
if(experience >= 5){
bonus += 2;
}
System.out.println("Bonus amount: $" + bonus + "K");
}
Two-way selection
public static void example2(){
Scanner reader = new Scanner(System.in);
// Input a three-digit number like 121 222 343
System.out.println("Enter a three-digit number:");
int inputNumber = reader.nextInt();
if(inputNumber >= 100 && inputNumber < 1000){
int firstDigit = inputNumber / 100;
int lastDigit = inputNumber % 10;
if(firstDigit == lastDigit){
System.out.println(inputNumber + " is a palindrome");
}else{
System.out.println(inputNumber + " is not a palindrome");
}
}else{
System.out.println("Please enter a valid three-digit number");
}
}
Multiple selection (calculator example)
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
System.out.println("Welcome to calculator. Instructions:" + '\n' +
"Select operation type by entering corresponding number" + '\n' +
"+ --- 0" + '\t' + "- --- 1" + '\n' +
"× --- 2" + '\t' + "÷ --- 3" + '\n' +
"% --- 4" + '\t' + "² --- 5" + '\n');
while(true){
System.out.print("Select operation: ");
Scanner scanner = new Scanner(System.in);
int operation = scanner.nextInt() % 6;
if(operation == 5){
System.out.print("Enter one operand: ");
int operand = scanner.nextInt();
operand *= operand;
System.out.println("Result: " + operand);
}
else{
System.out.println("Enter two operands");
switch(operation){
case 0:
int first = scanner.nextInt();
int second = scanner.nextInt();
System.out.println("Result: " + (first + second));
break;
case 1:
int minuend = scanner.nextInt();
int subtrahend = scanner.nextInt();
System.out.println("Result: " + (minuend - subtrahend));
break;
case 2:
int multiplier = scanner.nextInt();
int multiplicand = scanner.nextInt();
System.out.println("Result: " + (multiplier * multiplicand));
break;
case 3:
int dividend = scanner.nextInt();
int divisor = scanner.nextInt();
if (dividend * divisor == 0)
System.out.println("Invalid input");
else
System.out.println("Result: " + ((double)dividend / divisor));
break;
case 4:
int div = scanner.nextInt();
int mod = scanner.nextInt();
if (div * mod == 0)
System.out.println("Invalid input");
else
System.out.println("Result: " + (div % mod));
break;
}
}
}
}
}