• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

problem in coding

 
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)".

 
Ranch Hand
Posts: 2410
  • 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!!!
 
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: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are required to initialize local variables before you use them.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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.
 
author
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
 
money grubbing section goes here:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!