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
}
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:
How do I compare strings in Java?
(23 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
I'm trying to make a user validation for multiple choices using a while loop and If the user doesn't match the choices then the program has to terminate. This is what I got so far
while(input!="air" || input!="water" || input!="steel"){
System.exit(0);
}
Unfortunately, it doesn't work.
I would prefer to do exceptions, but my professor is strictly against using it for this assignment. I really want to grow as a programmer so any advice, tips and tricks are welcomed, thank you all for trying to help me out.
Change it as
while(!input.equals("air") && !input.equals("water") && !input.equals("steel")){
System.exit(0);
}
Or
while(!(input.equals("air") || input.equals("water") || input.equals("steel"))){
System.exit(0);
}
This question already has answers here:
Avoiding NullPointerException in Java
(66 answers)
Closed 6 years ago.
I have some data in input that I'll have to use to set all properties of a POJO. The POJO might be partially set. My problem is to set the property only if related input data is not null.
I know I can do this in two ways:
if (input != null) {
obj.setData(input);
}
or
obj.setData(input != null ? input : obj.getData());
I'm looking for a solution less ugly and better for objects with a big number of properties to set.
You can also use java8 Optional
obj.setData(Optional.ofNullable(input).orElse(obj.getData()));
Or even use a more elegant way:
Optional.ofNullable(input).ifPresent(obj::setData);
Use guava:
String someString = "value";
Optional.fromNullable(someString).or("defaultValue");
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()
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am working on a multiplayer game.. and i came up with a really weird situation.
It's even possible? -
System.out.println(foo); // left
System.out.println(foo instanceof String) // true
System.out.println(foo == "left") // false
I don't understand how it's even possible..
I really don't think it will be a good idea to post all of my code.
Any ideas why its like that? and how can i fix it?
Thanks in advance
You shouldn't compare Strings by == operator, use equals method instead.