Java Programming Notes - Abdulmalik
1. Arithmetic Operators
Arithmetic operators are used for basic math operations:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
Example:
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a % b); // 1
2. Relational Operators
Used to compare values (results are true/false).
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Example:
int a = 10, b = 5;
System.out.println(a > b); // true
Java Programming Notes - Abdulmalik
3. Logical Operators
Used to combine multiple conditions.
&& Logical AND
|| Logical OR
! Logical NOT
Example:
if (a > b && b > 0) {...}
4. String Concatenation
Used to join strings.
+ "Hello" + " World"
concat() "Java".concat(" Programming")
Example:
String name = "Abdul";
System.out.println("Hello " + name);
5. Type Casting
Converting one data type into another.
Widening (automatic):
int a = 10;
double b = a;
Java Programming Notes - Abdulmalik
Narrowing (manual):
double x = 9.8;
int y = (int) x;
String to int:
int num = Integer.parseInt("123");
6. Control Structures
Used to control program flow.
if, else, else if
switch (for multiple cases)
Loops: for, while, do-while
Jump: break, continue, return
7. Arrays
Fixed-size collection of same data type.
int[] nums = {1, 2, 3};
Loop:
for (int n : nums) {
System.out.println(n);
8. Scanner Class
Java Programming Notes - Abdulmalik
Used for user input.
import java.util.Scanner;
Scanner input = new Scanner(System.in);
int age = input.nextInt();
String name = input.nextLine();
9. Constructors
Special methods to initialize objects.
Default constructor:
Person() { name = "Unknown"; }
Parameterized constructor:
Person(String n) { name = n; }
Called automatically when object is created.
10. Methods
Reusable block of code.
void greet() { ... }
int add(int a, int b) { return a + b; }
Method overloading: same name, different parameters.