literals why does this produce an error? float x = 2.5;

38
Literals • Why does this produce an error? float x = 2.5;

Upload: ashtyn-mauger

Post on 11-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Literals Why does this produce an error? float x = 2.5;

Literals

• Why does this produce an error?

float x = 2.5;

Page 2: Literals Why does this produce an error? float x = 2.5;

Literals

• Floating-point default datatype is ‘double’.

float x = 2.5;

Page 3: Literals Why does this produce an error? float x = 2.5;

Literals

• Solution 1: cast 2.5 to float

float x = (float) 2.5;

Page 4: Literals Why does this produce an error? float x = 2.5;

Literals

• Solution 2: use ‘f’ or ‘F’ identifier

float x = 2.5f;

Page 5: Literals Why does this produce an error? float x = 2.5;

Literals

• char literals are delimited by single quotes

char c = ‘a’;

Page 6: Literals Why does this produce an error? float x = 2.5;

Literals

char c = ‘a’;int x = 3 + c;

System.out.println( x );

Page 7: Literals Why does this produce an error? float x = 2.5;

boolean

floating pointintegral

float doubleboolean char short int long

character integer

byte16+

8+/-

16+/-

32+/-

64+/-

32+/-

64+/-

numeric

Page 8: Literals Why does this produce an error? float x = 2.5;

Literals

• char is a numeric integral type (i.e. evaluates to an integer

• What is the output?

char c = ‘a’;int x = 3 + c;

System.out.println( x );

Page 9: Literals Why does this produce an error? float x = 2.5;

Literals

char c = ‘a’;char c = ‘\u0061’;

• Unicode escape sequence is backslash + u + 4 digit hex

• Unicode can be used ANYWHERE in java code

Page 10: Literals Why does this produce an error? float x = 2.5;

Literals

ch\u0061r c = ‘\u0061’;

Page 11: Literals Why does this produce an error? float x = 2.5;
Page 12: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4

• 3,2 and 4 are int literals• Integer Division!• Result of 3 / 2 is : 1• Therefore, x=3/2*4 is the same as x=1*4• x = 4 !

Page 13: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4f

Page 14: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4f

• The ‘f’ denotes ‘float’ datatype

Page 15: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4f

• The ‘f’ denotes ‘float’ datatype• The binary operator ‘/’ must have same

datatype on both sides. Therefore, 2 is promoted to float datatype

Page 16: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4f

• The ‘f’ denotes ‘float’ datatype• The binary operator ‘/’ must have same

datatype on both sides. Therefore, 2 is promoted to float datatype

• Now the binary operator ‘*’ must promote its right hand operand. 4 is promoted to float.

Page 17: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = 3 / 2 * 4f

• The right hand expression evaluates to 6.0• This will not compile because float to integer

is a narrowing conversion requiring an explicit cast

Page 18: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

int x = (int) 3 / 2 * 4f

• Explicit cast !• System.out.print (x) : 4

Page 19: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = 3 / 2 * 4f

• cast not needed• System.out.print (x) : 6.0

Page 20: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = 3 / 2 * 4f

• Another method!

Page 21: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = 3.0 / 2 * 4

• Another method!• Literals are promoted to which datatype???

Page 22: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = 3.0 / 2 * 4

• Another method!• Literals are promoted to ‘double’ datatype

Page 23: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = 3.0 / 2 * 4

• Another method!• Literals are promoted to ‘double’ datatype• This expression will not compile because it

requires an explicit narrowing conversion from double to float

Page 24: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = (float) 3.0 / 2 * 4

• Finished!

Page 25: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = (float) 3.0 / 2.0 * 4

• Does not compile.. Cast only operates on left hand operand 3.0

Page 26: Literals Why does this produce an error? float x = 2.5;

Numeric Promotion

float x = (float) (3.0 / 2.0) * 4

• Phew!

Page 27: Literals Why does this produce an error? float x = 2.5;

Finally !

• evaluates left to right• 3 / 2 is integer division, result is 1• Int 1 is promoted to double 1.0• 1.0 * 4.0 evaluates to double 4.0

double x = 3 / 2 * 4.0

Page 28: Literals Why does this produce an error? float x = 2.5;

Literals

• What is the output?

int x = 2147483647 + 1;System.out.println( x );

Page 29: Literals Why does this produce an error? float x = 2.5;

Literals

• Int datatype is 32 bits: 1 sign bit + 31 bits to represent the number

• Sign bit: 0 means positive, 1 means negative

0 1111111 11111111 11111111 11111111

Sign bit: +/-

Page 30: Literals Why does this produce an error? float x = 2.5;

Literals

0 1111111 11111111 11111111 11111111+

0 0000000 00000000 00000000 00000001=

1 0000000 00000000 00000000 00000000

Sign bit is now 1

Page 31: Literals Why does this produce an error? float x = 2.5;

Literals

0 1111111 11111111 11111111 11111111+

0 0000000 00000000 00000000 00000001=

1 0000000 00000000 00000000 00000000

2,147,483,647

1

- 2,147,483,648

Page 32: Literals Why does this produce an error? float x = 2.5;

Implicit Narrowing Conversion

0 1000000 10000001

• What is the decimal value of this bit pattern?

Page 33: Literals Why does this produce an error? float x = 2.5;

Implicit Narrowing Conversion

0 1000000 10000001

• 16513 ( 16384 + 128 + 1 )

Page 34: Literals Why does this produce an error? float x = 2.5;

Implicit Narrowing Conversion

0 1000000 10000001

short x = 16513;‘16513’ is an integer literal. Default type is ‘int’. No cast required here because integral types (including ‘char’) can perform ‘implicit narrowing conversion’ as long as the value is in range. ( float c = 2.5 is illegal, because floating point types cannot perform implicit narrowing conversion.)

Page 35: Literals Why does this produce an error? float x = 2.5;

Implicit Narrowing Conversion

• Rule 1: Both datatypes must be integer types• Rule 2: Value must be within range of target

datatype

Page 36: Literals Why does this produce an error? float x = 2.5;

loss of precision with casting

0 1000000 10000001

short x = 16513;byte b = (byte) x;

Page 37: Literals Why does this produce an error? float x = 2.5;

loss of precision with casting

short x = 16513;byte b = (byte) x;

0 1000000 10000001

bye, bye….

Page 38: Literals Why does this produce an error? float x = 2.5;

loss of precision with casting

short x = 16513;byte b = (byte) x;

0 1000000 10000001

Value of x is -127