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 Local variables: type mismatch (int -> byte) if not initialized 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 "Local variables: type mismatch (int -> byte) if not initialized" Watch "Local variables: type mismatch (int -> byte) if not initialized" New topic
Author

Local variables: type mismatch (int -> byte) if not initialized

Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi,

trying out Inquisition, I came across the following problem:



Fact is that in line 4 there is a compile error "cannot convert from int to byte".
Same thing when I omit the final keyword in line 2.

Instead when I initialize the local variable i in line 2 (final int i = 127), it compiles fine.

Ok, I could learn this fact by heart, but as you know, there are tons of stuff to be learned by heart, so I strongly prefer a deep understanding of such "critical" behaviour.

Can anybody explain me WHY the later initialization doesn't work in this case?
What's going on behind the scenes? Why is the compiler so "stupid"? ;-)
Thanks in advance!


SCJP 6 (88%), SCWCD (89%)
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

in java
implicitly everything lower than integer automatically gets converted into int and we cannot assign byte value to the int value
hence we have to do the explicit typecasting
we have to cast the int into byte as follows

This should work now
happy coding

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi Prasad,

thank you for your reply - but sorry, this was not exactly the answer I'm looking for:
I already know that with such casting, I can "force" the compiler to trust me.

My question was related to the initialization stuff ... besides an int value of 127 (or generally spoken: from -128 until + 127) fits well into a byte ... same thing with a smart cowboy fitting into a pair of jeans ... a fat person - instead - should need a corset ... or simply a cast in order to wear them ;-)

So why do I need to initialize my int in the same line where I declare it?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/403360/Beginning-Java/java/final

specially look at the campbell answare
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Seetharaman Venkatasamy wrote:http://www.coderanch.com/t/403360/Beginning-Java/java/final

specially look at the campbell answare


Thanks a lot, Seetharaman!

I see, the main issue is the difference between compile-time and run-time constants.
Ok, I'll go more into the details, specially concerning the resulting byte-codes.

Sorry that I missed to perform a search before posting this problem!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ulrich Vormbrock wrote:Thanks a lot, Seetharaman!

You are welcome
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Ok, as proposed, I've just performed a generation of the following byte codes concerning compile-time and run-time constants:



... and now the run-time stuff:



If I'm right, the variable i2b (line 15, seems to be an alias for i, doesn't it?) is generated in the second byte code - so indeed there's no matter of initialization. Correct?
I've pasted thes two examples in order to become familiar with different behaviours behind the scenes.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

or to better understand use a Java *decompiler*
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40


I wrote a quick writeup about compile time constants a while ago. Maybe it can help here...

http://www.coderanch.com/t/454384/Beginning-Java/java/compile-time-constant

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Thanks for the hint using a decompiler.
I've just tried out the cavaj decomplier in order to see how compile-time and run-time constants are treated differently.

Here are the results - first comes the compile-time stuff:


and then the run-time stuff:



Of cause I've been obliged to perform an explicit cast to byte (line 13) in the latter case ... otherwise my code should not have compiled (in order to decompile it again)

What I get from the latter case is the fact that "int i" is now a run-time constant (see line 12, last code).
In the first code, "int i" doesn't appear, instead "byte byte0" gets the value 127 (see line 12, first code).

Another nice thing to see (although it has nothing to do with this issue) is the fact that the compiler creates a default no-arg constructor!

It's a very good idea to study some pieces of code compiling and decompiling it again!
Maybe it's beyond the exam, but - as already mentioned - I want to become familiar with different behaviours of Java without having to learn tons of rules by heart.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Hi
Take the modified code:

I think when final var is initialised on same line where it is declared JVM considered's it as compile time constant ,
else it is considerde as the declared primitive type.

Do not make thinks more complicated by decompiling and compiling it's easy.

Thanks & Regards
Sumit Kothalikar
Suresh Gokarakonda
Greenhorn

Joined: Jan 01, 2009
Posts: 13
@Ulrich Vormbrock

Did you try inline initialization of the final int with value greater than 127?




Regards,
Suresh
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
@Suresh Gokarakonda

Yes, I did ... as expected, the compiler jumps up and down, screaming "possible loss of precision" ... that's rather clear to me, because a number exceeding +27 is no longer considered as a byte.
Suresh Gokarakonda
Greenhorn

Joined: Jan 01, 2009
Posts: 13
@Ulrich Vormbrock
Considering a scenario where the compiler allows what you were expecting, what might be the output of the below piece of code



1 int i = 1278;
2 byte b = i;
3 S.o.p(b);


In case the value of int i is less than 128 and works for your piece of code in the question, how will you enforce that the value is not changed by any other module.
If that gets changed the output of some part of your code would be unpredictable which might lead to a test scenario with random result.
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Thank you, Suresh!

I see one crucial point concerning compile-time constants: to prevent them of being modified later by a routine or module.
Now it's perfectly clear to me - same thing with case-statements (switch-case) where only compile-time constants are allowed, too.
I revise my statement concerning compiler and "stupid behaviour" ;-)
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Guys, I know this topic is kinda dead for 4 months, but let me ask, why this:



works fine, as the a is a compile-time constant, while this:



shows "Type mismatch: cannot convert from long to int" error in line 02. Isn't it the same situation as in the int/byte example - a compile-time constant that fits the int? Am I missing some obvious fact here?


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
 
jQuery in Action, 2nd edition
 
subject: Local variables: type mismatch (int -> byte) if not initialized
 
Similar Threads
Doubt in assignment
Final variable
I cant believe my eyes?
Assignments
why I cann't assign FINAL INT to Byte