[go: up one dir, main page]

0% found this document useful (0 votes)
9 views12 pages

Oop

This document serves as a reference guide for Object Oriented Programming using Java, covering key concepts such as Java syntax, variables, data types, operators, and control structures like if-else and switch statements. It highlights the advantages of Java, including its platform independence, ease of learning, and strong community support. The document includes practical examples to illustrate the usage of Java programming elements.

Uploaded by

baghalialibhai
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)
9 views12 pages

Oop

This document serves as a reference guide for Object Oriented Programming using Java, covering key concepts such as Java syntax, variables, data types, operators, and control structures like if-else and switch statements. It highlights the advantages of Java, including its platform independence, ease of learning, and strong community support. The document includes practical examples to illustrate the usage of Java programming elements.

Uploaded by

baghalialibhai
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/ 12

Object oriented programming

Engr. Rakib Mustafa

FEBRUARY 11, 2025


Language: java
This document is for reference only
Object oriented programming

Contents
Week 1...................................................................................................................................................2
Java....................................................................................................................................................2
Why Use Java?...............................................................................................................................2
Syntax............................................................................................................................................2
The main Method..........................................................................................................................2
Java Variables.................................................................................................................................3
Declaring (Creating) Variables........................................................................................................4
Final Variables................................................................................................................................4
Identifiers.......................................................................................................................................4
Real Life Example of Variables:......................................................................................................5
Java Data Types..............................................................................................................................5
Java Operators...............................................................................................................................6
Java Comparison Operators...........................................................................................................6
Java Logical Operators...................................................................................................................6
Java Strings....................................................................................................................................7
Java Math.......................................................................................................................................8
Java If ... Else......................................................................................................................................8
Java Switch.......................................................................................................................................10

1
Object oriented programming

Week 1
Java
Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:
 Mobile applications (specially Android apps)
 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection
 And much, much more!
Why Use Java?
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It has a large demand in the current job market
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has huge community support (tens of millions of developers)
Java is an object-oriented language which gives a clear structure to programs and allows code to be
reused, lowering development costs

As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa.

Syntax

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

Example explained

Every line of code that runs in Java must be inside a class. And the class name should always start
with an uppercase first letter. In our example, we named the class Main.

Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.

The main Method


The main() method is required and you will see it in every Java program:

public static void main(String[] args)

2
Object oriented programming

Any code inside the main() method will be executed. Don't worry about the keywords before and
after it. You will get to know them bit by bit while reading this tutorial.

For now, just remember that every Java program has a class name which must match the filename,
and that every program must contain the main() method.

System.out.println()

Inside the main() method, we can use the println() method to print a line of text to the screen:

public static void main(String[] args) {

System.out.println("Hello World");

Note: The curly braces {} marks the beginning and the end of a block of code.

System is a built-in Java class that contains useful members, such as out, which is short for "output".
The println() method, short for "print line", is used to print a value to the screen (or a file).

Don't worry too much about how System, out and println() works. Just know that you need them
together to print stuff to the screen.

You should also note that each code statement must end with a semicolon (;).

Example:

System.out.println("Hello World!");

System.out.println("I am learning Java.");

System.out.println("It is awesome!");

Java Variables
Variables are containers for storing data values.

In Java, there are different types of variables, for example:

3
Object oriented programming

String - stores text, such as "Hello". String values are surrounded by double quotes

int - stores integers (whole numbers), without decimals, such as 123 or -123

float - stores floating point numbers, with decimals, such as 19.99 or -19.99

char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

boolean - stores values with two states: true or false

Declaring (Creating) Variables

Final Variables
If you don't want others (or yourself) to overwrite existing values, use the final keyword (this will
declare the variable as "final" or "constant", which means unchangeable and read-only):

final int myNum = 15;

myNum = 20; // will generate an error: cannot assign a value to


a final variable

Identifiers
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

4
Object oriented programming

Note: It is recommended to use descriptive names in order to create understandable and


maintainable code:

// Good

int minutesPerHour = 60;

// OK, but not so easy to understand what m actually is

int m = 60;

Real Life Example of Variables:

// 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);

Java Data Types

int myNum = 5; // Integer (whole number)

float myFloatNum = 5.99f; // Floating point number

char myLetter = 'D'; // Character

5
Object oriented programming

boolean myBool = true; // Boolean

String myText = "Hello"; // String

Java Operators
Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

int x = 100 + 50;

Java Comparison Operators

int x = 5;

int y = 3;

System.out.println(x > y); // returns true, because 5 is higher


than 3

Java Logical Operators


You can also test for true or false values with logical operators.

Logical operators are used to determine the logic between variables or values:

6
Object oriented programming

Java Strings
Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes:

String greeting = "Hello";

String Length
A String in Java is actually an object, which contain methods that can perform certain operations on
strings. For example, the length of a string can be found with the length() method:

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

System.out.println("The length of the txt string is: " +


txt.length());

More String Methods


There are many string methods available, for example toUpperCase() and toLowerCase():

String txt = "Hello World";

System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"

System.out.println(txt.toLowerCase()); // Outputs "hello world"

Finding a Character in a String


The indexOf() method returns the index (the position) of the first occurrence of a specified text in a
string (including whitespace):

String txt = "Please locate where 'locate' occurs!";

System.out.println(txt.indexOf("locate")); // Outputs 7

Java counts positions from zero.


0 is the first position in a string, 1 is the second, 2 is the third ...

String Concatenation

String firstName = "John";

7
Object oriented programming

String lastName = "Doe";

System.out.println(firstName + " " + lastName);

Note that we have added an empty text (" ") to create a space between firstName and lastName on
print.

You can also use the concat() method to concatenate two strings:

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName.concat(lastName));

Java Math
Math.max(x,y)

The Math.max(x,y) method can be used to find the highest value of x and y:

Math.max(5, 10);

Math.min(5, 10);

Math.sqrt(64);

Java If ... Else


This example shows how you can use if..else to "open a door" if the user enters the correct code:

int doorCode = 1337;

if (doorCode == 1337) {

System.out.println("Correct code. The door is now open.");

} else {

System.out.println("Wrong code. The door remains closed.");

This example shows how you can use if..else to find out if a number is positive or negative:

int myNum = 10; // Is this a positive or negative number?

8
Object oriented programming

if (myNum > 0) {

System.out.println("The value is a positive number.");

} else if (myNum < 0) {

System.out.println("The value is a negative number.");

} else {

System.out.println("The value is 0.");

Find out if a person is old enough to vote:

int myAge = 25;

int votingAge = 18;

if (myAge >= votingAge) {

System.out.println("Old enough to vote!");

} else {

System.out.println("Not old enough to vote.");

Find out if a number is even or odd:

int myNum = 5;

if (myNum % 2 == 0) {

System.out.println(myNum + " is even");

} else {

System.out.println(myNum + " is odd");

9
Object oriented programming

Java Switch
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
The break and default keywords are optional, and will be described later in this chapter
The example below uses the weekday number to calculate the weekday name:

int day = 4;

switch (day) {

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

10
Object oriented programming

System.out.println("Sunday");

break;

default:

System.out.println("Looking forward to the Weekend");

// Outputs "Thursday" (day 4)


The default keyword specifies some code to run if there is no case match:

When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest of the
code in the switch block.

11

You might also like