• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Type conversion.

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question.

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


Select 3 correct answers.

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

Ans- b,c,e


I know for implicit narrowing conversion the source should be constant of int,char,byte or short and destination char byte short
and the values should be in range.
So in this case why is b,d type incompatible?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'a' and 'd' fail because the expressions are non-constant ints.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that before the addition and shift are performed, the types are promoted to int so the result is an int.

Also on the last option, do you mean s += i because s =+ i is the same as s = i which is an error.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by non constant ints?

I know that all of them are converted to int before any operation is carried out and then assigned after implicit narrowing.
For eg in b) there is not need of casting as the destination is already an int, but why not in the case of 'a' and 'd'?
Why isn't it implicilty narrowed to short?

BTW..'e'was a typo..

It's e) s += i
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the expression b * 2, the byte b is widened to type int, and the result of the operation is also an int. This is not a constant because 'b' is a variable. So the int result cannot be implicitly narrowed. It can only be assigned to a short variable with an explicit cast.

The same reasoning applies to c + b.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nabila, please quote your sources.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in that case wouldnt c,e have to do the same thing.

c) b <<= c
that is b = b << c
it would mean b and c (both being variables) are being widened to int and later need to be narrowed to type Byte.

and same in the case of

e)s += i
where s is being widened into int and the result should be narroweed to short.

This Question is from the book by khalid mughal,rolf rasmussen.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nabila Mohammad:
So in that case wouldnt c,e have to do the same thing...


Compound assignment operators are different in that the "result of the binary operation is converted to the type of the left-hand variable..." (Ref: JLS 15.26.2.)

So s =+ i is equivalent to s = (short)(s + i).
 
reply
    Bookmark Topic Watch Topic
  • New Topic