• 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 Everone,
This is my first post.
I have one doubt in the given prgm.I ran this prgm but it is not compiling.error is y may not be inetialised..
my thinking is.. in 2nd if stmt y is assigned 2 so it should compile and run displaying 2
please see the following prgm and try to clear my doubt..I am planning to write scjp2 within 10 days..your suggestions will be appreciated..
public class Compare {
public static void main(String args[]) {
int x = 10, y;
if(x < 10)
y = 1;
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
i jusr replied to you in a diffrent thread
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As for as local variables, it must be initialised while declaring
them.
Assigning a value is different from initialising.
Saran
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java will not allow you to reference a variable unless it has been "definitely assigned". "Definitely Assigned" has a very strict definition, basically boiling down to "This variable must have been assigned no matter what path of execution has been followed".
Looking at your example, the compiler simply sees two 'if' statements. It is not concerned with evaluating the conditions in those 'if' statements; So far as it's concerned, they're unrelated.
Since, it reasons, both 'if' clauses could be false (since it hasn't evaluated and cross-referenced the clauses), then it's possible 'y' is being used before it's been assigned - hence your error.
Solution: Replace your 2 'if' statements with an 'if {...} else {...}'. This, of course, is exactly identical in function to your original code, but also lets the compiler know that every path through sets 'y'.
 
Saran Vel
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Great.. Now i got it..
Sorry for the wrong information..Radhika,
Thanks Rob,
Saran
 
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 Rob,
I changed the prgm as you mentioned and it is working..I got it now..
thanks for your help..
Hi Saravan,
Its ok..Any way now i got it..Thanks you tried to help me..
regards
radhika
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! radhika
the solution given by other challengers is insufficient and has
poor explaination.this can be proved by testing the following code.The logic of if{...}else{...} is not applicable here.still the program is running as expected.

//this was the problem code.
public class Compare {
public static void main(String args[]) {
int x = 10, y;
if(x < 10)
y = 1;
if(x>=10)
y = 2;
System.out.println("y is " + y);
}
}

//This is my piece of code .
public class Compare
{
int x = 10;
int y;
public static void main(String args[])
{
Compare cc=new Compare();
if(cc.x<10)
cc.y = 1;
if(cc.x>10)
cc.y = 2;
System.out.println("y is " + cc.y);
}
}

thanx,
prabal
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabal,
Nearly, but not quite.
Java differentiates initialisation between "class" variables and "method" variables.
In short, class variables are ALWAYS initialised to a default value - therefore by definition they are always "definitely assigned".
Method variables, on the other hand, are unassigned until you assign to them. Therefore the compiler performs the checks to ensure that they are definitely assigned before use.
So yes, you could move all your variables from the method to the class, but that's obviously poor design. Better is to use the 'if {} else {}' as I recommended, or to assign right at the start - eg:

 
prabal saha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sir,for the valuable concept of class and method variables.Now i got it.
prabal
 
Onion rings are vegetable donuts. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic