In the following code I'd expect that it should not be necessary to initialise variables a and b in last else block, however the compiler does not like it.
import java.util.Random;
public class Foo {
private void foo () {
double a,b;
boolean c;
double r = (new Random()).nextDouble();
if(r < 0.25) {
a = 1;
b = 2;
c = true;
} else if(r >= 0.25 && r < 0.75) {
a = 3;
b = 3;
c = true;
} else {
// why is it necessary to init a and b here?
// given that c is set to false
c = false;
}
if(c) {
double k = a + b;
}
}
}
With the code above, the compiler does complain.
bash-3.2$ javac Foo.java
Foo.java:25: variable a might not have been initialized
double k = a + b;
^
Foo.java:25: variable b might not have been initialized
double k = a + b;
^
2 errors
I would have thought that the compiler could do the static analysis to figure out that k will not be evaluated if c is set to false. So my question is why does the compiler demand that I initialise a and b?
The compiler isn't smart enough to understand that going through the else block will set c to false, and that the next if block thus won't ever be executed. The static analysis is more limited than what you expect, which also makes the comilation faster/
And it's probably a good thing, because changing the code of the else block would suddenly make the next if block not compilable, which would be annoying.
Related
In JavaScript you can add extra conditions like:
var b = 0.0;
var q = (void 0);
var e = -1.0;
while(q = e, b > 32.0){
console.log(q);
b++;
}
Meaning that q equals to e.
I have tried to rephrase Java code to
Float b = 0.0;
Float q = Float.NaN;
Float e = -1.0;
do{
q = e;
b++;
}while(b < 32.0);
But it seems that it doesn't work as JS version.
Can I just add q = e to while conditions? Is there any valid Java syntax for it?
There is no comma operator in Java, and even if there was, it would be considered bad style.
There are ways you can achieve similar things. If you define a function that takes any parameter and always returns true:
<T> boolean trick(T any) {
return true;
}
you can use it to sneak in assignment expressions in any boolean context you want:
while (trick(q = e) && b > 32.0){
System.out.println(q);
b++;
}
But again, this would be considered terrible style. Don't use this in a real project.
Java doesn't have a hyperflexible comma operator like JavaScript so you'll have to split the statements apart. That's not a bad thing. while(q = e, b > 32.0) is poor style.
Stick with a regular while loop. A do-while loop won't do because it'll always execute b++ at least once.
I would use double rather than float.
double b = 0.0;
double q;
double e = -1.0;
q = e;
while (b < 32.0) {
b++;
q = e;
}
And we might as well initialize q when it's declared:
double b = 0.0;
double e = -1.0;
double q = e;
while (b < 32.0) {
b++;
q = e;
}
Is it allowed to just place some curly braces without any if/for/etc statements to limit the variable scope?
An example:
public void myMethod()
{
...
{
int x;
x = 5;
}
...
}
I may want to do this, so I know for sure I won't access/change the variable outside the scope and that it will be destroyed beforehand
Yes, it's allowed. Just try and see for youtself
The curly braces { .. } limit the scope of variables to the block.
However, changes can be made to global variables falling into the scope of { .. } block.
int x = -1;
double y = 5;
{
x = 10;
y = 100;
char c = 'A';
}
System.out.println(x + " " + y); // 10 100.0
System.out.println(c); // Compile time error and out of scope
{
c = 'B'; // Compile time error and out of scope
}
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.
So well I'm just here wondering if there's a way to pass a variable through a statement. Something like this:
if (a < b) {
double g = 1
} else if (a > b) {
double g = 0
}
if (g = 1) {
System.out.print("true");
} else {
System.out.print("false");
}
Mainly saying, I want to set a variable if a statement is true or not, go to the next section of code and print out "true" or "false" and I pretty much am just wondering if this is possible without creating a new method (and of course if there is code for it).
Thank you.
You are almost there. You have to declare g outside the if statements, so you can access to it whithin the whole function. Read more about scopes, if you declare a variable inside a block {}, it will be accessible just inside it, so when you declared it into the if-else if blocks, you couldn't access to the variable outside.
Also to compare a primitive type (in this case double) you have to use == operator, because = is used for assignment.
double g;
if (a<b) {
g = 1;
}
else if (a>b) {
g = 0;
}
// What happen if 'a = b'?
if (g == 1) {
System.out.print("true");
}
else {
System.out.print("false");
}
Note: What value will take g if a == b? You may want to take care about that case too.
double g;
if (a<b) {
g=1
}
else if (a>b) {
g=0
}
if (g==1) {
System.out.print("true");
}
else {
System.out.print("false");
}
also make sure that you always use == instead of = in your if-statement
The if condition if (g=1) does not work with java. This would work with C though.
You should code if (g==1) to test if g is in fact equal to the int value 1.
You've got three problems.
a and b aren't defined. Define them before entering the if statement.
Define g outside of the if statement (a simple double g; will suffice), then set the values as part of your conditional logic. You do have to give it a default value if you intend to keep the else if there, since Java would complain about that not being defined.
g=1 isn't going to work the way you think it should; you probably mean g == 1.
With else if
int a, b; // assumed instantiated with values
double g = -1; // required since Java can't guarantee that the else-if will be hit
if (a<b) {
g = 1;
} else if (a>b) {
g = 0;
}
With else
int a, b; // assumed instantiated with values
double g; // instantiation not required since Java can guarantee the else case
if (a<b) {
g = 1;
} else {
g = 0;
}
double g; double a = 4.0; double b = 3.0;
if(a < b){
g = 1.0;
System.out.print("true");
}
else if (a > b){
g = 0.0;
System.out.print("false");
}
// Why not write your code like the above example. It seems like the
//same operations are executed but with less lines of code.
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.