Variables and Data Types
## Learning Objectives
- Declare and initialize variables
- Understand primitive types
- Work with reference types
- Master type conversion
## Variables
### Declaration
```java
int age; // Declaration
age = 25; // Assignment
int score = 100; // Declaration + Initialization
```
### Naming Rules
- Start with letter, underscore, or dollar sign
- Can contain numbers (after first character)
- Cannot use reserved words
- Case-sensitive
```java
int age; // Valid
int _count; // Valid
int $total; // Valid
int 2ndPlace; // Invalid
int class; // Invalid (reserved word)
```
## Primitive Data Types
### Integer Types
| Type | Size | Range |
|------|------|-------|
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2.1B to 2.1B |
| long | 8 bytes | Very large |
```java
byte b = 100;
short s = 32000;
int i = 2000000;
long l = 9000000000L; // Note the L suffix
```
### Floating-Point Types
| Type | Size |
|------|------|
| float | 4 bytes |
| double | 8 bytes |
```java
float pi = 3.14f; // Note the f suffix
double precise = 3.14159265359;
```
### Character Type
```java
char grade = 'A';
char symbol = '\u0041'; // Unicode
```
### Boolean Type
```java
boolean isActive = true;
boolean hasPermission = false;
```
## Reference Types
### Strings
```java
String name = "Alice";
String greeting = new String("Hello");
```
### Arrays
```java
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[3];
```
### Classes
```java
String name = "Alice"; // String is a class
Scanner scanner = new Scanner(System.in);
```
## Type Conversion
### Widening (Automatic)
```java
int i = 100;
long l = i; // int to long (automatic)
double d = i; // int to double (automatic)
```
### Narrowing (Explicit)
```java
double d = 3.99;
int i = (int) d; // Truncates to 3
```
### String to Number
```java
String s = "123";
int i = Integer.parseInt(s);
double d = Double.parseDouble(s);
// With error handling
try {
int i = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("Invalid number");
}
```
### Number to String
```java
int i = 42;
String s = String.valueOf(i);
String s2 = Integer.toString(i);
String s3 = "" + i; // Concatenation
```
## Constants
```java
final double PI = 3.14159;
final int MAX_SIZE = 100;
// Attempting to modify results in error
// PI = 3.14; // Compile error!
```
## Variables Scope
```java
public class Scope {
int global = 10; // Instance variable
public void method() {
int local = 20; // Local variable
for (int i = 0; i < 5; i++) { // Block variable
// i only exists in this loop
}
}
}
```
## Primitive vs Reference
```java
// Primitive - copy of value
int a = 10;
int b = a;
b = 20;
// a is still 10
// Reference - copy of reference
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1;
arr2[0] = 99;
// arr1[0] is also 99 (same array)
```
## var Keyword (Java 10+)
```java
var name = "Alice"; // Compiler infers String
var age = 25; // Compiler infers int
var list = new ArrayList(); // Long type inferred
```
## Summary
- Variables store data values
- 8 primitive types: byte, short, int, long, float, double, char, boolean
- Reference types: Strings, Arrays, Objects
- Use `final` for constants
- Widening is automatic; narrowing requires cast
- `Integer.parseInt()` converts String to int
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →