File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problem in coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in coding" Watch "problem in coding" New topic
Author

problem in coding

Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 16, 2006
Posts: 23
Thank you very much it worked!!!
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
On a different note: why are you using a 0 or 1 "flag". You could use a boolean variable to represent your "flag".


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
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

Joined: Feb 07, 2005
Posts: 2367
You are required to initialize local variables before you use them.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Samarth Barthwal
Greenhorn

Joined: Feb 16, 2006
Posts: 23
thank you to all of you for your ideas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in coding
 
Similar Threads
simple q but just to confirm
Prime No.
Putting prime numbers into an array
problem with java code
program correction