• 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

varaible initialize

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

please try the following code:
public class Arg {
public static void main(String args[]) {
int x = 10, y;
if(x < 10) y = 1;<br /> if(x >= 10) y = 8; //line 1
//else y = 8; //line 2
System.out.println("y is " + y);
}
}
the code will give compile error, but if you use the line2 instead of line 1 it works fine.
Can someone explain to me why and how this happen,
Thanks!
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the previous case where compiling problem is flagged, y is null and will not necessarily gets assigned. So, compiler flagged. In the latter case, y will be assigned each way. So, compiler knows it will be OK.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable declared inside the method should be initialized before to use.
Using line1
There may be chance that y will not get initialized
Using line 2
At any cost the variable y receives the value either from true or false block of if statement

Originally posted by Michael Lin:

please try the following code:
public class Arg {
public static void main(String args[]) {
int x = 10, y;
if(x < 10) y = 1;<br /> if(x >= 10) y = 8; //line 1
//else y = 8; //line 2
System.out.println("y is " + y);
}
}
the code will give compile error, but if you use the line2 instead of line 1 it works fine.
Can someone explain to me why and how this happen,
Thanks!


 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if(x<10) and if(x>=10) covers all the possiblities.
The difference may lie in that compile won't realize it by above code. But compiler knows if...else covers everything.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hh33,
Your name doesn't coform to the naming policy here at Javaranch. Please re-register with a more appropriate name. Check out this page for more info: www.javaranch.com/name.jsp
Thanks
Bill
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic