• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

compilation question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the foll code:
---------------------

-------------------------
local variable tmp is not initialized by an unconditional assignment but it still compiles. this contradicts what mughal says in his java book. Can someone help?
Thanks,
Akhilesh
[ Jess added UBB [code] tags to preserve whitespace ]
[ June 09, 2002: Message edited by: Jessica Sant ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's because the compiler knows because of the straight if/else statement that the 'tmp' variable is guaranteed to be initialized.
If you adjust your if-else statement to the following, it will not compile, complaining that the variable might not have been initialized:

.... can you figure out why?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because the compiler doesn't evaluate the expressions at compile time, so it won't know if either of them will always execute, right?
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akhilesh,
The code compiles because temp variable is initialized in the if condition as well as the else condition.If the if condition is executed,temp will be initialized,and if the if condition is not executed,then also temp will be initialized.Thats why,there is no compile time error.
For more information,you can read the JLS section of unreachable statements and definite assignment.Those are very informative chapters and will certainly improve your knowledge.
 
Akhilesh Varma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all,
if there had been another 'if' instead of 'else' then the code won't compile because the compiler is not sure that the condition will be satisfied and the var will ever be initialized.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic