Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Real-Life Examples

Real-Life Examples

In our examples, variable names are often simplified to reflect their data type (e.g., using “myInt” or “myNum” for integer types and “myChar” for character types) to prevent confusion.

For a practical illustration of variable usage, consider the following program that manages various data for a college student.

Example

// Student data

String studentName = “John Doe”;

int studentID = 15;

int studentAge = 23;

float studentFee = 75.25f;

char studentGrade = ‘B’;

// Print variables

System.out.println(“Student name: “ + studentName); System.out.println(“Student id: “ + studentID); System.out.println(“Student age: “ + studentAge); System.out.println(“Student fee: “ + studentFee); System.out.println(“Student grade: “ + studentGrade);  

 

Calculate the Area of a Rectangle

In this practical example, we develop a program to compute the area of a rectangle by multiplying its length and width:

Example

// Create integer variables

int length = 4;

int width = 6;

int area;

// Calculate the area of a rectangle

area = length * width

// Print variables

System.out.println(“Length is: “ + length);

System.out.println(“Width is: “ + width);

System.out.println(“Area of the rectangle is: “ + area);