• 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 Variables

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that local variable have to be initialized explicitly. The behaviour of tehse two programs is confusing me. Please help.
-----------------------------------------
public class local1
{
public static void main(String args[])
{
int x=100;
int y;
if(x<10)
y=1;
if(x>=10)
y=2;
System.out.println("Y is"+y);
}
}
This tells me y is not initialized.
--------------------------------------------
public class local2
{
public static void main(String args[])
{
int i,j;
int k=0;
j=2;
k=i=j=1;
System.out.println("K is"+k);
}
}public class local2
{
public static void main(String args[])
{
int i,j;
int k=0;
j=2;
k=i=j=1;
System.out.println("K is"+k);
}
}
This compiles fine.
-----------------------------------------------
Can someone explain???
Thanks
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Deepali,
Let me to "decode" your code..
Your code:
===========================
public class local2 {
public static void main(String args[]){
int i,j;
int k=0;
j=2;
k=i=j=1;
System.out.println("K is"+k);
}
===========================
"Decoded" version
===========================
public class local2 {
public static void main(String args[]){
int i,j;
int k=0;
j=2;
j=1;// k=i=j=1;
j=i;
k=i;
System.out.println("K is"+k);
}
===========================
I think, it's clear, now
Jamal Hasanov
www.j-think.com
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My ? is why it does not give compile time error though i and j are not initialized when it says local var have to be initialized.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From class local1:
...
if(x<10)
y=1;
if(x>=10)
y=2;
...
Even though it seems to us that y will be initialized every time the program passes through this code, the compiler has no way of knowing this. That's because it doesn't evaluate the expressions at compile-time, so it just sees two if statements. If it was something like:
...
if(x<10)
y=1;
else
y=2;
...
Then the compiler would see an if-else statement, so it's certain that y would get initialized. Note that if you changed the 'else' to 'else if(...)', you'd still get the compile-time error.

From class local2:
...
k=i=j=1;
...
Operands are evaluated left-to-right, but assignments are done right-to-left. So what happens here is that first, the operands are evaluated (stuff like determining array indices happens here.) Then j gets the value of 1. Then i gets the value of j (which is 1), and so on.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So can i conclude saying that Local Variables NEED NOT be initialized when they are declared(that is what i had understood before.
But they need to be initialized explicitly before the end of the local block whether thro statements like if-else or just as i=1;
Am i right???
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you just have to initialize them before they are used.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
So local varibles are not initilaized to default values implicitly. Got it.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class local1{


public static void main(String args[])
{
int x=100;
int y;
if(x<10)
y=1;
else
y=2;
System.out.println("Y is"+y);
}
}
how about this ?
i don`t why but i think you must have complete( if else )to initialized local var.
 
Deepali Pate
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not a matter of having a complete if-else black but the issue is before y is printed in the S.O.P statement it should be initialized.
If u give only if statement and if the 'if' statement return false then y will not get initialized. And unlike other variables local variable dont initialize to default value like 0 for int, false for boolean and so on. So when if return false and ur next statement tries to prnt y it finds that it is not initialized so u have to have else statement.

Originally posted by huanyu zhao:
public class local1{


public static void main(String args[])
{
int x=100;
int y;
if(x<10)
y=1;
else
y=2;
System.out.println("Y is"+y);
}
}
how about this ?
i don`t why but i think you must have complete( if else )to initialized local var.

 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Huanyu,

Either branch of code in this if-else statement initializes the value of y. The compiler sees this, so there's no error.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic