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 Doubt in numeric conversion 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 "Doubt in numeric conversion" Watch "Doubt in numeric conversion" New topic
Author

Doubt in numeric conversion

Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
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

Joined: Nov 15, 2004
Posts: 1026
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;


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Lalitha Gottumukkula
Ranch Hand

Joined: May 24, 2005
Posts: 45
I am confused.


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

Thanks,
Lalitha.
SomeswaraRao Vudattula
Greenhorn

Joined: Mar 28, 2005
Posts: 12
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

Joined: Mar 14, 2003
Posts: 806
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


Thanks and Regards, Amit Taneja
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
still waiting for response.. :roll:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Thanks Jim
John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
Well said! Jim. Thanks!


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
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

Joined: Jul 11, 2003
Posts: 1392
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

Joined: Mar 14, 2003
Posts: 806
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

Joined: Dec 29, 2004
Posts: 1071
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
That's the way to go Amit.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 806
[/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 ]
 
wood burning stoves
 
subject: Doubt in numeric conversion
 
Similar Threads
casting doubt
Doubt in Numeric conversion(Khalid Mughal)
A question
Narrowing Primitive Conversions
strange casting and conversions