Help coderanch get a
new server
by contributing to the fundraiser
  • 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

uranary + operator

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = + 7 ; // compiles fine
BUT
byte b1 = 7 ;
byte b2 = + b2 ; // needs an explicit cast
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you make the variable b2 a object variable then you get a "illegal forward reference' compile error as you are trying to use b2 before it has been declared.
If you make it a automatic variable (ie method variable) then it gives a compile time error (as you stated) "possible loss of precision" error.
Adding in the cast to (byte) then gives you a "variable might not have been initialized" error.
So adding in the cast does not work either. I think the bottom line is that you cannot use a variable before it is declared and initialized.
Did you mean to say??
byte b1 = 7 ;
byte b2 = + b1 ; // needs an explicit cast
 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following up on your question, assuming you meant:
byte b1 = 7 ;
byte b2 = + b1 ; // needs an explicit cast
then I thought I understand the problem. Any arithmatic operation on a byte, short or char results in the result being implictly cast to int. So the cast would be needed but if I do the following:
byte b1 = 7 ;
byte b2 = ++b1 ;
System.out.println(b2)
it works and prints 8. I do not understand why the cast was not need in this case.
Any ideas, anyone?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
byte b = + 7 ; // compiles fine
BUT
byte b1 = 7 ;
byte b2 = + b1 ; // needs an explicit cast[/B]

Well, thatz right u need to cast it back to byte coz the unary operator( for that matter almost all operators) converts the operand to atleast an int. so if u want a byte cast it.
Thanks
Sanjeet

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daryl,
regarding your question, ++ and -- are exceptions to that rule
of promoting to at least int before operation! No promotion there.
Chengx
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic