Operators in Java Flashcards
What is an expression built using?
Constants, variables, operators and method calls
What are operators?
Symbols which are used in expressions to perform arithmetic or logical operations
What is operand?
The variable or literal on which the operator is applied
Name the three forms of operators
- Unary
- Binary
- Ternary
What is a unary operator?
An operator that has just one operand.
Give examples of unary operators
+,-, ++, –, !
What is a binary operator?
An operator that has two operands in known as binary operator
Give examples of binary operators
+, %, >, &&, etc
What is a ternary operator?
An operator that has three operands is known as a ternary operator
Give example of a ternary operator
?:
What are arithmetic operators?
+, -, *, /, %
What is the result when two integers are divided?
The result of the division will also be an integer (the decimal part will be truncated
What are the two forms of the plus and minus operators?
- Unary form - adding/subtracting stuff
2. Preceding an operand
Name the process of joining two strings together
Concatenation
What are relational operators?
They determine the relationship between the two operands by comparing them
What is the outcome of relational operators?
The outcome of these operations is a boolean value which is either true or false
How many relational operators does Java provide?
6 types
Name the six types of relational operators
- Greater than (>)
- Less than (=)
- Less than or equal to (<=)
- Not equal to (!=)
What are logical operators?
They operate only on boolean operands and are used to construct simple decision making expressions
Name some logical operators
- Logical AND - &&
- Logical OR - I I
- Logical NOT - !
What is the logical && operator?
It is a binary operator and is used to combine two conditions or expressions. The resultant value is a boolean value. The result is true if both values are two, and false if either is false
What is the logical | | operator?
It is a binary operator and is used to combine two conditions or expressions. The resultant value is also a boolean value. The result is true if both or either is true, false if both are wrong
What is the logical NOT operator?
It is a unary operator and is written before a single operand. It negates the result of the expression following it
What are assignment operators?
It is used to assign the value of an expression to a variable
What type of operator is the assignment operator?
Binary operator
Give the difference between = and ==
= is an assignment operator, whereas == is a comparison operator
What are shorthand assignment operators?
Several variations of the assignment operator that are offered in Java
Give two advantages of using shorthand operators
- They save a little bit of typing time
- The use of shorthand operators result in an efficient code as they are implemented most efficiently by Java run-time environment as compared to their long forms
State the main shorthand operators
- +=
- -=
- *=
- /=
- %=
What are increment and decrement operators?
The operators ++ and –are increment and decrement operators, respectively
What type of operators are increment and decrement operators?
Unary
What is the rule in postfix increment?
First use, then increment
Explain the process of postfix increment
- Initially, the value of a variable y is 10, and the value of z us not defined
- You first use y, which is 10, hence z is assigned to the value 15
- You increment the value of y by 1. Thus, the final value of y = 16
What is the rule in the Prefix increment?
First increment, then use
What is the conditional operator?
It is also called a ternary operator because it has three operands
What is the syntax of a conditional operator?
boolean-expression ? expression 1 : expression 2
What is precedence of operators?
Each operator has a precedence associated with it. Precedence is the priority of an operator according to which it is evaluated. This precedence is used to determine the order of evaluation of an expression involving more than one operator.
What is associativity?
If two operators have the same precedence, then they are either evaluated from “Left to Right” or from “Right to Left”. This is termed as associativity, which tells the direction of execution of operators
Name the two steps involved in creating the object of the class datatype
- Declaring a variable of the class type
2. Allocating memory for the object where the details of the object can be stored
What is the new operator used for?
To allocate memory for the object
Give the syntax for creating an object of a class
Classname objectname = new Classname();
What does it mean when a class is being instantiated?
An instance of the class is being created
What is the purpose of the dot operator?
Once the objects have been declared and the memory has been allocated, member variables and member methods can be accessed using the dot operator
Give the syntax of the dot operator
Objname.memberVariable;
Objname.memberMethod();
What do you use to determine the order of evaluation of the operators when several operators appear in an expression?
Precedence and associativity
What does an arithmetic expressoin represent?
A numeric value
When does a type conversion take place?
When an arithmetic expression consists of constants and variables of different data types (mixed expression)
What is type conversion?
It is a process that converts a value of one data type to another data type
What are implicit type conversions?
The type conversions which are automatically performed by the Java compiler
Give other names for implicit type conversions
Automatic type conversion, type promotion, widening conversion or coercion
What are explicit type conversions?
In a case where a data type smaller than another has to hold a value of the bigger data type, a programmer has to force the conversion
What needs to be done to create a conversion between two incompatible types?
A cast must be used, which is simply an explicit type conversion
What is the drawback of using a cast?
It can lead to loss of precision. For example, when a double value is assigned to an int type, the fractional component is lost