History and Features of Java
Java Basics
This section covers fundamental Java concepts that form the building blocks for writing Java programs.
1. **Keywords and Identifiers**
- Keywords are reserved words with predefined meanings in Java (e.g., `class`, `public`, `static`, `int`, `if`,
`else`).
- Identifiers are names given to classes, methods, variables, etc. They must begin with a letter, underscore
(_) or dollar sign ($).
2. **Data Types and Variables**
- Data types define the type of data a variable can hold.
-> Primitive: byte, short, int, long, float, double, char, boolean
-> Non-primitive: String, Arrays, Classes
- Variables store data values. Syntax: `int age = 25;`
3. **Operators**
- Java supports various operators:
-> Arithmetic: +, -, *, /, %
-> Relational: ==, !=, >, <, >=, <=
-> Logical: &&, ||, !
-> Assignment: =, +=, -=, *=, /=
-> Unary: ++, --
-> Ternary: condition ? expr1 : expr2
History and Features of Java
4. **Type Casting**
- Converting one data type to another.
-> Implicit (widening): int to long
-> Explicit (narrowing): double to int
Example: `int a = (int) 3.14;`
5. **Input and Output**
- `[Link]()` is used to print output to the console.
- To take input, use the Scanner class:
import [Link];
Scanner sc = new Scanner([Link]);
int num = [Link]();
6. **Comments in Java**
- Single-line: `// This is a single-line comment`
- Multi-line: `/* This is a
multi-line comment */`
- Used for documentation and making code more understandable.