aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About conversions and access modifiers 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 "About conversions and access modifiers" Watch "About conversions and access modifiers" New topic
Author

About conversions and access modifiers

Sanjeev Verma
Ranch Hand

Joined: Aug 24, 2000
Posts: 87
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

Joined: Sep 23, 2000
Posts: 243
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

Joined: Aug 30, 2000
Posts: 3141
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.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10

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

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About conversions and access modifiers
 
Similar Threads
Confusion
Difference between protected and blank method
Ques on Access Specifiers
my notes on JLS for any1 who needs them !!
Sign extension doubt?