Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final keyword

 
nagendra satyakrishna jasti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nagendra satyakrishna jasti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot assign an int to byte without explicit cast please check your code
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes I made a mistake there. You do need an explicit cast. Sorry
 
vikash dwivedi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic