aspose file tools*
The moose likes Certification Results and the fly likes doubt in one question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Certification Results
Bookmark "doubt in one question" Watch "doubt in one question" New topic
Author

doubt in one question

radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18

Hi Every one,
This is my first post in this site..
I am preparing for SCJP..planning to write within 10 days.
I saw this program in javaprepare.com site in language fundamentals questions..
I ran this program and it is not compiling..giving error as y may not be inetialised..
my thinking is..as in 2nd if stmt y is getting 2 It has to compile and run and display 2 for y..
I want help..please clarify my doubt..
public class Compare {
public static void main(String args[]) {
int x = 10, y;
if(x < 10) <br /> y = 1;<br /> if(x>= 10) y = 2;
System.out.println("y is " + y);
}
}
Thanks in advance..
Regards
radhika
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
since you initalizing Y inside an if()statement it may not be initalized as if the if()is false then Y will never be initalized
this post should be in anothe rthread not here
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18

Hi Fred Abbot,
Originally posted by Fred Abbot:
since you initalizing Y inside an if()statement it may not be initalized as if the if()is false then Y will never be initalized
this post should be in anothe rthread not here

you mean if we assign values inside if loop those values are not visible outside that loop..
am i correct in this..
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi Fred,
I ran the program by assigning value for y outside if loop and it is working..
Thanks alot for your help..
Regards
Radhika
ashwini srinivasan
Greenhorn

Joined: Nov 07, 2000
Posts: 26
radhika,
y is a local variable. Local variables must be initialized before u use it....bcaz local variables will not get automatically initialized to default. So u need to initialize it and then can change its value in if() statement.Hope this helps u.
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
hi ashwini
although what you are saying is correct that is not the issue at hand here as she is initalizing it before using it the problem however is that it is being initalized inside an if() statement so there will betimes that it is not initalized hence the error is y MAY NOT have been initalized
in your case the error would be
y WAS NOT initalized
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
Originally posted by radhika madini:

Hi Fred Abbot,
you mean if we assign values inside if loop those values are not visible outside that loop..
am i correct in this..

what you are saying is true but againg is not your origanal question
if you would initalize y to 0 before the loop then inside a loop change it to 5 it will be 5 outside the loop as well
however it cannot be initalized for the first time inside the loop since it may never get inside the loop
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi Fred,
I tested what u said by assigning y=0 inetially.then it is printing the value assigned inside if{} ie 2.I got your concept..
one more thing is..
is it same for do and while loops also?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I am moving this thread to Programmer Certification Study forum.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in one question