ch2 Flashcards
(99 cards)
What is an operator?
A special symbol that can be applied to a set of variables, values or literals and that returns a result
What are the three types of operators in Java
3 types of operator are :
1 Unary
2 Binary
3 Ternary
What are arithmetic operators?
*
/
%
Includes unary operators ++ and –
List the numeric promotion rules.
If two values have different data types, Java will automatically promote of the values to the larger of the two data types.
If one of the values is the int and the other is the float, Java will promote the int to float
Smaller data types namely byte, short, char are first promoted to int anytime thery are used with a java binary arithmetic operator.
After all promotion has occured and the operands have the same data type, the resulting value will have the same data type as its promoted operands
What is the data type of x / y?
short x = 10;
short y = 3;
int
What is the data type of x + y?
double x = 39.21;
float y = 2.1;
compile time error
bc
float y should be 2.1f;
What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;
double
Will this compile?
float y=13;
Yes
What is the data type of x * y?
int x = 1;
long y = 33;
long
Will this compile?
double a = 2.0f;
yes
What is a unary Operator?
An operator that requires exactly one operand, variable to function.
x -> operand
Will this compile?
float a=1.0?
no
you gotta put :
float a =1.0f;
What is the output?
boolean x = false; System.out.println(x); x = !x; System.out.println(x);
x=false;
x=true;
Remember that you cannot do this
boolean hello = true;
+hello;
boolean y = -true
int x =!5;
You cannot apply a negation operator to a Boolean expression nor can you apply a logical complement to an operator
What is the output
int x = !5;
boolean y = -true;
boolean z = !0;
does not compile
What is the output?
int counter = 0; System.out.println(counter); System.out.println(++counter); System.out.println(counter); System.out.println(counter--); System.out.println(counter);
System.out.println(counter); // Outputs 0 System.out.println(++counter); // Outputs 1 System.out.println(counter); // Outputs 1 System.out.println(counter--); // Outputs 1 System.out.println(counter); // Outputs 0
What is an assignment operator?
An assignment operator is a binary operator that modifies or assigns the variable on the left hand side of the operator with the result on the right hand side of the equation.
What is the output?
int x = 1.0;
short y = 1921222;
int z = 9f;
long t = 192301398193810323;
int x = 1.0; // DOES NOT COMPILE
short y = 1921222; // DOES NOT COMPILE
int z = 9f; // DOES NOT COMPILE
long t = 192301398193810323; // DOES NOT COMPILE
What’s the output?
short x = 10;
short y = 3;
short z = x * y;
z is int. Does not compile
you can make it work by casting it to short
short x =8; short y=9; short sum = (short)(x*y); System.out.println(sum);
What do you mean by casting primitive values?
int x = (int) 1.0;
there is a cost, you can have overflow and underflow.
What are compound assignment operators?
x +=2;
What problem does it solve?
Say you have the following -
int x =1;
long y =3;
y *=x;
instead of doing this
y = (int)( x * y);
What’s the output of the following?
long x= 5;
long y = (x=3);
What are the values of x and y?
3
What are relational operators?
They compare two expressions and return a boolean value.
<, >, <=. >=, instanceof