This question already has answers here:
Scala Unit type
(5 answers)
Closed 6 years ago.
My code is as follows:
myBuffer = myInput.read()
if (!myBuffer.!=(-1)) {
return -1
}
myBuffer is declared as a var int at the top of the class and I am trying to check when it equals -1 so that I can return -1. When I tried doing it the Java way, like so:
if ( (myBuffer = myInput.read()) == -1) {
return -1;
}
I got an warning saying that a test for equality with Unit will always give false, which was verified when I ran it and it never exited the loop that this code was a part of. This feels like it should be trivial but I haven't been able to find a solution to this. Any ideas?
using var is not recommended in Scala as you may know.
try scala.io.StdIn.readLine()
Related
This question already has an answer here:
Purpose of Objects.isNull(...) / Objects.nonNull(...)
(1 answer)
Closed 12 months ago.
Can someone explain the following code?
if (Objects.nonNull(department.getDepartmentName()) && !"".equalsIgnoreCase(department.getDepartmentName())) {
depDB.setDepartmentName(department.getDepartmentName());
}
The first stab at understanding how a piece of code works should be look at the documentation available.
In your context, code intends to check if department.getDepartmentName() is both not-null and an empty string, only then set it to some value.
On an additional note, double check is redundant.
"".equalsIgnoreCase(department.getDepartmentName())
checks both, if department.getDepartmentName() is not null and is blank string.
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 answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem
This question already has answers here:
Best way to "negate" an instanceof
(9 answers)
Closed 2 years ago.
Is it possible to get the opposite of instanceof in java? I have tried code like this:
if( example !instanceof blarg)....
but it won't let me put the ! anywhere without an error, please help.
You have to negate the entire thing:
if(!(example instanceof blarg))
You could also write it like so:
if(example instanceof blarg == false)
As an alternative make use of isInstance method:
if (!example.class.isInstance(blarg))
{
// your code here
}