aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "question 1" Watch "question 1" New topic
Author

question 1

Hina Mustafa
Greenhorn

Joined: Dec 01, 2000
Posts: 8
Hi All,
I have a problem.According to the rules an assignment to the local variable must occur on every possible execution path to the access.But why I'm getting an error here.
class assign{
void status(int a){
String s;
if ( a>=0 ) { s="A Positive Number"; System.out.println(s);}
if ( a<0 ) { s="A Negative Number";System.out.println(s);}<br /> }<br /> and why does it compile??<br /> class assign{<br /> void status(int a){<br /> String s;<br /> if ( a>=0 ) { s="A Positive Number"; System.out.println(s);}
else { s="A Negative Number";System.out.println(s);}
}
ThanX
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
Although you know the first version will definately assign something to s, the compiler doesnt.


SCJP
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
class assign
{
void status(int a)
{
String s;
if( a>=0 )
{
s="A Positive Number";
System.out.println(s);
}
if( a<0 )
{
s="A Negative Number";
System.out.println(s);
}
}

public static void main(String args[])
{
assign ob=new assign();
ob.status(-5);
}
}
Well, the code above matches your first code. (It is just a bit formatted for clear readability, and I have added a main() method to execute it.)
It did compile correctly at my system and even ran perfectly. The second code also compiled correctly and ran. Choosing a number less than 0 prints "A Negative Number" on the shell prompt.
Where is the problem, (if, I missed it!!!).
Bye,
Tualha Khan


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

Hina,
It does compile. Perhaps you should try adding another } at the end ?
Rgds
Sahir


....
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
I seem to remember reading about this and it wasnt supposed to compile. well it gives me something to look into tomorrow.
Hina Mustafa
Greenhorn

Joined: Dec 01, 2000
Posts: 8
Hi,
Sorry I missed final with String s;
I mean 3rd line is
final String s;
Now there's an error cannot assign a second value to blank final variable
I'm also confused with another variation
class assin{
void method(int a){
String s;
if ( a>=0 ) { s="A Positive Number";}//1
if(a<0) { s="A Negative Number";}//2
System.out.println(s);
}
}
with this code either line 1 or 2 will excecute so there should not be error according to the rules..

[This message has been edited by Hina Mustafa (edited December 11, 2000).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
Indeed Tualha's code does compile and run printing "A Negative Number".
the JLS says in chapter 16 that it should not.

Except for the special treatment of the conditional boolean
operators &&, | |, and ? : and of boolean-valued constant
expressions, the values of expressions are not taken into
account in the flow analysis.
As another example, a Java compiler will accept the code:

void flow(boolean flag) {
int k;
if (flag)
k = 3;
else
k = 4;
System.out.println(k);
}
as far as definite assignment of k is concerned, because the
rules outlined in this section allow it to tell that k is
assigned no matter whether the flag is true or false. But
the rules do not accept the variation:
void flow(boolean flag) {
int k;
if (flag)
k = 3;
if (!flag)
k = 4;
System.out.println(k);// k is not "definitely assigned" before here
}
and so compiling this program must cause a compile-time error to occur.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi All,
Think I figured it out
If you move the <code>System.out.println(s)</code> outside the <code>if</code> blocks you get the <code>s might not have been initialized</code> error.

Since, in the original code, 's' is only referenced after an assignment has been made the compiler knows it will always been initialized.
Isn't Java fun !
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
[This message has been edited by Jane Griscti (edited December 11, 2000).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4095
    
    1
thanks so much Jane. this one had me going. it seems so obvious now. i didnt really think the JLS would contain an error of that magnitude. i certainly would have gotten it wrong if it had been on a test.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question 1
 
Similar Threads
Mughal Q3.14
Switch
Math.min() and Math.max()
HELP! Why not Compiling ? Easy but.......
Different between signs(Positive and Negative ) and values