This question already has answers here:
Why can't I use the ternary ? operator to select between two function calls?
(4 answers)
Java: Ternary with no return. (For method calling)
(6 answers)
Closed 4 years ago.
c.getCollectibles()[i][j].isCollected() ? collectiblePanels[i][j].setSplash(c.getCollectibles()[i][j].getIcon()) : repaint();
I am writing a program in Java and have an error in my program. I am using a conditional operator. c.getCollectibles() returns a two-dimensional array of a Collectible class, and isCollected() is a non-static public method in the Collectible class that returns a boolean value.
collectiblePanels is a two-dimensional array of a class that contains a setSplash() method. The setSplash() method takes in the same type that getIcon() returns (the specifics are irrelevant to the error). Overall, setSplash() is void.
repaint() is a random void method. Again, specifics are irrelevant to the error.
The specific error is below:
The left-hand side of an assignment must be a variable
Syntax error on token "?", invalid AssignmentOperator
Syntax error on token ":", ; expected
Related
This question already has an answer here:
Any difference between `var x` and `int x`? [duplicate]
(1 answer)
Closed 1 year ago.
I am stuck on how to use var. Is there any difference between this two?
var input = (String) result.get("some field from DB");
String input = (String) result.get("some field from DB");
In java 10 and above when you use var you are allowing the compiler to attempt to decide what the variables type should be. In your example your casting a String so it will give that variable type String.
In your second example you are specifically telling the compiler that your variable is of type String.
The second is safer if you know what your variable type will be as it leaves no confusion down to the compile.
This question already has answers here:
Java: Not a statement
(2 answers)
Closed 2 years ago.
I want to know why the following is invalid in Java. Java compiler says that it is not a valid statement.
1+1;
I know the following works.
int i = 1+1;
Please explain why the second one is valid while the first is not. Thanks in advance.
Because you are doing nothing with 1+1. That is not a statement, it's an expression that returns a value that should be stored somewhere, like in the second example you give. If your statements have no effect, they are excluded from the language grammar.
The Java syntax needs the variable to be declared like the following
Class name = value;
You can't create a value without a variable definition and can't create a variable without a name and a class.
This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Boolean expressions optimizations in Java
(5 answers)
Closed 3 years ago.
I have a method with return statement like this:
return method(parameter 1, parameter 2) && method(parameter 2, parameter 1);\
However, when looking at my call tree, the second method is never being called (I see no calls with parameters like this). Can anyone explain why is this happening? Thanks
Possible short-circuiting, meaning if method(parameter 1, parameter 2) evaluates to false then the second method will never be called.
See also
This question already has an answer here:
Java Reflection: "java.lang.NoSuchMethodException"
(1 answer)
Closed 5 years ago.
ResultSet data_result = dao.getDetails();
Method resultset_method;
resultset_method = data_result.getClass().getMethod("getInt", Integer.class);
it is giving error:
java.lang.NoSuchMethodException: org.apache.commons.dbcp.DelegatingResultSet.getInt(java.lang.Integer)
The ResultSet's getInt method has two overloads, one that takes an int for the index and one that takes a String for a column name. You're attempting to retrieve a getInt() method, which indeed does not exist.
You need to supply the types that the method takes - eg, if you were going by name,
resultset_method = data_result.getClass().getMethod("getInt", String.class);
This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 8 years ago.
For instance if I have an if statement as follows:
if(returnsFalse() && timeConsumingFunction()){
//do whatever
}
Will the program run the time consuming function or will it realise that the if evaluates as false after the "returnsFalse()" function returns its value?
How does this work in different languages? Mainly interested in java and c.
No if you use && it will not continue on if the first statement is false.(Java) If you use & it will evaluate all expressions.