Pp Java - Copy
Pp Java - Copy
OBJECT-ORIENTED
PROGRAMMING
[COS2182]
BASICS IN JAVA
PROGRAMMING
value.
Java actually has three kinds of variables:
instance variables,
local variables
Instance variables: are used to define
attributes or the state for a particular
object.
Class variables: are similar to instance
variable name.
Syntax: datatype variableName;
Declaring Variables
Example: a variable that stores an integer
representing the highest score on an exam
could be declared as follows:
Declaring Variables(continued)
Other examples:
double Old_Price;
double radius;
int qtty;
char grade;
int x;
Assignment Statements and
Expressions
After a variable is declared, you
can assign a value to it by using
an assignment statement (=).
The syntax as follows:
variableName = expression;
For Example: highScore = 98;
Where it is used?
Expression – represents a computation involving values,
variables, and operators that evaluates to a value.
For example:
int x = 5; //assign 5 to a variable x
double radius = 2.3; //assign 2.3 to a variable radius
expression to x
area = radius * radius * 3.142; //compute area
POP QUIZ
Statements
A statement is a command that
causes something to happen.
All statements in Java are separated
by semicolons ;
Example:
System.out.println(“Hello, World”);
Introduction to Strings
Strings consist of a series of characters inside
double quotation marks.
Examples statements assign String variables:
assignment statements
logical comparisons
Examples:
3 + 5 // uses + operator
14 + 5 – 4 * (5 – 3) // uses +, -, * operators
The Operator Groups
There are 5 different groups of
operators:
Arithmetic operators
Assignment operator
Increment/Decrement operators
Relational operators
Conditional operators
Arithmetic Operators
Java has 6 basic arithmetic operators
+ add
- subtract
* multiply
/ divide
% modulo (remainder)
Order of operations (or precedence) when evaluating
an expression is the same as PEMDAS:
1. Parentheses
2. Exponents
a) (10 + 15) / 5 = 5
b) 10 + (15 / 5) = 13
Reading Input from the Console
Enables the program to accept
input from the user.
Java uses:-
System. out to refer to the standard output device.
System.in to the standard input device.
count = count + 1;
can be written as:
++count; or count++;
++ is called the increment operator.
count = count - 1;
Decision Statements
Loops
Decision Statements
while loops
for loops
course)
The while Loop
while (expression){
statement
}
This while loop executes as long as the given
logical expression between parentheses is true.
When expression is false, execution continues