• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Compile Time Constant

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

I want to know if we declare char as final then how the
implicit cast from char to byte happens.

static byte m(){
final char c='a'; //try without final, it's compilation error
return c;
}
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it is a 'final', compiler knows that its value can never be changed.
Now it sees if this char value can be fit into byte or not.
If it can fit, then compiler has no problems and hence compiles fine.

Try this code:


Murali...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one important point to remember is that it only works if the final variable is declared and initialized in one statement. The following will not compile.
 
Himanshu Saxena
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

But still the question remains unanswered why is it happening ?
If you are saying that because we declare it final so it automatically
narrow the return type.And implicity it cast from char(16 bit ) to byte (8 bit). Then why it does not happen all times.

Eg it is not the case with other data types.
public int m2(){
final long a=10;
return a; //Error,Implicitly not cast long to int WHY ?
}
But for int & it's subtype byte,char it does
public byte m1(){
final int b=10;
return b; //No Error,Implicitly cast int to byte WHY implicit conversion
}

Tell Me logically.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because for line:

final int b=10;

compiler is able to deduce value 10 at compile time and return this way value 10 as byte without loosing precision.

if it is written this way:

final int b;
if ( UserPressedKey() ) // some user function.
b = 10;
else
b = 99999; // this cannot be cast to byte without loosing precision

so compiler is unable to decide value for b at compile time.
 
Martin Jedrzejewski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but,...

the fact that:

static byte m() {
final long m = 12;
return m; /// <- Error
}

static byte m2() {
final int m = 12;
return m; /// <- OK!
}


is a bit confusing, is there a rule that allow for casting from int to byte implicitly in such case?
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!in the above question,in the first case it is shown that

char is cast to byte
later...........
int can be cast to byte.......but not long....is it bcoz of the range of
byte...


or there is some other reason?please if anyone could explain it will be kinda of you.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
is it bcoz of the range of [/b]byte...



But... byte is smaller and narrower than char. it takes 8 bits and char 16. char is between Unicode 0 and Unicode 2^(16)-1. Byte between -128 nad +127.

Does anybody understand last question?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Piotr Milewski wrote

But... byte is smaller and narrower than char.


Therefore the compiler checks, if the char provided fits into a byte. The method would not compile if the line was final char c=500;


By the way, byte is both: bigger and smaller than char.
byte b = -12;
char c = b; no compile, explicit cast required

and
char c = 500;
byte b = c; no compile, the other way round.

Making the variable of the second lines of these examples final wouldn't help, as they both won't fit in.



Yours,
Bu.
 
Piotr Milewski
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I understand it, but i mean such situation:

static byte m() {
final long m = 12;
return m; /// <- Error
}

static byte m2() {
final int m = 12;
return m; /// <- OK!
}

Why such difference? What's the rule to distinguish first case from the second one? When the error occurs and when it's OK?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Piotr,

the language specification says in �5.2 (Assignment Conversion):
In addition, if the expression is a constant expression of type byte, short, char or int : ...
and then the rules follow.

So these things work only for primitive types int and lower.


Yours,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic