ICT

module 2, lesson 3 - module 3, lesson 4 :

Keywords

  • Keywords are reserved words used for specific purposes/uses. Programmers are not allowed to use them as identifiers.

  • All keywords are lowercase. If any letter of the keyword is capitalized, it becomes an identifier

Identifiers : Identifiers are user-defined names for methods, variables, constants, and classes.

  • It start with a letter, an underscore ( _ ), or a dollar sign ( $ ) followed by alphanumeric characters, and underscores.

  • Create identifiers that are descriptive of their purpose.

  • Your identifier must contain no spaces

Data Types

Java has two sets of data types: primitive and reference (non-primitive).

Below is  a table of data types:

  1. string : “hello”

  2. boolean : “true”, “false”

  3. char : “A”

  4. byte : -128 to 127

  5. short : -32,768 to 32,767

  6. int : -2,147,483,648 to 2,147,483,647

  7. long : -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

  8. float : 6 to 7 decimal digits

  9. double : 15 to 16 decimal digits

byte, short, int, and long hold integer values.

float and double are floating point literals – numbers with possible decimal parts.

String is a reference data type. It is a class that allows the storage of a sequence of characters.

Variables : Variables are identifiers whose values can be changed.

Syntax =

<data_type> <identifier>;

Constants : Constants are identifiers whose values never change once declared.

Syntax =

final <type> <identifier> = <literal>;

Casting is the process of assigning a value or variable of a specific type to a variable of another type,

Operators are symbols that perform logical or mathematical functions on operands such as variables, constants, and objects.

Unary operators require only one operand. These changes the value of the operand without using the assignment operator (=).

  • negation (-)

  • bitwise complement (~)

  • increment (++)

  • decrement (--)

Binary operators require two operands. Examples of these are (+) for addition or concatenation, (*) for multiplication and (/) for division.

Arithmetic Operators :

  1. ++ = increment

  2. -- = decrement

  3. / = division

  4. % = modulo

  5. * = multiplication

  6. - = subtraction

  7. + = addition

Shortcut/Assignment Operators

  1. += (assignment with addition)

  2. -= (assignment with subtraction)

  3. /= (assignment with division)

  4. *= (assignment with multiplication)

  5. %= (assignment with modulo)

Comparison/Relational Operators

All relational operators are binary operators primarily because they indicate the relationship among two operands.

The result of a relational operation is a Boolean value: either true or false, depending on whether the expression to be evaluated is true or false.

  1. > : greater than

  2. >= : greater than or equal to

  3. < : less than

  4. <= : less than or equal to

  5. == : equal to

  6. != : not equal to

Conditional/Logical Operators

Conditional/Logical Operators will also return a Boolean value, and evaluated like the comparison operators.

Truth Tables

Truth tables help demonstrate the result of a logical operation.

  • The NOT (!) operator is a unary operator that negates the value of the operand.

  • The OR (||) operator is a binary operator that returns true if at least one of its operands is true.

  • The bitwise exclusive OR or XOR (^) operator is a binary operator that returns true when both operands have different values. Otherwise, it will return false.

  • The AND (&&) operator is a binary operator that returns true only when both operands are true. Otherwise, it will return false.

Precedence refers to the order which operators are executed. It dictates which operations are executed first if given a complicated expression. It also follows the MDAS (Multiplication-Division-Addition-Subtraction) rule.

The Table shows which operator must be performed first.

  • Perform first the innermost parenthesis.

  • Operators with the same precedence are performed in order according to their associativity.

  • Unary Operators and NOT operator are performed from right to left.

  • The rest of the operators are performed from left to right.

image.png