Assignment Operators Flashcards

1
Q

Define Overflow in great detail !

A

An overflow occurs when a numeric literal exceeds the maximal value allowed by the primitive data type of the variable that contains that value which causes that numeric literal to wrap around that maximal value to start all over from the minimal value that the data type allows

IMPORTANT :

It’s important to note that overflow typically happens during calculations, not when assigning a numeric literal directly. For example, adding two numbers can cause overflow, while assigning a numeric literal that exceeds the type’s range (like int x = 2147483648;) will result in a compilation error rather than an overflow.

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

Describe in great detail the behaviour of
the java assignement operator !

A

The = operator in Java is the assignment operator that assigns the value of the expression on its right to the variable on its left and returns that assigned value. This allows for chaining assignments and using the assigned value in subsequent expressions.

The = assignment operator in Java requires that the data types of the operands be compatible. Specifically:

  1. Left Operand: This must be a variable that can hold the value being assigned (e.g., int, double, String, etc.).
  2. Right Operand: This can be a literal, variable, or expression of a compatible type. If the right operand is of a different type, it may require explicit casting to match the left operand’s type.

For example :

Assigning an int to an int is valid: int x = 5;

Assigning a double to an int requires casting: int y = (int) 3.14;

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

Define casting primitives with edge cases !

A

If the types are incompatible and cannot be implicitly converted, a compilation error will occur.

WHAT IS TYPE COMPATIBILITY ?

So type compatibility is a strict requirement from the ‘=’ operator in order to compile successfully

Type compatibility in Java refers to the ability of one data type to be assigned to another without causing a compilation error. Two types are compatible if:

  1. Direct Assignment: They are the same type (e.g., int to int).
  2. Implicit Conversion: One type can be automatically converted to another without data loss (e.g., int to double).
  3. Casting: A value of one type can be explicitly converted to another compatible type (e.g., casting a double to int).

HOW TO KNOW IF A TYPE CAN BE EXPLICITY CASTED TO ANOTHER TYPE ?

  1. Numeric Types: You can explicitly cast between numeric types (e.g., double to int), but this may lead to data loss.
  2. Object Types: You can cast between object types if they are related by inheritance (e.g., casting a subclass to a superclass).

HOW TO KNOW IF A TYPE IS AUTOMATICALLY CONVERTED TO ANOTHER TYPE ?

  1. Numeric Promotion: Smaller numeric types can be automatically converted to larger numeric types (e.g., byte to int, int to double).
  2. Reference Types: A subclass reference can be assigned to a superclass reference without explicit casting.

No Data Loss: Automatic conversions occur when there’s no risk of losing data (e.g., int to double).

RULES OF THUMB

  1. Check Type Hierarchy: For objects, check if one type is a superclass or subclass of the other.
  2. Numeric Type Sizes: Review the sizes of numeric types to see if a widening or narrowing conversion applies.

By following these guidelines, you can determine the compatibility of types for casting in Java.

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