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
Once an array is created, can its size be changed?
No it cannot
What is the output?
~~~
int dayOfWeek = 5;
switch(dayOfWeek) {
case 0:
System.out.println(“Sunday”);
default:
System.out.println(“Weekday”);
case 6:
System.out.println(“Saturday”);
break;
}
~~~
Weekday
Saturday
Always pay attention to break
What a break statement?
The break statement can also be used to jump out of a loop.
What is a label?
A label is an optional pointer to the head of a statement that allows the application flow to jump to it or break from it. It is a single word
that is proceeded by a colon (:).
int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}};
OUTER_LOOP: for(int[] mySimpleArray : myComplexArray) {
INNER_LOOP: for(int i=0; i<mySimpleArray.length; i++) {
System.out.print(mySimpleArray[i]+”\t”);
}
System.out.println();
}
not a good practise so dont learn it.
Logical operators
&, | and ^
What is the result for the following?
x=true
y=true
x&y = ?
x=true
y=false
x&y = ?
x=false & y= false?
true
false
false
What is a continue statement?
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
INCLUSIVE OR
x=true | y= true?
x=true | y= false?
x=false | y= false?
true
true
false
EXCLUSIVE OR
x=true ^ y= true?
x=true ^ y= false?
x=false ^ y= false?
false
true
false
byte range?
-128 to 127
long i = Long.parseLong(“2l”);
float f = Float.parseFloat(“2.0f”);
double d = Double.parseDouble(“2D”);
no. gives number format exception
yes.
Yes
Can you do the following?
long var1 = 0_x_4_13;
no can only use _ with digits.
which one takes less memory?
float or double
float
which is more precise?
double or float?
double
Will the following compile?
float a = 2.3F;
double d = 2.4D;
double dd= 2.3d;
yes
Can you assign a number value to char?
char A= 65;
System.out.println(A);
yes
prints “A”
Can you assign a number value to char?
char A=- 65;
System.out.println(A);
wont compile
Can you assign a number value to char?
char A= (char)-65;
System.out.println(A);
compiles and returns ?
Can you cast an int to a boolean?
no
int apple = 3;
apple+=3;
System.out.println(apple);
6
int apple = 3;
apple=+3;
System.out.println(apple);
3
char A=20;
System.out.println(A+A);
40
will it compile?
final byte a = 10;
final byte b= 10;
short c = a+b;
yes
Name three ways you can create object of the wrapper class
Assignment (Autobox)
constructor
static methods
Write the autobox way of assigning boolean
Boolean luck = true;
Write Boolean in constructor
Boolean a = new Boolean(“True”);
Boolean a = new Boolean(true);
Boolean a = new Boolean(“TRUE”);
Make Boolean using static method
Boolean a = Boolean.valueOf(“true”);
Make the wrapper class for the following
Character
Integer
Double
Short
Byte
Double
Long
Byte a = 10; Byte b = new Byte("10"); Byte c = new Byte((byte)10); Byte d = Byte.valueOf((byte) 100);
Make for short
Short a = 10; Short c = new Short("10"); Short b = new Short((short) 10); Short dd = Short.valueOf("10"); Short cc = Short.valueOf((short)10);
Can you do the following?
Character cccc = new Character(“aa”);
How about
Character cccc = new Character(‘a’);
HOw about?
Character ass = Character.valueOf(“b”);
no. you are making a string.
yes
No string and characters do not mix
How to parse String to Integer?
Integer.parseInt(“23”); //int primitive
Integer.valueOf(“23”) //Integer obj
What does the following print?
Long var1= Long.valueOf(-12); Long var2 = Long.valueOf("-12"); System.out.println(var1==var2); Long var3 = Long.valueOf(23); Long var4 = Long.valueOf(23); System.out.println(var4==var3); Long var5 = Long.valueOf(128); Long var6 = Long.valueOf(128); System.out.println(var5==var6);
true
true
false
-128 to 127
Double var1= Double.valueOf(12); Double var2 = Double.valueOf(12); System.out.println(var1==var2); Double var3 = Double.valueOf(23); Double var4 = Double.valueOf(23); System.out.println(var4==var3); Double var5 = Double.valueOf(128); Double var6 = Double.valueOf(128); System.out.println(var5==var6);
false
false
false
Float var1= Float.valueOf(12f); Float var2 = Float.valueOf(12f); System.out.println(var1==var2); Float var3 = Float.valueOf(23); Float var4 = Float.valueOf(23); System.out.println(var4==var3); Float var5 = Float.valueOf(128); Float var6 = Float.valueOf(128); System.out.println(var5==var6);
false
false
false
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
i1==i2
Integer i3 = Integer.valueOf(10);
Integer i4 = Integer.valueOf(10);
i3==i4
Integer i5 =10
Integer i6 = 10
i5==i6
i1.equals(i2)
i2.equals(i3)
i3.equals(i4)
What if i1 > 128
false
true
true
———————–
true
true
true
true
equals = all true
false
Integer obj1 =100;
Short obj2 = 100;
obj1.equals(obj2);
obj1 == obj2
false
DNC
Can you do this?
char b = -10;
You cannot assign negative value to primitive data type without casting
Can you do the following?
public void myMethod(int weight) {
int weight = 10;
}
No DNC because same name
List the other of operators. Yes all of htem
post unary x++, x–
pre unary ++x, –x
unary +, - , !
Multiplication, division, modulus [+ - ]
addition/substraction +, -
shift operators»_space;, «,»_space;>
relational operators <, >, >=, <=, instance of
equal to, not equal to == !=
logical operators &, ^, |
short circuit &&, ||
ternary
assignment operator =, +=, «=
Post present UM ADD SOME REAL Elegant Long short TeA
Does switch statement allows to use continue statement?
no
Does if allow break statement?
no
Does if statement allow continue statement
no
Can you do the following?
int i = Integer.parseInt(“45_98”);
no because only string is expected.
double inclination2 = 1.20173e5;
valid
Character is signed or unsigned?
unsigned Integer
What is casting?
forceful conversion of one datatype to another datatype.
What does all numeric wrapper class extend?
java.lang.Number
What does Boolean and Character extend?
Object
What does all wrapper class implement interface?
java.io.Serializable
java.lang.Comparable
Can you do the following?
Character a = Character.valueOf(“c”);
System.out.println(c);
No. You can do for all wrapper classes except Character.
Which two wrapper class doesn’t cache object?
decimals
Float and Double
What is the range of values for cache objects?
-128 to 127.
What is the range of values for caching object for Character?
0 to 127
Can you do the following?
Long a1 =10?
No you either have to cast it or do
Long a1 = 10L;
Long a1 = (long)10;
Which of the following will compile?
Long a1 = 10L;
Long a2 = new Long(10L);
Long a3 = Long.valueOf(“10”);
Long a4 = Long.valueOf((long) 100L);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
Long a1 = 10L; //yes
Long a2 = new Long(10L); //yes
Long a3 = Long.valueOf(“10”); //Yes
Long a4 = Long.valueOf((long) 100L); //Yes
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
Will the following compile?
Long a3 = Long.valueOf(“10L”);
No. Number format exception
Will the following compile?
Double a1 = 10D;
Double a2 = new Double(10D);
Double a3 = Double.valueOf(“10d”);
Double a4 = Double.valueOf((double) 100D);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
Yes to all
Can you do Double a = 10;
Float a1 = 10.0;
No it needs to be
Double a = 10D;
Long l = 10L;
Float a1 = 10.0f;
Can you do the following?
Float a3 = Float.valueOf(“10.0”);
Float a4= Float.valueOf((float)10.0f);
Yes
WAP given
int y=4;
ternary operators to find out what the value of y is.
System.out.println((y==1)? “1”:(y==2)?”2”:(y==3)?”3”:(y==4)?”4”:”nothing”);