• 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

why no error with byte b=3+5

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sir i would like to know one thing as see the following code


1 byte a=5;
2 byte b=6;
3 byte c=a+b;

in the above code. It is flaging an error mesage that "possible loss of precission" well no problem becaus we know that result of any expression is int that's why this error , but here one question like see

byte b=23;

Here 23 will automaticaly get casted but why in the line c=a+b compiler is not casting automaticaly why?

one thing more sir if i am coding like this as see

byte b=3+5; Here it is not genrating any error message why although 3+5 is also an expression.

Regard

Arun kumar maalik
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun Maalik:
Dear sir i would like to know one thing as see the following code


1 byte a=5;
2 byte b=6;
3 byte c=a+b;

in the above code. It is flaging an error mesage that "possible loss of precission" well no problem becaus we know that result of any expression is int that's why this error , but here one question like see

byte b=23;

Here 23 will automaticaly get casted but why in the line c=a+b compiler is not casting automaticaly why?

one thing more sir if i am coding like this as see

byte b=3+5; Here it is not genrating any error message why although 3+5 is also an expression.

Regard

Arun kumar maalik



The difference is that 3+5 can be computed at compile-time because they are compile-time constants.

However, in the first code, a and b are not compile-time constants, and at compile-time, the type of a+b is int and can't be assigned to a byte without a cast.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there's an EXCELLENT article called "The SCJP Tip Line Implicit Conversions, Explicitly" by Corey McGlone about this topic that helped me a lot.
[ November 17, 2006: Message edited by: catherine powell ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add on to this the following code will compile because a and b are compile time constants
final int a = 1;
final int b = 2;
int c = a + b;

However, this code will not compile because a nd b are runtime constants(not compile time constants)

final int a;
final int b;
a = 1;
b = 2;
int c = a + b;
reply
    Bookmark Topic Watch Topic
  • New Topic