• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in one question

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am moving this thread to Programmer Certification Study forum.
Ajith
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic