§ — — Object-Oriented Programming with Java
This lesson introduces object-oriented programming (OOP), the Java platform, the structure of a basic Java program, variables, data types, and operators.
By the end of the lesson, a learner should be able to explain common OOP features, describe how Java source code becomes executable bytecode, write valid variable declarations, choose appropriate primitive data types, and use basic Java operators.
Procedural programming organizes a program as a sequence of instructions. The program often moves step by step through statements, decisions, loops, and procedure calls.
Object-oriented programming organizes a program around objects. An object combines data and behavior in one unit. In Java, objects are created from classes. This style is useful for larger programs because it lets developers model real-world entities, reuse code, and keep related logic together.
Java is both a programming language and a platform. Java source code is written in .java files, compiled into bytecode, and then executed by the Java Virtual Machine (JVM).
The main idea is portability: the same compiled bytecode can run on any system that has a compatible JVM. This is often described as write once, run anywhere.
The Java platform includes:
| Component | Purpose |
|---|---|
| JVM | Runs Java bytecode on a specific machine |
| Java API | Provides ready-made classes and packages for common tasks such as input, math, strings, GUI, files, and networking |
A basic Java application normally contains a class and a main method. The main method is the starting point of execution.
Important parts of a simple Java program:
| Part | Meaning |
|---|---|
public class ClassName | Defines a class. If the class is public, the filename should match the class name. |
{ } | Curly braces mark the beginning and end of a class or method body. |
public static void main(String[] args) | Main method where program execution starts. |
System.out.println() | Prints output and moves to the next line. |
; | Ends most Java statements. |
Create a file named HelloJava.java and run it.
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello, Java learner!");
}
}
Expected output:
Hello, Java learner!
Free Sample
That was 1 of 52 reviewers with answer keys in Object-Oriented Programming with Java. Unlock all of them for the semester.
Unlock all reviewers →Comments are notes for human readers. The Java compiler ignores them.
| Comment type | Syntax | Common use |
|---|---|---|
| Line comment | // comment | Short note on one line |
| Block comment | /* comment */ | Multi-line explanation |
| Documentation comment | /** comment */ | API-style documentation for classes and methods |
A variable is a named storage location. In Java, every variable must have a data type.
Example:
int age = 20;
double price = 99.75;
char grade = 'A';
boolean isPassed = true;
Common primitive data types:
| Type | Typical use |
|---|---|
byte | Very small integers |
short | Small integers |
int | Standard whole numbers |
long | Very large whole numbers |
float | Decimal values with less precision |
double | Standard decimal values |
char | A single character |
boolean | true or false |
A Java variable name must be a valid identifier. It cannot be a reserved keyword, and it cannot duplicate another variable name in the same scope.
Common naming conventions:
score, totalAmount.StudentRecord.isVisible, studentAge.A variable may be given a value when it is declared.
int count = 0;
A final variable can be assigned only once.
final double TAX_RATE = 0.15;
After a final variable receives a value, attempting to assign a new value will cause a compile-time error.
Arithmetic operators perform numeric calculations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder | a % b |
Relational operators compare values and produce a boolean result.
| Operator | Meaning |
|---|---|
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
Conditional operators combine or reverse boolean expressions.
| Operator | Meaning |
|---|---|
&& | True when both conditions are true; short-circuits |
| ` | |
! | Reverses a boolean value |
?: | Ternary shorthand for simple if-else decisions |
Assignment shortcuts combine calculation and assignment.
x += 2; // same as x = x + 2
x -= 1; // same as x = x - 1
x *= 3; // same as x = x * 3
x /= 2; // same as x = x / 2
x %= 5; // same as x = x % 5
An expression is a combination of values, variables, operators, and method calls that evaluates to one result.
Examples:
int total = price * quantity;
boolean isAdult = age >= 18;
int larger = (a > b) ? a : b;
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Run the code below. Try changing the variable values and add your own.
Done with this module? Track it — your progress shows on the subject list.
Up next
Input and Control Statements→