CH2 Operators Flashcards

1
Q

The arrow (->) operator

A

Arrow function or lambda operator - binary operator that represents a relationship between two operands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Numeric Promotion Rules

A

📍If two values have different data types, Java will automatically promote one of the values to the larger of two data types.

📍If one of the values is integral and the others is floating-point, Java will automatically promote the integral value to the floating-point value’s data type.

📍Smaller data types, namely, byte, short and char, are first promoted to int any time they’re used with a Java binary arithmetic operator with a variable (as opposed to value), even if neither of the operands is int.

📍 After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

float type value assigninig

A

Why is the f suffix required?
Default Type: Floating-point literals like 3.14 are considered double by default in Java.

Precision Loss: A double has higher precision (~15-16 decimal digits) compared to a float (~6-7 decimal digits). Assigning a double to a float without explicit casting or suffix can lead to precision loss, so Java requires you to explicitly indicate that the value is a float.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Casting

A

Casting is unary operation where one data type is explicitly interpreted as another data type.

Casting is optional and unnecessary when converting to a larger or widening data type, but it is required when converting to a smaller or narrowing data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How casting is performed?

A

By placing the data type, enclosed in parentheses, to the left of the value you want to cast.

int fur = (int) 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Comparing null to null

A

In some languages, comparing null with any other value is always false, although this is not the case in Java.

System.out.print(null == null); // true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

null and the instanceof operator

A

Calling instanceof on the null literal or a null reference always returns false.

System.out.print(null instanceof Object); // false

But
System.out.print(null instanceof null);
-> Does not compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Logical Operators

A

logical operators evaluate both sides
of the expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Key Notes on Java Operator Precedence

A
  1. Parentheses () have the highest precedence and should be used to make expressions clear.
  2. Unary operators (++, –, !, ~) are evaluated before arithmetic operations.
  3. Multiplication (*), division (/), and modulus (%) have higher precedence than addition (+) and subtraction (-).
  4. Relational operators (<, >, <=, >=, instanceof) come before equality (==, !=).

5.Logical AND (&&) has higher precedence than logical OR (||).

  1. Assignments (=) and compound assignments (+=, -=, etc.) are evaluated right-to-left.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Logical operators Precedence Order

A
  1. ! (Logical NOT)
  2. && (Logical AND)
  3. || (Logical OR)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Casting primitives

A

It is required any time you are going from larger numerical data type to a smaller data type, or converting from a floating-point number to an integral value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

long literal

A

Always use L (uppercase) instead of l because the lowercase l can be easily confused with the digit 1.

If a number fits within int, L is optional because Java automatically converts int to long when assigning it to a long variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

short circuit expression

A

& and | do not short circuit the expression but && and || do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given:

public class TestClass {
public static void main(String[] args) throws Exception {
if(args.length > 3 ^! (args.length == 2)){
System.out.println(args.length);
}
}
}

What will be the output if the above code is run without any argument?

A

Don’t be alarmed at the usage of ^!. It is not a new operator. It is just a combination of two operators ^ and !. ^ performs a logical XOR operation if both of its operands are boolean and ! is a logical not operator.
Now, if the program is run without any argument, args.length is 0. Therefore, args.length > 3 is false and args.length == 2 is also false. Thus, the expression reduces to:

false ^! false i.e.
false ^ true i.e.
true

Hence, 0 will be printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

goat -= 1.0;
is it legal?

A

It does not produce a compilation error since the compound operator applies casting automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the output?

short zebra = (byte) weight * (byte) height;

A

Even though both height and weight are cast to byte, the multiplication operator automatically promotes them to int, resulting in an attempt to store an int in a short variable.

17
Q

== and != with objects

A

Both operators can be used with objects.

18
Q

Does this code compile?

int ticketsSold = 3;
ticketsSold += (long)1;

A

Yes as the compound operator automatically casts the right‐hand operand.

19
Q

Integer Caching Range

A

Integer caching is a mechanism used in Java (and some other languages) to optimize memory and performance when dealing with frequently used integer values.

20
Q

How Integer Caching works

A

How It Works
Java automatically caches integer objects within a certain range, typically from -128 to 127.
This means that when you assign or compare integers within this range using Integer.valueOf(int) or autoboxing (Integer x = 100;), Java reuses the same object instead of creating a new one.

21
Q

Compound assignments

A

✅ Compound assignments automatically cast, but normal assignments require explicit casting.

byte b = 10;
b = b + 200; // ❌ Compilation Error! (Requires explicit cast)

byte b = 10;
b += 200; // Allowed: (b = (byte) (b + 200))
System.out.println(b); // → -46 (overflow!)

22
Q

long to double

A

Java automatically converts the primitive long to a primitive double.