This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How To Decide Compile Time constant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How To Decide Compile Time constant?" Watch "How To Decide Compile Time constant?" New topic
Author

How To Decide Compile Time constant?

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

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

Joined: Dec 27, 2005
Posts: 1249

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



The Best way to predict your future is to create it - Every great individual common man
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 07, 2005
Posts: 643

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

Joined: Feb 02, 2006
Posts: 2023
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How To Decide Compile Time constant?
 
Similar Threads
Dan's Q doubt again?
Help in Final variables
Integer literals implicit casting
Inner class-final static variable is possible
Compile time constant