Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conversion prb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conversion prb" Watch "conversion prb" New topic
Author

conversion prb

Akshatha Nayak
Ranch Hand

Joined: Jul 15, 2004
Posts: 53
hi guys,
Given these declarations, which of the following expressions are valid?

byte b = 1;
char c = 1;
short s = 1;
int i = 1;

Select the three correct answers.

a. s = b * 2;

b. i = b << s;

c. b <<= s;

d. c = c + b;

e. s += i;

i got this questtion from Khalid book.. i have some doubt in this question ... answers for this question are b,c,e.
i agree with b...for obvious reasons that int is larger than short in size.
for c & e my doubt is if this
byte a = 10, b = 20, c = 0;
c = a+b; gives a compiler error ..saying that there will be loss of precision .. & the solution is we have to cast it to byte.
how come we dont need a cast here for s += i which is s = s + i & b <<= s which is b = b << s ;

can anybody PLS help me with this ..


Only those who will risk going too far, Can possibly find out how far one can go! <br />happiness is journey and not destination<br /> <br />A Nayak<br />----------------------------------<br />SCJP 1.4<br />SCWCD 1.3<br />SCBCD 1.3
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char.


This was in Dan's Mock exam 1 Q22

If both operands of a binary arithmetic expression are of type byte, char or short; then both are implicitly widened to type int, and the result of the expression is of type int.


This is from Dan's Mock exam 2 Q11

With these two rules...All the 5 answer choices should be correct. However they are not. Could someone please explain !!

Thanks


---<br />SCJP 1.4
Steve Bradley
Greenhorn

Joined: Jun 09, 2004
Posts: 15
byte b = 1;
char c = 1;
short s = 1;
int i = 1;

Select the three correct answers.

a. s = b * 2;

b. i = b << s;

c. b <<= s;

d. c = c + b;

e. s += i;

This is a great question, I have part of the answer and I raise an additional question...

Sierra / Bates state: "all operands in a bit shift are promoted to at least an int."

My question is, Does this mean both sides? the term "all operands" certainly indicates that and it provides an explanation for answers b and c.

As for question e. the compound operator excuses you from an explicit cast. An equivalent statement of s = (short)(s + i) would require an explicit cast. Hope this helps.
Kitty Dayal
Ranch Hand

Joined: Jul 22, 2004
Posts: 89
Sorry a small change in my previous statement
All operands of comparison operator are promoted to atleast int before operation and the result would obviously be int and so b is correct.


Its actually All operands of SHIFT operator are promoted to atleast int before operation and the result would obviously be int and so b is correct.

Hope that helps,
Kits
[ August 13, 2004: Message edited by: Kitty Dayal ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Akshatha, please pay attention to our JavaRanch Naming Policy and change your displayed name to conform.

Thanks,
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Stefan Guilhen
Ranch Hand

Joined: Jul 31, 2004
Posts: 61
Just to clarify some things:

a. s = b * 2;
b. i = b << s;
c. b <<= s;
d. c = c + b;
e. s += i;

a & d - As Murtuza correctly posted, operands of numeric expressions are promoted to at least int. So a and d are invalid, as we cannot assign an int value to a short or char variables without an explicit cast.
b - This answer is valid because the result of the shift expression is an int, the same type of variable i.
c & e - These answers are valid because all the compound assignment operators do an implicit cast. So, b <<= s is the same as b = (byte)(b << s) and s += i is the same as s = (short) (s + i).

Hope this helps.
Stefan
[ August 16, 2004: Message edited by: Stefan Guilhen ]

SCJP 1.4, SCBCD 1.3<br />IBM 141, 484, 486
Akshatha Nayak
Ranch Hand

Joined: Jul 15, 2004
Posts: 53
Thankx Stefan ..
 
 
subject: conversion prb
 
Similar Threads
well a doubt !
Dan's Q doubt again?
Type conversion.
doubt in method argument declared final
doubt