Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in numeric conversion

 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the program little bit to assign a final var in the constructor.
This time it gives compile error. Plese let me know why.

Thanks


class NumConvTest {
final int j;

NumConvTest() {
j = 2;
}

public static void main(String[] args) {
NumConvTest nc = new NumConvTest();

int i = -20;
//final int j = 2;
final int k = i;

byte b1 = nc.j;
byte b2 = (byte)i;
byte b3 = (byte)k;
}
}
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if initialize the final variable directly like

final int j = 5;

still you will get compile time error because you are assigning
an int to byte without explictly cast.

byte b1 = nc.j;
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused.


Why b2 requires an explicit typecasting while b1 has no problem ?

Thanks,
Lalitha.
 
SomeswaraRao Vudattula
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chitra,

when u r declaring the final var and using.. thete.. no probes...i meen no need the casting..

but when u r using the other class final var int,we need the casting.. because.. until runtime that dont know the type of variable.. so it need casting...

i hope now u r clear...
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by SomeswaraRao Vudattula:
Hi Chitra,

when u r declaring the final var and using.. thete.. no probes...i meen no need the casting..

but when u r using the other class final var int,we need the casting.. because.. until runtime that dont know the type of variable.. so it need casting...

i hope now u r clear...



didn't clear

pls give last conclusion ..

using final int j=9; // using directly this will give error need casting
using int m=0; // same above

using final static int n=8; // don't need casting
casting need if final is intialized in constructor ?

can you pls give the final conclusion

when we need casting and when not..


and what if on constructor is intilizing final primitive and on does not
will the complier error is not create if use that constructor which will intialize final variable and will compiler error will generate when using the construcot which does not intitilize the final variable value..

?

pls give answer to alll questions
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still waiting for response.. :roll:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your :roll: is not appropriate here, Amit. You ask a lot of questions. And many times when someone takes the time to respond and compose a good reply, you reply that you didn't understand, could someone please explain more clearly. If you do understand, you usually immediately follow up with another question. As I write this, I've just read about ten posts in a row from you where you are impatient for responses in different threads. It's hard to keep up with all these questions, and personally I'm not really inclined to try. I think you might get better results here if you didn't ask so many questions at once - instead, try harder to find some of these answers yourself, and save your questions only for the things you can't find out otherwise. Remember, every time you ask a question here, you're asking for a favor from someone, for them to take their time to try to explain something for you. Now many of us are here because we enjoy doing that sort of thing - but we also like to see students putting some real effort into their studies, taking responsibility for researching things themselves rather than always asking questions of others. There are many good books out there, and writing code yourself is always the best way to learn. I hope that helps...
[ June 05, 2005: Message edited by: Jim Yingst ]
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim
 
John Wolf
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said! Jim. Thanks!
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys...

I have a side question about the above code.

class some_class {
final int x; // gives compiler error saying not initialzed.

but when I assign a value in a constructor:

some_class { x=123; } // no compiler error.

But if I assign a value in a normal method and not in the constructor, gives me the same error saying not initialized.

1) Doesn't "final" varibles get default values?
2) Are they only assignable in constructors?

Thanks a lot guys.. Really need your help..
Regards..
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maduranga,

Quoted from JLS2 8.3.1.2 final Fields:
-------------------------------------------------
A blank final instance variable must be definitely assigned (�16.8) at the end of every constructor (�8.8) of the class in which it is declared; otherwise a compile-time error occurs.
-------------------------------------------------

A final instance variable must be assigned with a value, either during the declaration or in the constructor like the following:



And once a final variable is assigned with a value, any attempt to reassign it will result in compile-time error.


[ June 06, 2005: Message edited by: Joyce Lee ]
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys..

its a request again to pls put light on the post and give the final confulsion...
i would be really gr8full to you ppl

thanx in advance..

regard,,
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the way to go Amit.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/qb]<hr></blockquote>

i compiled the above code and showing the thing u wrote in comments
i.e // remember the compiler makes no assumptions on actual type

what does that means
[ June 10, 2005: Message edited by: amit taneja ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic