[go: up one dir, main page]

0% found this document useful (0 votes)
2 views6 pages

JAVA Week3

This learning guide covers Java basic operators and program structure, detailing various types of operators including arithmetic, relational, bitwise, logical, assignment, and the ternary operator. It also outlines the structure of a Java program, including documentation, package statements, import statements, class definitions, and the main method. Additionally, it includes hands-on activities for writing Java programs that utilize these concepts.
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)
2 views6 pages

JAVA Week3

This learning guide covers Java basic operators and program structure, detailing various types of operators including arithmetic, relational, bitwise, logical, assignment, and the ternary operator. It also outlines the structure of a Java program, including documentation, package statements, import statements, class definitions, and the main method. Additionally, it includes hands-on activities for writing Java programs that utilize these concepts.
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/ 6

0

LEARNING GUIDE

Week No.: __3__

TOPIC/S

1. Java Basic Operators


2. Java Program Structure

EXPECTED COMPETENCIES

Upon completing this Learning Module, you will be able to:


1. Understand
2. Know
3. Experience and Familiarize

CONTENT/TECHNICAL INFORMATION

I Operators

Operators are symbols or characters that have a special meaning in Java.

Types of Java Basic Operators

1. Arithmetic Operators
2. Relational Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Ternary Operator (miscellaneous operator)

1. The Arithmetic Operators


Arithmetic operators are used in mathematical expressions in the same way that they are
used in algebra.

Symbol Name Description Example


+ () Addition Adds two or more operands A+B
- () Subtraction Subtracts two or more operands A-B
* Multiplicatio Multiplies values on either side of the A*B
n operator.
/ Division Divides left-hand operand by right-hand B/A
operand.

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
1

% Modulus Returns the remainder of the division process B%A


++ Increment Increases the value of operand by 1. B++
-- Decrement Decreases the value of operand by 1. B--
2. The Relational or Comparison Operators
Relational Operators are used to compare two values or conditions and always return a
boolean value (true/false).

Symbol Name Description Example


== equal to If values are equal then condition becomes true. (A == B)
not equal to If values are not equal then condition becomes
!= (A != B)
true.
> greater than If yes then condition becomes true. (A > B)
< less than If yes then condition becomes true. (A < B)
greater than If yes then condition becomes true.
>= (A >= B)
or equal to
less than or If yes then condition becomes true.
<= (A <= B)
equal to
3. The Bitwise Operators
Bitwise operator works directly on data bits and performs bit-by-bit operation. Its
operation is similar to what is done in Boolean Algebra.
Symbol Name Description Example
& bitwise and returns bit by bit AND of input values (A & B)
| bitwise or returns bit by bit OR of input values (A | B)
^ bitwise XOR returns bit by bit XOR of input values (A ^ B)
bitwise
~ returns bit by bit AND of input values (~A )
compliment
left shift Shifts the bits of the number to the left and
<< A << 2
fills 0 on voids left as a result.
right shift Shifts the bits of the number to the right and
>> A >> 2
fills 0 on voids left as a result.
4. The Logical Operators
Logical operators are used to perform logical “AND”, “OR” and “NOT” operation, the
function similar to AND gate and OR gate in digital electronics. They are used to extend
the number of comparisons in an expression to more than 1.
Symbol Name Description Example
logical AND This operator returns true when both the
&& (A && B)
conditions under consideration are true.
logical OR This operator returns true when one of the two (A || B) is
||
conditions under consideration are true. true
logical NOT Inverts or complements result of a condition. !(A &&
!
B) is true

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
2

5. The Assignment Operators


These operators are used to assign values to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is
a value.
General Format / Syntax : variable operator value;
Symbol Name Description Example
= Simple Assigns values to a variable C=A+B
+= Add AND is equivalent to C = C + A C += A
Subtract is equivalent to C = C – A
-= C -= A
AND
Multiply is equivalent to C = C * A
*= C *= A
AND
/= Divide AND is equivalent to C = C / A C /= A
Modulus is equivalent to C = C % A
%= C %= A
AND
Modulus
<<= is same as C = C << 2 C <<= 2
AND
Right shift
>>= is same as C = C >> 2 C >>= 2
AND
&= Bitwise AND is same as C = C & 2 C &= 2
bitwise is same as C = C ^ 2
^= C ^= 2
exclusive OR
bitwise is same as C = C | 2
|= C |= 2
inclusive OR
6. Conditonal / Ternary Operator ( Miscellaneous Operator
Conditional operator is also known as the ternary operator. This operator consists of
three operands and is used to evaluate Boolean expressions.
General Format : variable = (expression) ? value if true : value if false;
Example
public class Test {

public static void main(String args[]) {


int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );

b = (a == 10) ? 20: 30;


System.out.println( "Value of b is : " + b );
}
}
Output
Value of b is : 30
Value of b is : 20

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
3

Precedence of Java Operators

Operator precedence describes the order in which operations are performed when an
expression is evaluated. Operations with a higher precedence are performed before
those with a lower precedence.

Categoty Operator Associativity


Parenthesis ()
Left To Right
Array/Subscript []
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> >>> Left to right
Relational < > <= >= instanceof Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= ^= |= <<= >>= >>>= Right to left
II Java Program Structure

Section Description

Documentation Comments are used to document a Java program. This makes the program
Section more understandable to others who view the code.
2 types of comments
 Single line Comment
// This is a single line comment
 Multi-line Comment
/* This is a Multi line comment ……………………………………

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
4

Package A package is a group of classes that are defined by a name. It is an optional part
statement of the program. The word package is a keyword that tells the compiler that
package has been created.
It is declared as:
package package_name;

Import An import statement is used to refer to the classes stored in other packages. It is
statements always written after the package statement but it has to be before any class
declaration.
Example:
import calc.add;

Interface This section is used to specify an interface in Java. It is an optional section


statement which is mainly used to implement multiple inheritance in Java.

Class Definition A Java program may contain several class definitions. Classes are the main and
essential elements of any Java program.
A class is a collection of variables and methods that operate on the fields.
Every program in Java will have at least one class with the main method.

Main Method Every Java stand-alone program requires the main method as the starting point
Class of the program. This is an essential part of a Java program. The main method
is from where the execution actually starts and follows the order specified for
the following statements.

Java Program that Prints "Hello Java"


Example:
//Name of this file will be "Hello.java"

public class Hello

{
/* Author: Name1
Date: 2020-04-28
Description:
Writes the words "Hello Java" on the screen */

public static void main(String[] args)


{
System.out.println("Hello Java");
}
}
Important Note : Java Code is Case Sensitive.

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.
5

Activity:

1. Hands On Activity . Writing programs with Java operators.

Problem:

1. Write a program that calculates simple interest.

2. Write a program that computes compound interest.

This module is a property of Technological University of the Philippines Visayas and intended
for EDUCATIONAL PURPOSES ONLY and is NOT FOR SALE NOR FOR REPRODUCTION.

You might also like