Lesson 1
Java Operators and Expression Statements
This module discusses how operators are a fundamental element from which Java expressions and statements are built.
The certification exam recognizes the importance of operators and contains a number of questions that call upon your knowledge of them.
However, this should not be a cause for concern. You have probably already used most of the operators that Java provides. This module will review these operators and point out any important aspects of them that you should master in preparation for the exam.
Module Objectives
This module will review your knowledge of Java operators, provide numerous examples of their use, and help you to satisfy the following exam objectives:
- Determine the result of applying any operator, including assignment operators and
instanceof
to operands of any type, class, scope, accessibility, or any combination of these.
- Determine the result of applying the boolean
equals(Object)
method to objects of any combination of the classes java.lang.String
, java.lang.Boolean
, and java.lang.Object
.
- In an expression involving the operators
&
, |
, &&
, ||
,
and variables of known values, state which operands are evaluated and the value of the expression.
In addition to the above, you will cover the topics of conversion, casting, operator precedence, and order of evaluation.
Operators are a fundamental element from which Java expressions and statements are built
These little symbols are the building blocks of expressions and statements, kind of like how a bunch of Lego bricks can create a masterpiece... or a pile of plastic.
Think of operators as the verbs of Java. They're the actions that make things happen. You've got your arithmetic operators (+, -, *, /, %), which are like the basic math operations you learned in school (or maybe not, if you were too busy doodling spaceships on your notebook). Then there are the assignment operators (=, +=, -=, *=, /=, %=), which are like the bossy friends who tell you what to do with your variables. And let's not forget the comparison operators (==, !=, >, <, >=, <=), which are like the judges of Java, deciding if things are equal or not.
Now, expressions are like sentences in Java. They're made up of variables, operators, and method invocations, all strung together to create something meaningful (or not, if you're a beginner). For example, `x = 5 + 3` is an expression that adds 5 and 3, and then assigns the result to the variable `x`. It's like saying, "Hey, Java, take 5 and 3, add them together, and put the result in this box called `x`."
Statements, on the other hand, are like complete thoughts in Java. They're the things that actually do something, like declaring a variable, calling a method, or even just printing something to the console. An expression can become a statement by simply adding a semicolon (;) to the end, like this: `x = 5 + 3;`. It's like saying, "Okay, Java, I'm done thinking about this expression, now go ahead and execute it."
So, to sum it up, operators are the building blocks of expressions, which are the building blocks of statements. Without operators, Java would be like a language without verbs, and that's just a recipe for confusion and chaos.
Building expressions using operands and operators
An expression consists of operands and operators. Operands are normally variable names or literals while operators act on operands. The following are examples of expressions:
int numberWheels = 4;
System.out.println("Hello");
numberWheels = numberWheels + 1;
There are several ways of classifying operators:
- Arithmetic
- Assignment
- Relational
- Logical complement
- Logical
- Conditional
- Bitwise
Expressions can be thought of as the building blocks of a program and are used to express the logic of the program