• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about byte datatype

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic