In Java, certain predefined words are reserved for specific purposes and must be used according to their syntax rules. These keywords are generally lowercase, such as public, void, int, and boolean.
- Identifiers
A identifier is a name given to elements like variables, methods, classes, packages, or constants in Java. Identifiers follow naming conventions:
- Identifiers can consist of letters, digits, underscores (
_), and dollar signs ($). The first character cannot be a digit. - Identifiers cannot be keywords.
- They should be meaningful and descriptive.
- Avoid abbreviations if they reduce clarity.
- Package names should be all lowercase, class names should start with an uppercase letter, and constant names should be fully uppercase.
- Follow camelCase naming conventions where appropriate (e.g.,
myVariableName).
- Comments
Java supports three types of comments: single-line, multi-line, and documentation comments.
// Single-line comment
/*
Multi-line comment
*/
/**
* Documentation comment
* @author Example Author
* @version 1.0
*/
- Class Structure
A class describes the properties and behaviors of a group of objects. Attributes defined within a class are accessible throughout the class. Methods define actions that can be performed on those attributes.
// Instantiation example
MyClass obj = new MyClass();
// Constructor example
public MyClass(String property1, int property2) {
// Initialization logic
}
- Data Types
Java data types include primitive types and reference types.
| Data Type | Name | Size (bytes) | Range | Default Value |
|---|---|---|---|---|
| Character | char | 2 | 0 to 65,535 | '\u0000' |
| Byte | byte | 1 | -128 to 127 | 0 |
| Short | short | 2 | -32,768 to 32,767 | 0 |
| Integer | int | 4 | -2^31 to 2^31-1 | 0 |
| Long | long | 8 | -2^63 to 2^63-1 | 0L |
| Float | float | 4 | 1.4E-45 to 3.4028235E38 | 0.0F |
| Double | double | 8 | 4.9E-324 to 1.7976931348623157E308 | 0.0 |
| Boolean | boolean | N/A | true/false | false |
Type conversion includes implicit and explicit conversions. Implicit conversions occur automatically when converting smaller types to larger ones (e.g., byte to int). Explicit conversions require casting and may result in data loss.
// Implicit Conversion
int num = 10;
double dNum = num;
// Explicit Conversion
double value = 12.34;
int iValue = (int)value;
- Wrapper Classes
Wrapper classes allow primitive types to be treated as objects. Each primitive type has a corresponding wrapper class.
| Primitive | Wrapper |
|---|---|
| char | Character |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
Autoboxing converts primitives to wrappers, while unboxing does the reverse.
// Autoboxing and Unboxing
Integer wrappedInt = 10; // Autoboxing
int primitiveInt = wrappedInt; // Unboxing
- Variables and Constants
Variables store data that can change during program execution. Constants use the final keyword to prevent modification.
// Variable Declaration
int variable = 10;
// Constant Declaration
final int CONSTANT = 20;
- Escape Characters
| Escape Sequence | Description |
|---|---|
| \n | Newline |
| \t | Tab |
| \r | Carriage return |
| \\ | Backslash |
| \' | Single quote |
| " | Double quote |
- Enput and Output
Java provides simple mechanisms for input and output operations.
// Output Example
System.out.println("Hello, World!");
// Input Example
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
scanner.close();