File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final keyword 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 "final keyword" Watch "final keyword" New topic
Author

final keyword

nagendra satyakrishna jasti
Greenhorn

Joined: Jan 25, 2006
Posts: 18
please see this code

class Test{
public static void main(String args[]){
final int x=5;
byte y=x;
}
}
according to java this should not complie beacuse their is no explicit cast
but this one got complied
please offer some explanation


ekalavya of bert bates and kathy sierra
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Hi,
program what you have will compile as you said. The reason is that as we know byte can take only 8 bits(1 byte) and int 32 bits(4 bytes). So only first 8 bits will be copied from int to byte. Its an implicit cast. For implicit casting, explicit casting can be given but its redundant.

Ex.



x: 0000 0000 0000 0000 0000 0000 1000 0001
y: 1000 0001

So, the value of byte variable b will be. -1(check this value once you run the program). Hope you understood it.


Raghusham Sankargal
nagendra satyakrishna jasti
Greenhorn

Joined: Jan 25, 2006
Posts: 18
you cannot assign an int to byte without explicit cast please check your code
steven gerrard
Ranch Hand

Joined: Jan 21, 2006
Posts: 55
hi nagendra,
i dont knw the answer but i noticed one thing. if u dont make "x" final then the code wont compile

so i guess it s something to do with "final" . u may refer casting at sun java forum
nagendra satyakrishna jasti
Greenhorn

Joined: Jan 25, 2006
Posts: 18
Originally posted by mohit juneja:
[QB]hi nagendra,
i dont knw the answer but i noticed one thing. if u dont make "x" final then the code wont compile

yeah that is true it wont compile if you take that final modifier what my question is why that mandatory explicit cast required for assinging larger types to narrower types dost not apply here?
Thivya Manoharan
Greenhorn

Joined: Oct 04, 2005
Posts: 2
Hi,
since x is declared as final the value can not be changed ,as the value is with in the range of a byte. Assigning it to a byte will not cause an error.
Suppose if x is out of range say for eg(300) then you will get a compile-time error. You have to typecast it.

Eg:
final int x= 300;
byte y = x; //Compile time error
byte y =(byte)x
vikash dwivedi
Greenhorn

Joined: Jan 10, 2006
Posts: 6
hi
according to raghu sham (1st reply) the answer whold be -1. but it gives 5.(and remain same till 127.)
as the first bit will use for sign bit and remaining for values. so it should give -1. but its giving 5.
why.....

_______________________

vinay
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Hi,

Yes I made a mistake there. You do need an explicit cast. Sorry
vikash dwivedi
Greenhorn

Joined: Jan 10, 2006
Posts: 6
hi

no i have problem on that why it gives 5. as the first bit used for sign bit and remaining bis are values(stored in 2's form). if it takes 1000 0001 then "000 0001" are value bits.... is it?

and i have still confusion if we not declere "int x=5" as final its not compile why...

please help me

thanks

vinay
Praveen Ponna
Greenhorn

Joined: Nov 26, 2005
Posts: 11
I guess there are 2 things that seems to be confusing in this post:

1: Raghu's example: If you check back his first reply, it refers to



and not the original value 'x=5' .



2. Although actual answer given by Raghu is correct. When you declare a variable as final and assign it to another variable, then its as good as assigning the literal value itself.

Example:



this is same as:



But if you remove final from the declaration:



then the compilation fails, as 'x' can take up any value (>127)


Hope this clarifies everything!!
[ February 02, 2006: Message edited by: PraveenPonna ]

scjp1.4
 
Don't get me started about those stupid light bulbs.
 
subject: final keyword