Assignment Operators Flashcards
Define Overflow in great detail !
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.
Describe in great detail the behaviour of
the java assignement operator !
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:
- Left Operand: This must be a variable that can hold the value being assigned (e.g., int, double, String, etc.).
- 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;
Define casting primitives with edge cases !
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:
- Direct Assignment: They are the same type (e.g., int to int).
- Implicit Conversion: One type can be automatically converted to another without data loss (e.g., int to double).
- 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 ?
- Numeric Types: You can explicitly cast between numeric types (e.g., double to int), but this may lead to data loss.
- 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 ?
- Numeric Promotion: Smaller numeric types can be automatically converted to larger numeric types (e.g., byte to int, int to double).
- 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
- Check Type Hierarchy: For objects, check if one type is a superclass or subclass of the other.
- 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.