0% found this document useful (0 votes)
18 views7 pages

Oop Reviewer

Uploaded by

jrobertzz616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Oop Reviewer

Uploaded by

jrobertzz616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Basics & Syntax - Review Guide

Java Basics & Syntax

What is a Variable & Data Type


• Variable: Used to temporarily store data for runtime usage.
• Data Type: Indicates the type of data stored in a variable.

Most Commonly used Data Types


• Characters: char - Holds a single character
• String: String - Holds a set of characters or text
• Boolean: boolean - Holds the value of True or False
• Integers: int - Holds whole numbers without decimal points
• Double: double - Holds numbers with decimal points

Identifiers
• Identifier: The name assigned to a variable for reading and writing.

Rules of Identifiers
• No special characters except underscore
• No whitespaces
• Cannot use numbers alone
• Can use numbers alongside letters

Declaring Variables
Without value:
datatype identifier;
Example: int num;

With value:
datatype identifier = value;
Example: int num = 5;

Arithmetic Operators
Symbol | Operation | Result
+ | Addition | Sum
- | Subtraction | Difference
* | Multiplication | Product
/ | Division | Quotient
% | Modulus | Remainder
++ | Increment | Add 1
-- | Decrement | Subtract 1

Relational Operators
Symbol | Label | Syntax
== | Equals | x == y
!= | Not Equals | x != y
< | Less Than | x < y
<= | Less Than or Equal | x <= y
> | Greater Than | x > y
>= | Greater Than or Equal | x >= y

Logical Operators
Symbol | Label | Description
&& | AND | Both conditions must be true
|| | OR | Either condition must be true
! | NOT | Inverts the current condition

Control Flows
Control flows manage the execution of code in programs. Java provides several structures to
make decisions, repeat tasks, and control program flow.

Condition Statements - if Statement


if(condition){
//your code
}
else{
//alternative code
}
Example:
int x = 5;
int y = 8;
if(x > 10){
System.out.print(x);
} else {
System.out.print(y);
}

Condition Statements - switch Statement


switch(expression){
case value1:
//code
break;
default:
//default code
}
Example:
int x = 5;
switch(x){
case 1:
//code
break;
case 5:
//code
break;
}

Loops
1. For loop - Repeats a code block a specified number of times
2. While loop - Repeats a code block while condition is true
3. Do-while loop - Executes at least once before checking condition

For loop Syntax


for(initialization; condition; update){
//code
}
Example:
for(int x = 1; x < 10; x++){
System.out.print(x);
}

while loop Syntax


while(condition){
//code
}
Example:
int x = 1;
while(x < 10){
System.out.print(x);
}

do-while loop Syntax


do{
//code
} while(condition);
Example:
int x = 1;
do{
System.out.print(x);
} while(x < 10);

Branching Statements
• break - Exits loop or switch early
• continue - Skips current loop iteration
• return - Exits a method, optionally returning a value

Exception Handling - try-catch Statement


try-catch allows handling exceptions or errors during execution.

Conditional Operator - Ternary Operator


A concise way to express conditions: result = (condition) ? Value1 : Value2;

Functions or Methods
Methods are code blocks that perform specific actions when called, organizing and sorting
code within a class.

Creating Methods
modifiers returnType methodName(){
//code
}

Calling Methods
public static void main(String[] args){
methodName();
}

Writing Clean & Maintainable Code


• Use meaningful names - Descriptive names for variables, classes, methods
• Follow naming conventions - camelCase for variables, PascalCase for classes
• Use comments wisely - Explain complex code and design decisions
• Indentation & Formatting - Consistent structure
• Consistency - Keep consistent style, libraries, and frameworks
• Simplicity - Keep code simple and maintainable
Module 3: Objects and Classes - Java Review Guide

Introduction to OOP

Java Classes and Objects


• Classes: Blueprints or templates for creating objects.
• Objects: Instances of classes created with the class name and the 'new' keyword.

Java Class Attributes and Methods


• Class Attributes: Also called fields; variables within a class. Accessed via objects using dot
syntax.
• Final Keyword: Used to ensure a variable always holds the same value.
• Class Methods: Functions within a class. A 'static' method can be accessed without an
object, while 'public' methods require an object.

Java Constructors
• Constructors are special methods for initializing objects, called upon object creation to set
initial values for attributes.

Java Constructor Example

The 'this' Keyword in Java Classes and Objects


• Purpose: Refers to the current object, commonly used to distinguish class attributes from
parameters with the same name.
• Uses:
- Invoke current class constructor (constructor chaining).
- Call a current class method.

- Return the current instance.

- Pass an argument in a constructor call.

Java Packages
• Definition: Used to group related classes and avoid name conflicts.
• Types:
- Built-in Packages: Java API, included in the Java Development Environment.
- User-defined Packages: Created by developers to organize classes and improve
maintainability.

Importing Packages

You might also like