JAVA 1
JAVA 1
• Machine dependent.
• Introduced keyword | add, sub, …ect.
HIGH-LEVEL LANGUAGE
A new generation of programming languages
• Use English words | easy to learn & use.
• Machine independent | your program will run
on different machines.
• Instruction are called ‘statements’.
HIGH-LEVEL LANGUAGE
• A program written in high-level language is
called ‘source program’ or ‘source code’.
• To add 2 & 3the result: result = 2 + 3;
1. Primitive
2. Non-primitive
Primitive data types
1. Numeric Types:
• int: Stores whole numbers (integers)
– Ex: int age = 25;
• double: Stores floating-point numbers with
decimals
– Ex: double pi = 3.14159;
• float: Similar to double but uses less memory
– Ex: float distance = 10.5f;
Primitive data types
• byte: Stores small whole numbers (8 bits)
– Ex: byte initial = -128;
• short: Stores whole numbers larger than bytes
(16 bits)
– Ex: short temperature = 30;
• long: Stores very large whole numbers (64
bits)
– Ex: long worldPopulation = 8_000_000_000L;
Primitive data types
2. Character Type:
– char: Stores a single character enclosed in single
quotes
• Ex: char initial = 'A';
3. Boolean Type:
– boolean: Represents logical values (true or false)
• Ex: boolean isComplete = true;
Non-Primitive data types
• complex data structures that can hold collections
of other data or represent real-world entities.
• String: Represents a sequence of characters
– Ex: String name = "Alice";
• Arrays: Ordered collections of elements of the
same data type
– Example: int[] numbers = {1, 5, 9};
• Classes: Blueprints for creating objects
– Example: class Person { ... }
Variables
• like named boxes that hold data.
• Act as containers to store information.
• Each variable has a specific data type.
Variables
• Declaring Variables:
• Data Type: The type of information the
variable will store (e.g., int for
integers, String for text).
• Name: A unique identifier you'll use to refer to
the variable (follows specific naming rules).
Operators
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Conditional (Ternary) Operator
7. Bitwise Operators (advanced)
8. Shift Operators (advanced)
9. Instanceof Operator
Arithmetic Operators
• +: Addition (e.g., int sum = 10 + 5;)
• -: Subtraction (e.g., int difference = 20 - 7;)
• *: Multiplication (e.g., int product = 3 * 4;)
• /: Division (e.g., double result = 10.0 / 3.0;)
• %: Modulo (remainder after division) (e.g., int
remainder = 11 % 3;)
Assignment Operators
• These are used to assign values to variables:
• Use for loops when you know the exact number of iterations
beforehand (e.g., iterating over an array).
• Use while loops when the condition for continuing the loop
may change dynamically based on user input or calculations.
• Use do-while loops when you need the code block to execute
at least once, regardless of the initial condition.
Methods
• methods are reusable blocks of code that
perform specific tasks.
Methods
• Structure of methods:
1. Modifiers (optional): Keywords like public, private, or static that control
access and behavior.
2. Return Type: The data type of the value the method will return (can
be void if it doesn't return anything).
3. Method Name: A unique identifier that reflects the method's purpose
(follows naming conventions).
4. Parameters (optional): A comma-separated list of variables enclosed in
parentheses that accept values when the method is called.
5. Method Body: The block of code enclosed in curly braces {} that defines
the actions the method performs.
Methods
• Types of Methods:
• Void Methods: Don't return any value (use void as the return
type).
• Value-Returning Methods: Return a specific data type using
the return statement within the method body
Methods
• Ex:
Methods
• Benefits of Methods:
• Reusability: Write code once and use it multiple times
throughout your program or even in other programs.
• Modularity: Break down complex programs into smaller,
manageable functions.
• Readability: Improve code organization and clarity by
grouping related functionalities.
• Maintainability: Easier to modify or debug specific parts of
the code by focusing on individual methods.
Java Programming Fundamental & OOP
• Object-Oriented Programming (OOP)
1. Classes
2. Objects
3. Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
7. Exceptions
8. Abstract classes
9. Interfaces
10. File (I/O)