Java Basics Flashcards

1
Q

What is narrow-type casting/downcasting?
How can we make the final answer more accurate?

A

Narrowing casting must be done manually by placing the type in parentheses in front of the value:. So by converting a double to an int, the decimal points are truncated.

Java does not automatically round the value. If we want to round our positive value to the nearest integer, we can use (int)(value + 0.5). For negative numbers, we use (int)(value - 0.5)

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

What is widening type casting/upcasting?

A

Widening casting is done automatically when passing a smaller size type to a larger size type:

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

What are the limitations and of working with whole numbers/int?

A

There is a limited amount of data that can be stored in int - 4 bytes. We can use integer.MIN_VALUE and integer.MAX_VALUE to find the minimum and maximum value of an int.

An expression that evaluates outside of this range will result in an integer overflow - which could lead to errors or wrong value’s that’s within the range.

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