• 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

local variable initialisation

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please look at the following code
class strr
{
String msg="type is";
public void show(int n)
{
String tm;
if(n> 0) tm="positive";
else
tm="not positive";
System.out.println( msg +tm);
}
}
if the else part is commented ,the code gives compiler error but
putting the else part avoids the warning of tm not initialised.why???

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres what I think.
All local variables must be initialised otherwise the compiler
will throw an error. So String tm should be explicitly initialised in the method to some value.
If you dont do that, the compiler checks whether a value will
always be assigned to this variable. the if else clause
is such that it will always be executed and in both cases
tm is being set to some value, so the compiler does not object.
The end result is that tm still gets a proper value. But if you comment out the assignment to tm in either the if or the else portion, it will give an error.
Please correct me if i am wrong.
Sajida
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi preeti,
if u comment else part then the case in which n is less than zero
will not be handled.hence the local variable tm would remain uninitialized.thus it gives compliler error.
hope this hepls.
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thanks sajida and kriti,that clarifies my doubt
thankyou again
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi preeti,
i tried ur code and commented out the else part but the code compiles fine. i removed the comments and it as expected compiled. but the even though i passed a +ve value to show(). the ouptut was "type is not +ve". cud u brief on the environment ur working on. ( ihope im not wrong).
bye.
 
preeti dengri
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 amit,
i am working with jdk1.3 and using sun's compiler only and the above program is giving me compiler error if no else part is there.
 
preeti dengri
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 all,
please see to this as an extension to above topic:
if i comment out the else part and initialise tm as
String tm;
tm=" ";
then program is compiled .
i want to know that the declaration and initialisation need not be at the same place in case of local var while it has to be at the same line in case of instance var.
Is it so???if not then please explain.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preeti,
The local variable declaration statement may or may not have initialization statement. If a local variable decalration does not have an initialzation, then compiler must prove that, according to the rules of definite assignment, every reference to this variable is preceded by an assignment to it.
In the example given by you, when "else part" was commented out, the compiler knew that assignment to local variable takes place only when condition was true, (i.e for n> 0). in all other cases, assignment was not taking place. This is not sufficient in case of local variables.
On the other hand, when else part was not commented out, compiler knows that whether or not condition is true or false, assignment takes place always.
If you want to go further and experiment, I would ask you to see what happens when following code is executed.
... //rest of your code
String tm;
if (true)
tm = "positive";
System.out.println(msg+tm);
... //restof your code
Do you think that this will compile? Sure it will! Because we used "true" (which is a literal constant that has a value = true always) as the conditional expression and compiler knows that assignment will take place always in this case.
I hope I cleared your doubts. If you have any further questions, please feel free to post them here.
Thanks,
Rajesh
[This message has been edited by Rajesh Radh (edited March 28, 2001).]
[This message has been edited by Rajesh Radh (edited April 04, 2001).]
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you rajesh for taking time to explain it so clearly.now i got it.
thankyou all
 
reply
    Bookmark Topic Watch Topic
  • New Topic