This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About conversions and access modifiers

 
Sanjeev Verma
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends
i was playing with some conversion code yesterday when i tried to compile this:
class BytetoChar
{
public static void main(String argv[])
{
int i=255;
byte a=(byte)i;
System.out.println(a);
char z=a;
System.out.println(z);
}
}
on compiling, i received an error which read:
BytetoChar.java:8: possible loss of precision
found : byte
required: char
char z=a;
^
1 error
A few questions:
why does it report an error when byte to char is a widening conversion ?
is it because of the signed nature of bytes and unsigned nature of chars ?
anyway, then i recomipled the code below:
class BytetoChar
{
public static void main(String argv[])
{
int i=255;
byte a=(byte)i;
System.out.println(a);
char z=(char)a;
int x=z;
System.out.println(x);
}
}
the sample output is:
-1
65535
But i was expecting an output of
-1
255
because when I convert i to a (refer above variables), the result is a narrowing conversion, hence the lower 8 bits are assigned to a, bit to bit. hence first output is ok. but next, i am widening a to z, i expect the 8 bits to assigned to the lower 8 bits of z and hence a value of 255.
Another code: (Question #34 in Brogden Exam Perp)
class ZZZ
{
XXXXX void setWidth(int n){}
}
class YYY extends ZZZ
{
YYY()
{
int a=20;
setWidth(a);
}
}
in the above code, what should be the access modifier(s) for the setWidth() method in class ZZZ (in place of XXXXX) so that YYY is allowed access to this method but other unrelated classes in other packages are not allowed to call the method.
a. private
b. protected
c. default (no modifier)
d. friend
the answer given is b and c. but I think the answer can only be b as the wording of the question clearly mentions that the method should not be accessible to UNRELATED CLASSES IN OTHER PACKAGES.
will someone explain
thanx in advance
sanjeev
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rules for automatic conversion of numeric types as / RHE

byte
|
V
short
|
V
int < --char
|
V
long
So byte to char or short to char is not converted automatically
in direct assignment. char can only convert to int or higher.
Perhaps JLS has a better answer, but I dont know where to look
I think you are right about the second question.
So when are you planning to give the cert. have you fixed the time. How was the party yesterday.I missed the party but found this wonderful site yesterday only.


[This message has been edited by mohit joshi (edited September 24, 2000).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjeev,
You're getting the 'loss of precision' error in the first example as <code>byte</code> is signed and <code>char</code> is not. The conversion would lose the sign-bit.

Widening primitive conversions do not lose information about the overall magnitude of a numeric value. (JLS &5.1.2)

I think the following is happening in the second example:
You start with an <code>int</code> 32-bit signed

Then you convert to a <code>byte</code> 8-bit signed

Then you convert to a <code>char</code> 16-bit unsigned BUT widening conversion says you won't lose the magnitude of a numeric value, hence, even though <code>char</code> is unsigned the extra bits are filled with the sign value of the <code>byte</code> producing

Converting back to an <code>int</code> the extra bits are zero filled as the compiler considers <code>char</code> to be unsigned (no sign bit to lose); and you end up with

Hope that helps.
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Another code: (Question #34 in Brogden Exam Perp)
class ZZZ
{
XXXXX void setWidth(int n){}
}
class YYY extends ZZZ
{
YYY()
{
int a=20;
setWidth(a);
}
}
in the above code, what should be the access modifier(s) for the setWidth() method in class ZZZ (in place of XXXXX) so that YYY is allowed access to this method but other unrelated classes in other packages are not allowed to call the method.
a. private
b. protected
c. default (no modifier)
d. friend
the answer given is b and c. but I think the answer can only be b as the wording of the question clearly mentions that the method should not be accessible to UNRELATED CLASSES IN OTHER PACKAGES.

But, Sanjeev, default (no modifier) access is package access. In other words, only classes in the SAME package have access. This excludes UNRELATED CLASSES IN OTHER PACKAGES.
Marilyn
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic