Java: Confused about the variable initialization concept - java

I'm new to Java and I'm having a bit of trouble understanding the concept of the declaration and the initialization of variables.
For example, when I do:
public class Foo {
public static void main (String[] args) {
int x, y;
for (x = 0 ; x < 10 ; x++) {
y = x + 1;
}
System.out.println(x);
System.out.println(y);
}
}
It does not compile and says that "variable y might not have been initialized."
However, it does not have any trouble if I tell it to just print out the x value after the loop. Of course it would work if I simply declared it in the beginning (saying int y = 0; or something like that), but I wanted to know why x is printed but not y.
Thanks in advance!
Edit:
I understand that the compiler doesn't actually check inside the loop to see if the variable would be initialized or not so it just says it might not have been initialized, but then why does the following code work? Does the compiler check the if loop but not the for loop?
public class Foo {
public static void main (String[] args) {
int x = 0, y;
if (x == 0) {
y = 1;
}
else {
y = 2;
}
System.out.println(y);
}
}
Edit 2:
It looks like it gives me the same error if I actually give another condition for the else part so that it would be:
if (x == 0) {
y = 1;
}
else if (x == 1) {
y = 2;
}
So I guess the other example worked since y was initialized in both the if and the else part, which means the y would always be initialized no matter what the condition given is. Now I really get it. Thank you!!

local variables don't have a default value and you need to initialize them. You are certainly setting the value of x (x=0), but the compiler doesn't check if the loop body will be actually entered. So give y a value of 0.

This is wrong:
public class Foo {
public static void main (String[] args) {
int = x, y; // Wrong
for (x = 0 ; x < 10 ; x++) {
y = x + 1;
}
System.out.println(x);
System.out.println(y);
}
}
This is correct:
public class Foo {
public static void main (String[] args) {
int x, y; // Declaration only: x and y are uninitialized
This is also correct:
public class Foo {
public static void main (String[] args) {
int x = 1, y = 10; // Declaration + initialization
'Hope that helps...

If you look in your code; you have initialized x to 0 in for loop and then incrementing it with x++. But you are initializing Y inside loop which may or may not execute at runtime (nothing to do with compile time). In Java, you have to initialize local variable before using it and if you are not doing so compiler will prompt the error. And that is why x is printed and not Y

It cannot be determined until run-time whether the for loop will be run even once. Therefore that initialization doesn't count (i.e., the compiler cannot depend on it, so it errors).
It cannot be determined until run-time which of the two -- the if or the else clause -- will fire. However, at compile-time we know that one OR the other will fire, so if you initialize in both, the compilation error goes away.

x gets initialized in the for loop (first argument)

Consider what could happen if your loop was
for (x = 0 ; x < someOtherVariable ; x++) {
and the value of someOtherVariable happened to be zero. The loop wouldn't run at all, so y would never be initialized.
Of course, the loop you wrote will always run ten times since the lower and upper bound are both hard-coded, but the compiler (apparently) doesn't analyze the code enough to prove that. It sees a variable that's only initialized within a loop, and following the general rule that a loop might not run at all, it complains that the variable might not be initialized.
BTW, int = x, y; isn't valid Java syntax: the equals sign doesn't belong there.

It depends on two things:
executes no matter what (here "x")
executes based on some constraint (here "y")
The compiler repels ambiguity in any case so it throws error on witnessing a variable initialization in a block which is conditionally executed.

Related

Java has (apparently) contradictory scoping

For this code sample the Java compiler is not happy:
public class Test1 {
public static void main(String args[]) {
int x = 99;
{
int x = 10;
System.out.println(x);
}
System.out.println(x);
}
}
The compiler says:
error: variable x is already defined in method main(String[])
However, the Java compiler is completely satisfied with this:
public class Test1 {
public static void main(String args[]) {
{
int x = 10;
System.out.println(x);
}
int x = 99;
System.out.println(x);
}
}
And that is a little piece of insanity in Java. When I look up Java scoping rules, I'm almost always seeing writers describe that variables are scoped within the block level they're in, except for instances of object level variables which retain their values for the instance lifetime.
None of them explain the rules in a way that explains the way the Java compiler deals with the code samples here. But as we can see from these two obvious examples, the scoping rules don't really work the way everyone is describing them.
Will someone explain this correctly, please, so that I am understanding it properly?
int x = 99;
{
int x = 10; // variable x defined before is still available, so you can not define it again
System.out.println(x);
}
System.out.println(x);
And:
{
int x = 10;
System.out.println(x);
}
int x = 99; // variable x defined before is not available, so you can define it again
System.out.println(x);
I can try to explain, but without knowledge of the exact rules.
In the first example, both x would be known in the inner scope. That's not allowed.
In the second example, in the inner scope, the outer x isn't defined yet. So there is no problem.
public class Test1 {
public static void main(String args[]) {
int x = 99;
{
int x = 10; // not allowed because the fist x is visible/known here
System.out.println(x);
}
System.out.println(x);
}
}
The scope of the first x in the first code is within the main method
so x is visible everywhere within the method , that is why it is not allowing
re-declaring it.
where as in the second code the the scope of the first x is within the block {}
so since out of this block x is not visible or not known, declaring the second x is allowed.
public class Test1 {
public static void main(String args[]) {
{
int x = 10;
System.out.println(x);
// x gets out of scope after this closing block
}
int x = 99; // allowed because the first x got out of scope
System.out.println(x);
}
}
It is not a problem with Java. The scoped variable x has gone out of scope when the second x is declared and used.

Calling function inside another function [duplicate]

Consider the following method:
void a ()
{
int x;
boolean b = false;
if (Math.random() < 0.5)
{
x = 0;
b = true;
}
if (b)
x++;
}
On x++ I get the "Local variable may not have been initialized" error. Clearly x will never be used uninitialized. Is there any way to suppress the warning except by initializing x? Thanks.
No, there is no way Java can examine all possible code paths for a program to determine if a variable has been initialized or not, so it takes the safe route and warns you.
So no, you will have to initialize your variable to get rid of this.
There is one :
void a () {
if (Math.random() < 0.5) {
int x = 1;
}
}
The compiler isn't responsible for devising and testing the algorithm. You are.
But maybe you should propose a more practical use case. Your example doesn't really show what's your goal.
Why don't you simply use
void a ()
{
int x;
boolean b = false;
if (Math.random() < 0.5)
{
x = 0;
b = true;
x++;
}
if (b) {
//do something else which does not use x
}
}
In the code why do you want to use x outside the first if block, all the logic involving x can be implemented in the first if block only, i don't see a case where you would need to use the other if block to use x.
EDIT: or You can also use:
void a ()
{
int x;
boolean b = (Math.random() < 0.5);
if (b) {
x=1
//do something
}
}
You can and should be defining the value of x unconditionally if it will be used later in your code.
There are a few ways to do this:
On initialization
int x = 0;
Because this is outside the conditional (if), Java won't complain.
Add else clause to conditional
if (Math.random() < 0.5)
{
x = 0;
b = true;
} else
{
x = 1;
}
Because there is an else to this if, and both code paths initialize x, Java will also be happy with this.
Move your usage of the variable into the conditional block
Clearly the question has a minimally-reproducible example, not a full one, but if you only ever want to use the variable conditionally, then it belongs in the conditional block.
if (Math.random() < 0.5)
{
x = 0;
x++;
}
If you don't aren't conditionally using the variable, then you need to provide an integer value to use in case Math.random() >= 0.5, using one of the solutions above.

Java Char increment [duplicate]

This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 8 years ago.
I was executing the following code
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++;
System.out.println(x);
}
}
Here the output is A.
My question is why didn't x get incremented before printing.
You're using the post-increment operator incorrectly - you don't need to use assignment as well. And in this case it undermines what you're trying to do.
For context, remember that the post-increment operator increases the value, and returns the old value. That is, x++ is roughly equivalent to:
int x_initial = x;
x = x + 1;
return x_initial;
Hopefully now you can see why your code is failing to change x. If you expand it, it looks like:
char x= 'A';
char y;
{
y = x;
x = x + 1;
}
x = y;
System.out.println(x);
and the net effect of the assignment, is to set x back to what it was originally.
To fix - you can simply call x++. Or, if you want to make it clear there's some sort of assignment happening, x += 1 or even just x = x + 1 would do the same thing.
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++; // it is post increment as ++ sign is after x
System.out.println(x);
}
}
Post Increment(x++) : First execute the statement then increase the value by one.
Pre Increment (++x) : First increase the value by one then execute the statement.

java cannot find symbol error: beginner

When compiled this static method comes up with the error that the int array variable, coord, cannot be found. I declared it within the method and it is of the type int[] and I can't figure out why it won't work. I have a feeling that it has to do with the method being static, but changing it to static was the only way I found to make that method work in the first place.
I feel like this is probably really simple for anybody but me especially when all I could find on this subject were much more complicated coding issues.
In case this helps.. this method is supposed to return the (x,y) coordinates for a move location. Sorry for probably not inputting the code correctly. First time doing this. Thanks in advance for any help
CODE:
public static int[] getMove(String player)
{
boolean done = false;
while(!done)
{
Scanner in = new Scanner(System.in);
System.out.println("Input row for " + player);
int x = in.nextInt() - 1;
System.out.println("Input column for " + player);
int y = in.nextInt() - 1;
int[] coord = {x,y};
if(getLoc(coord[0], coord[1]).equals("x") || getLoc(coord[0], coord[1]).equals("o") || coord[0] < 0 || coord[0] > ROWS || coord[1] < 0 || coord[1] > COLUMNS)
{
System.out.println("Invalid coordinates... please retry");
}
else
{
done = true;
}
}
return coord;
}
What you are missing is the scope of the variable. A variable declared in parent block is accessible in child blocks, but not the other way around.
public void someMethod()
{
int x=1;
while(x<10)
{
x++; //x is accessible here, as it is defined in parent block.
int j = 0; //This variable is local to while loop and will cause error if used inside method
j++;
}
System.out.println(j);// The outer block does not know about the variable j!!
}
Now in your case,
Notice where you have defined coors, and in what all places you are using it.
Try to figure where should you define coors variable.
That is because the array coord is local to the while loop. And therefore its not visible outside its scope. Move the declaration of coord outside the while and it should work.
int[] coord = new int[2];
while(!done){
...
...
coord[0] = x;
coord[1] = y;
...
}

Learning Recursion, error when calling class

public class recursion {
public static void main(String[] args)
{
thisclass(0);
}
public static void thisclass(int z)
{
int x = 1;
int y = 3;
if (z==10)
{
System.out.println("Done");
}
else
{
System.out.println(x/y);
x++;
y= y+2;
thisclass(z++);
}
}
}
I'm learning recursion right now and when I get to the else statement in the thisclass method, I get an error after it prints an abnormal amount of zeros.
What I want the program to do is run 10 times and do something along the lines of Print 1/3 2/5 3/7 etc.
Where do I go wrong?
This line:
thisclass(z++);
Doesn't do what you think it does. It increments 'z' and then calls thisclass on the original value of z. It's a lot like doing:
int temp = z;
z = z + 1;
thisclass(temp);
You want to use preincrement instead of postincrement here:
thisclass(++z);
The answers that drunkenRabbit and Serdalis have posted are also valid. It won't work correctly until you've made all of these changes.
There are as far as I can tell (other than naming conventions) three things that are causing your program to not run as intended.
(1) You are doing integer division in your print, so 1/3 will be 0 (no decimals in ints).
Solution: Change int x and int y to double x and double y.
(2) You are post incrementing z when you pass it into the recursive call, meaning the recursive call does not see the new value, but rather the old one.
Solution: Pre-increment z thisclass(++z).
(3) You also likely mean to have x and y declared outside of your method, so that their updated values persist. (Instead you would just be printing the same value 10 times).
Solution:
double x = 1.0;
double y = 3.0;
public static void thisclass(int z){ ...
You are getting all those 0's because you are doing integer division, which does not support 1/3 etc.
You should change your code to use float's which will get rid of the 0 problem.
E.g.
float x = 1.0;
float y = 3.0;
You are also resetting the value of y with each call, so y will always be 3 at the beginning of the call and 5 at the end. You should check the value of z to see what the value of y should be.
Same can be said about the value of x.
The z is being post-incremented at each call, which will cause the value of z to not increase with each call, you should change the call to:
thisclass(++z);
To make tha value pre-incremented. Otherwise this call will go on forever.
Also, please don't call your methods thisclass is it very confusing.
Incrementing x and y in the else condition does not affect the recursive call variable. Think of recursion as a new call to the same method.
So, when making the recursive call x and y are initialized back to 1 and 3. You can pass x and y as a parameter so the updated value can be passed to each recursive call. Is one way to go about this...
Hope this helps :)
Notes:
it's better to use an initial call function that calls the recursive function itself, for initialization
use static data, otherwise they won't be preserved through the recursive calls
recur(z++) will execute like recur(z) resulting in an infinite recurrence, use recur(z+1) instead
Code:
public class recursion
{
public static void main(String[] args)
{
startRecursive(0);
}
// using static data
private static int x = 1, y = 3;
// initial call
public static void startRecursive (int initZ)
{
x = 1;
y = 3;
// avoid infinite recurrence
if (initZ > 10) initZ = 0;
recur(initZ);
}
// recursive function
public static void recur(int z)
{
if (z == 10)
{
System.out.println("Done");
}
else
{
System.out.println(x/y);
x += 1;
y += 2;
recur(z+1); // z++ will return z
}
}
}

Categories

Resources