Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in coding

 
Samarth Barthwal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i made the following code to check whether a number is prime or not but it gives an error on compilaton which is "variable flag might not be initialized if(flag==1)".

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you use a local variable in a method, before you use that variable in any kind of comparison or calculation or other statement that depends on its value, you must make sure that that variable is initialized before it is used. Because it's possible that for loop may never execute, and you only initialize the variable in the for loop, the compiler complains that it may not have been initialized.
 
Samarth Barthwal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much it worked!!!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a different note: why are you using a 0 or 1 "flag". You could use a boolean variable to represent your "flag".
 
Samarth Barthwal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a boolean is also a good idea.
Is it a convention or a compulsion to initialize a variable before using in a function?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are required to initialize local variables before you use them.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samarth Barthwal:
... Is it a convention or a compulsion to initialize a variable before using in a function?

Unlike variables at the class level, local variables are not automatically initialized, so you are required to give them a value before using.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on above point, you need to also understand how local variables and class or instance variables are stored.

Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed.
 
Samarth Barthwal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you to all of you for your ideas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic