Java Basics - Unit 1 Notes
1. Object-Oriented Programming (OOP):
Java uses objects and classes. Key principles:
- Encapsulation: Wrap data and methods together
- Inheritance: Child class gets properties from parent
- Polymorphism: One function behaves differently in different classes
- Abstraction: Hiding complexity
2. A First Simple Program:
class Hello {
public static void main(String[] args) {
[Link]("Hello, Java!");
3. A Second Short Program:
class Add {
public static void main(String[] args) {
int a = 5, b = 10;
int sum = a + b;
[Link]("Sum = " + sum);
4. Two Control Statements:
- if-else: decision making
- for loop: repeating tasks
5. Using Blocks of Code:
Code inside { } is a block. It groups statements.
6. Lexical Issues:
Rules of Java code: keywords, identifiers, literals, comments, whitespaces.
7. Java Class Libraries:
Prebuilt useful classes. Examples:
- [Link] for input
- [Link] for file handling
8. Java is a Strongly Typed Language:
Must declare the data type of every variable.
9. Primitive Types:
- int, float, double, char, boolean, byte, short, long
10. Integers, Floating-Point Types, Characters, Booleans:
- Integer: int, long
- Floating-point: float, double
- Character: char
- Boolean: true/false
11. A Closer Look at Literals:
Fixed values in code: 10, 5.5f, 'A', true
12. Variables:
Named memory location. Example: int age = 20;
13. Type Conversion and Casting:
- Implicit: automatic (int to double)
- Explicit: manual (double to int)
14. Automatic Type Promotion in Expressions:
Small types get promoted. Example: byte + byte = int
15. Arrays:
Store multiple values.
- 1D: int[] arr = {10, 20};
- 2D: int[][] mat = { {1, 2}, {3, 4} };
16. A Few Words About Strings:
String is a group of characters. Example:
String name = "Java"; [Link]() gives 4