aspose file tools*
The moose likes Mock Exam Errata and the fly likes about byte datatype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "about byte datatype" Watch "about byte datatype" New topic
Author

about byte datatype

sumanth
Greenhorn

Joined: Mar 16, 2001
Posts: 2

public class a{
public static void main(String s[]]
{
byte b=1;
while (++b<0)
;
System.out.println("LADY");
}
}
IN ABOVE CODE OUT PUT IS LADY.
BUT HERE MY CONFUSION IS b is byte primitive type inthe while loop increment b value (++b) but here b++ means b=b+1;
b is byte so b=b+1 is not allowed.
so its work properly why?
any one help me
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Sumanth,
Your <code>while()</code> is not doing anything Change the code to:

And it will work as you excpect; nothing will be printed.
Hope that helps.
Please read the Javaranch Name policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited March 18, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
vkswamy venkatachalam
Greenhorn

Joined: Feb 18, 2001
Posts: 21
Hi,Sumanth,
++b or b++ is not b=b+1 first of all.It means b=(type of b)(b+1);
byte b =b+1 gives U compile error.In ++b the type of varial is implicitly changed to byte.
byte b=b+1 will not compile because byte is promoted to int first and then addition takes place.Now the left side of '=' is byte which cannot accomodate int in byte type.
As in Ur Q b=2 in the while loop as the value is incremented and asigned to b.The loop test condition fails.
I hope Ur doubt is cleared now.
regrds
vkswami

vkswami
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
I think we need not to go that far to discuss this. Just see rules below and it will make clear.
RHE says :
For unary operators, two rules apply, depending on the type of the single oprand:
1. If the oprand is a byte, a short or a char, it is converted to int( unless the operator is ++ or --, in which case no conversion happens).
2. else there is no conversion.
So ++b is covered under "unless part of rule 1" of unary operator conversion. If we say b=b+1 this means we are talking about binary oprators and not about unary oprators and that is why we are getting confused.
I donot think this while is not doing anything. In that case how output will be "LADY" actually this b is being incremented everytime upto 127 ( max positive value a byte can take) and in that case ++b < 0 is not true so it does print anything. But as soon as it crosses 127 it becomes negative and that is why contion turns true and "LADY" is the output.
Pl let me know if I am wrong.

------------------
Veer
Sarang Gurao
Greenhorn

Joined: Mar 14, 2001
Posts: 13
Hello Friend
The unary operator implictly do conversion from byte to int.
so
byte b=1;
b=++b; will not give error
b=b+1; will always give u error because + operator
cannot do conversion.
in ur problem the loop will continue till the value of b becomes 127 and then value of b will -128 which becomes less than 0 hence it comes out of the loop and prints lady.
Branka Kranjac
Greenhorn

Joined: Mar 28, 2001
Posts: 7
Hi, I think main problem is here:
while (++B<0)
; <--- that's why code below is executing - since System.out.println("LADY"); is not in {}, it will be printed anyway...
Try this:
while (++B<0)
{
System.out.println("LADY");
}

------------------
Branka


Branka
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Branka,
The original code had an error, as you noted. Veer and Sarang are correct as to why it does eventually print "LADY".
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about byte datatype
 
Similar Threads
casting problem
array
Doubt in assignment
Can you explain this with code...
why I cann't assign FINAL INT to Byte