aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes who can explain me this please ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "who can explain me this please ??" Watch "who can explain me this please ??" New topic
Author

who can explain me this please ??

Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
I was trying by mysel the following :


int i =10;
private byte k = i ;

this won't compile, but when declare i final, it compiles fine

final int i =10;
private byte k = i ;

This will compile

Could you explain me why ??


SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

A final int is a compile time constant. What that means is that you cannot change the value of that int at runtime. So the compiler now knows what the value of int is and whether it can fit into the byte. So it compiles.

However if the int were not final its value may not be in the range of a byte. So the compiler cannot know for sure if the assignment is legal. Try these different code snippets


int i = 10;
byte b = i;

final int i = 10;
byte b = i;

final int i = 256;
byte b = i;


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
A final int is a compile time constant
Note that John is using a different definition of a compile time constant than K&B (see chap. 5 under "Legal expressions for switch and case", page 324): There a compile time constant is final and the value is assigned in the declaration.
So it compiles
This is true only for compile time constants in the sense of K&B. For example

final int i;
i = 10;
byte b = i;

won't compile, but

final int i = 10;
byte b = i;

will.


SCJP 5 (98%) - SCBCD 5 (98%)
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Thanks my friends, I got it.

Regards
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
Hi John,

I think compile-time constant is a prerequisite for "case" legal argument, and nothing to do with this case.



the value of i is still in the range of byte, and yes, it won't be compiled successfully, because you need an explicit cast on it.



It should be fine.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
public class Test {

final int i =10;
private byte k = i ;

}


This is very simple.. compiler doing optimizations on code. it`s final so it`s value not got to be change. onces you compile this code then latter when you decompile this code. you will get code like following.

public class Test
{

public Test()
{
k = 10;
}

final int i = 10;
private byte k;
}



now there is no relation between int and byte like k=i; like. now it`s totally independent and not affect each other. and 10 is in range of byte so again no need to cast it.

every compiler doing compile time optimization on code.

for future detail go Java performance tunning.


[ July 21, 2008: Message edited by: kaushik vira ]

[ July 21, 2008: Message edited by: kaushik vira ]
[ July 21, 2008: Message edited by: kaushik vira ]

kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: who can explain me this please ??
 
Similar Threads
byte.......
about casting.
casting
casting
I cant believe my eyes?