ch2 Flashcards
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
What is a short-circuit operator?
The && and || operators “short-circuit”, meaning they don’t evaluate the right-hand side if it isn’t necessary.
What are logical operators?
The & and | operators, when used as logical operators, always evaluate both sides.
When is equality operator used?
Comparing two numeric primitive types.
Comparing two boolean values
Comparing two objects including null and String values
What is the output?
~~~
File x = new File(“myFile.txt”);
File y = new File(“myFile.txt”);
File z = x;
System.out.println(x == y);
System.out.println(x == z);
~~~
File x = new File(“myFile.txt”);
File y = new File(“myFile.txt”);
File z = x;
System.out.println(x == y); // Outputs false
System.out.println(x == z); // Outputs true
true bc same reference
false bc diff reference
What’s the output?
int y = 10;
int x = (y > 5) ? (2 * y) : (3 * y);
20
Code practice:
Using ternary operator
if y > 10 print y is bigger. else y is smaller
System.out.println((y>=10)? (“y is greater”): (“y is smaller”));
What is the output?
~~~
System.out.println((y > 5) ? 21 : “Zebra”);
int animal = (y < 91) ? 9 : “Horse”;
~~~
does not compile bc you cant assign string to int
What are the data types supported by switch statement?
int and Integer
byte and Byte
char and Character
short and Short
String
enum
What data types are not supported by switch statement?
long and boolean
A case statument value must be?
a literal, enum constant or final constant variable of the same datatype.
When casting primitives values is required ?
Casting primitives is required any time you are going from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.
int x = (int)1.0;
short y = (short)1921222;
int z = (int)9l;
What will be printed:
long x = 5;
long y = (x=3);
System.out.println(x);
System.out.println(y);
Console output:
3
3
What is the result of:
short x = 10;
short y = 3;
short z = x * y;
Does not compile
(promoted to int when applying any arithmetic operator, with the resulting value being of type int)
What is the difference between &&,|| and &,| operators ?
The short-circuit operators (&&, ||) are nearly identical to the logical operators (&, |) except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.
What is an array?
An array can hold multiple values of the same data type simultaneously