• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Initializing local variable

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InitEx {
public static void main(String[] args) {
boolean b;
if (b = false){System.out.print("A");
} else {System.out.print("B");}
}
}

Here b is a local variable,why doesnt it give compiler error when trying to use it before initializing?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have (b=false) where it should be (b == false)
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumi rankan:
class InitEx {
public static void main(String[] args) {
boolean b;
if (b = false){System.out.print("A");
} else {System.out.print("B");}
}
}

Here b is a local variable,why doesnt it give compiler error when trying to use it before initializing?



because (b=false) is an assignment and not an equality test (b==false).
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this results in expression being evaluated to false (with b is assigned to false).
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(b=false)

Here, you are intializing it rather than using it. So the compiler is not giving you any error.

The value of assignment is equal to the value of the variable which is false in this case.
 
sumi rankan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.I got it!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I still find odd about this is that the compiler provides no warning that the construct is a bit fishy. My experience is that modern C/C++ would provide a warning for an assignment inside the coditional statement. Just out of interest is there an option for javac that would pick this up? (I've tried -Xlint without success).
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output of the above program is
else (t = false)
if (f = true)
if (x = true)
else (y = false)


So, first assignment statement was executed and then variable at left is evaluated for Boolean checking.

Thanks,
Mohammad
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic