• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To Decide Compile Time constant?

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In above program at line->6 50 is compile time
constant this is why method accepts it
even though method return byte.

I know that 50 is final and is in ranger of byte
this is why it is compile time constant.

By at line->10 100 is not compile time constant why.
how will i decide that 100 is not compile time constant
but 50 is.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gowher amin naik:

In above program at line->6 50 is compile time
constant this is why method accepts it
even though method return byte.

I know that 50 is final and is in ranger of byte
this is why it is compile time constant.

By at line->10 100 is not compile time constant why.
how will i decide that 100 is not compile time constant
but 50 is.


Because in your class there is no where any method exist named as "method(int)" which is accpeting an int parameter if you really want to run the program then you have to explicitly typecast to 100.

and call method as follows

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
100 is a compile-time constant.

That is not the issue here.

The issue is the difference between method invocation conversion and assignment conversion.

Assignment conversion includes the narrowing primitive conversions.

Method invocation conversion doesn't include the narrowing primitive conversions.
[ August 01, 2006: Message edited by: Keith Lynn ]
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ok Ankur check code 2
method return x which is int but compile time constant
hence it works.
variable a is also int but compile time constant but it does not work
there is compile time error at Line->10
why
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic