Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

INTRODUCTION

COMPUTER PROGRAMMING <COM01>


Program
● a sequence or set of instructions in a
programming language for a computer
to execute.
Programming
Language
● A formal language, which comprises a
set of instructions that produce various
kinds of output.
Program
Language Levels
● High-Level Language

● Low-Level Language
○ Assembly
○ Machine Code
Tools for Program
Creation
● Text Editor

● Compiler
Integrated
Development
Environment (IDE)
● a software application that provides
comprehensive facilities to computer
programmers for software
development.

● A combination of text editor, source


code compilation, and debugging tools.
Sample
Program
Classes
● Used to define data types and
methods of an object

● Maybe referred as the “Blueprint”


of the objects of the program
Methods

● a block of code which only


runs when it is called.

● also known as functions


OPERATORS
COMPUTER PROGRAMMING <COM01>
OPERATORS
● symbols that represent
simple computations.
● used to perform
operations on variables
and values.
JAVA
OPERATORS
● Arithmetic
● Assignment
● Comparison
● Logical
● Bitwise and Shift
ARITHMETIC
OPERATORS
● used to perform common
mathematical operations.
ARITHMETIC OPERATORS
Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by x++


1

-- Decrement Decreases the value of a variable by x--


1
ASSIGNMENT
OPERATORS
● used to assign values to
variables while operating
the value.
ASSIGNMENT OPERATORS
Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


COMPARISON
OPERATORS
● used to compare two
values
● return value of a either
true or false
COMPARISON OPERATORS
Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


LOGICAL
OPERATORS
● used to determine the
logic between variables or
values
LOGICAL OPERATORS
Operator Name Description Example

&& Logical and Returns true if both statements x < 5 && x < 10
are true

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical not Reverse the result, returns false if !(x < 5 && x < 10)
the result is true
BITWISE
OPERATORS
● perform operations on
integer data at the
individual bit-level
BITWISE OPERATORS
Operator Description

| Bitwise OR

& Bitwise AND

^ Bitwise XOR

~ Bitwise Complement

<< Left Shift

>> Signed Right Shift

>>> Unsigned Right Shift


CONDITIONAL
STATEMENTS
COMPUTER PROGRAMMING <COM01>
CONDITIONAL
STATEMENTS
● statements which
evaluates actions in
the program and
evaluates if it's true or
false
if
Used specify a block of Java code to be executed if a condition is
true
else if
statement to specify a new condition if the first condition is
false
else
Used specify a block of Java code to be executed if a condition is
false
switch
statement selects one of many code blocks to be executed
Programming
Variables
COMPUTER PROGRAMMING <COM01>
Variable
a value that can change, depending
on conditions or on information
passed to the program

runtime storage of data.


Types of
Variables
• Constants
• Global variables
• Class variables
• Instance variables
• Local variables.
Data Types
● determines the values that the
variable can have and the
operations that can be
performed on it
Data Types
Groups
● Primitive Data Types - includes
byte, short, int, long, float,
double, boolean and char
● Non-primitive data types - such
as String, Arrays and Classes
(you will learn more about these
in a later chapter)
Primitive
Data Types
● A type with specified size and
type of variable values, and it has
no additional methods.
Primitive Data Types
Data Type Size Description

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values


Non-Primitive
Data Types
● Also called as reference
types because they refer to
objects.
Primitive vs.
Non-Primitive
• Primitive types are predefined (already
defined) in Java. Non-primitive types are
created by the programmer and is not
defined by Java (except for String).
• Non-primitive types can be used to call
methods to perform certain operations,
while primitive types cannot.
• A primitive type has always a value, while
non-primitive types can be null.
• A primitive type starts with a lowercase
letter, while non-primitive types starts
with an uppercase letter.
Modifiers
● keywords that you add to
those definitions to change
their meanings.
● It can be Access and Non-
Access Modifier
Access
Modifier
● Private - Visible to the class
only
● Public – Visible globally
● Protected - Visible to the
package and all subclasses.
Non-Access
Modifiers
● “static" keyword is used to
create variables that will
exist independently of any
instances created for the
class. Only one copy of the
static variable exists
regardless of the number of
instances of the class.
Non-Access
Modifiers
● “final” keyword is used to
create variable can be
explicitly initialized only
once
Non-Access
Modifiers
● “abstract” keyword is used
to create a placeholder
method or variable for an
abstract class

You might also like