Checked and unchecked operators
CHECKED AND UNCHECKED OPERATORS
Normally we have seen operators and we know what are operators and its types Recalling the types
- Assignment operators
- Relational operators
- increment decrement operators
- Bitwise operators
- Conditional operators
- Logical operators
Before that we must refresh some old concepts..
we know what is type casting and type conversion,just we have gone through Java we know what is narrow and wide conversion narrow conversion is from highest data type to lowest data type, and wide conversion is from lowest data type to highest data type.
That is we can say that widening conversion is implicit that is automatic conversion and opposite is the narrowing conversion that is we have to do manually the conversion
example if we have int a,b,c;
b=400;
double x;
Than we have automatic conversion from integer to double that x=b;
But we have manually for a=(int)x; // this syntax is according to java in cpp we do the round bracket in front of the variable not in the front of data type.
Well some times the cases arise that the original data is not preserved by the conversion methods so there is the data loss naturally and the data becomes useless.
so now comes the checked and unchecked operators in picture .The
checked
and unchecked
operators are used to control the overflow checking context for integral-type arithmetic operations and conversions.- checked-expression:
- checked ( expression )
- unchecked-expression:
unchecked ( expression )
The
checked
operator evaluates the contained expression in a checked context, and the unchecked
operator evaluates the contained expression in an unchecked context. A checked-expression or unchecked-expression corresponds exactly to a parenthesized-expression.
The overflow checking context can also be controlled through the
checked
and unchecked
statements.class Test { const int x = 1000000; const int y = 1000000; static int F() { return checked(x * y); // Compile error, overflow } static int G() { return unchecked(x * y); // Returns -727379968 } static int H() { return x * y; // Compile error, overflow } }
In short Checked means it checks whether the data is changed or not if changed than throws overflow Exception at compile time so that we can know that original Data is not lost in unchecked it returns the data with modificztion that might not be the result we want or the actual answer we want.
Comments
Post a Comment