Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

A question from new mock exam site about "narrowing conversion"!!!!!

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,
I was just trying one of the new mock exam sites just posted after Maha's request.This question is from the enthuware link, the JQ+ exam.
I wonder if I am just confused or if really there is something funny in their explanation for the answer they say is correct.I can't even cut and paste the question, I have to type the whole thing, here it is:
Q.The follwong class will compile and print 30 twice.
class A {
void setColor(byte i){System.out.println(""+i);}
public static void main(String args[]){
A a = new A();
byte b = 30;
a.setColor(b);
a.setColor(30); //compiler error
}
}
a. true
b. false
They say answer is false.And the explantion is:
"Narrowing conversion happens for byte b = 30.As 30 is a constant and is small enough to be held by a byte.BUT narrowing conversion DOES NOT take place for method parameters ,and hence this code will not compile."
So what is it? Am I missing a very fine point here??? Bytes have a range from -128 to 127 so why this talk of conversions.Can someone please clarify this for me!!!
Thanks
Gazala.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gazala,
As I understand the concept the only time an integer literal is allowed is in an assignment. In your example byte b = 30 is assigning the integer value 30 to the byte primitive b. Java usually doesn't allow this without casting, but the special case mentioned above allows it assuming that the integer literal fits into the primitive being assigned.
For method calling, the special case doesn't exist. Therefore we are back to an integer value of 30: a.setColor(30) being assigned to a primitive byte: void setColor( byte i ), which is not allowed by Java without casting. Therefore, the compiler will complain that this is not valid method call (i.e., no method exists with the signature: setColor( int ).
Regards,
Manfred.
 
Gazala Bohra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Manfred,
Does this mean that I can never call this method setColor with a constant number small enough to fit into bytes as they will all be integer literals ? Which means I can only assign a value to a byte variable and then use that variable to make a call to the setColor method?
Waiting for a reply,
Thanks,
Gazala
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the explanation:
You can assign integer values in the range -128 to 127 to a variable of type byte.
So the statement byte b =30; is perfect.
But when you pass "30" as an argument to the method "void setColor(byte i)", it expects a byte value, whereas the value "30" being passed is an integer. That's why a compile-time error occurs.
To avoid this compile-time error you need to cast the int value "30" to byte.
Here is the code:
class A1 {

void setColor(byte i)
{
System.out.println(""+i);
}
public static void main(String args[])
{
A1 a = new A1();
byte b = 30;
a.setColor(b);
a.setColor((byte)30);
}
}
This will print "30" twice.
- Suresh Selvaraj
[ www.jtips.net ]
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic