Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Notes 5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

OBJECT ORIENTED

PROGRAMMING
Creating a Class
Creating a Class

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

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

Any code inside the main() method will be executed.


System.out.println()

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

Comments can be used to explain Java code, and to make it more


readable. It can also be used to prevent execution when testing
alternative code.
Single-line Comments

Single-line comments start with two forward slashes (//).


Any text between // and the end of the line is ignored by Java (will not
be executed).
It is preferred to use comments before the code itself to notify the
user or for more organized coding.
Single-line Comments
Multi-line Comments

Multi-line comments start with /* and ends with */.


Any text between /* and */ will be ignored by Java.
Multi-line Comments

Relationships are the connection between things. The four types of


relationships that can be represented in UML are −
• Dependency
• Association
• Generalization
• Realization
Exercise
Java Variables

Variables are containers for storing data values.


In Java, there are different types of variables, for example:
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
To create a variable, you must specify the type and assign it a value:

Where type is one of Java's types (such as int or String), and


variableName is the name of the variable (such as x or name). The
equal sign is used to assign values to the variable.
String

To create a variable that should store text, look at the following
example:
int

To create a variable that should store a number, look at the following
example:
int

You can also declare a variable without assigning the value, and
assign the value later:
int

Note that if you assign a new value to an existing variable, it will


overwrite the previous value:
Final Variables

However, you can add the final keyword if you don't want others (or
yourself) to overwrite existing values (this will declare the variable as
"final" or "constant", which means unchangeable and read-only):
Other Types

A demonstration of how to declare variables of other types:


Display Variables

The println() method is often used to display variables.


To combine both text and a variable, use the + character:
Display Variables

You can also use the + character to add a variable to another


variable:
Display Variables
For numeric values, the + character works as a mathematical operator (notice
that we use int (integer) variables here):

x stores the value 5


y stores the value 6
Then we use the println() method to display the value of x + y, which is 11
Declare Many Variables

To declare more than one variable of the same type, use a comma-
separated list:
Java 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).
It is recommended to use descriptive names in order to create
understandable and maintainable code.
Java Identifiers
Naming Variables
The general rules for naming variables are:

Names can contain letters, digits, underscores, and dollar signs


Names must begin with a letter
Names should start with a lowercase letter and it cannot contain whitespace
Names can also begin with $ and _ (but we will not use it on our lecture)
Names are case sensitive ("myVar" and "myvar" are different variables)
Reserved words (like Java keywords, such as int or boolean) cannot be used as names
Exercise
Java Type Casting

Type casting is when you assign a value of one primitive data type to
another type.
In Java, there are two types of casting:
Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when passing a smaller size
type to a larger size type:
Narrowing Casting
Narrowing casting must be done manually by placing the type in
parentheses in front of the value:
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:
Java Operators (cont....)

Although the + operator is often used to add together two values, like
in the example before, it can also be used to add together a variable
and a value, or a variable and another variable:
Type of Java Operator

Java divides the operators into the following groups:

Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.


Assignment Operators

Assignment operators are used to assign values to variables.


In the example below, we use the assignment operator (=) to assign the value 10 to
a variable called x:

The addition assignment operator (+=) adds a value to a variable:


Assignment Operators
A list of all assignment operators:
Comparison Operators
Comparison operators are used to compare two values:
Logical Operators
Logical operators are used to determine the logic between variables or values:
Java Strings
Strings are used for storing text.
A String variable contains a collection of characters surrounded by
double quotes:
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:
More String Methods
There are many string methods available, for example toUpperCase() and
toLowerCase():
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 Concatenation
The + operator can be used between strings to combine them. This is
called concatenation
String Concatenation(cont....)
You can also use the concat() method to concatenate two strings:
Special Characters

Because strings must be written within quotes, Java will


misunderstand this string, and generate an error:
Special Characters (cont....)
The solution to avoid this problem, is to use the backslash escape
character.
The backslash (\) escape character turns special characters into
string characters:
Special Characters (cont....)
Special Characters (cont....)
Six other escape sequences are valid in Java:
Adding Numbers and Strings

If you add two numbers, the result will be a number:

If you add two strings, the result will be a string concatenation:

If you add a number and a string, the result will be a string
concatenation:
End of Introduction to
Object Oriented
Programming

Prepared By :

Engr. Jovialis Llanita

You might also like