How object class non Null works [duplicate] - java

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.

Related

Invalid Java statement [duplicate]

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.

Recursively returning two methods via && [duplicate]

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

What does the number after # in thread view in Intellij IDEA mean? [duplicate]

This question already has answers here:
While debugging java app what information is shown for a variable in a stack frame [duplicate]
(3 answers)
Closed 4 years ago.
What does 954 mean? I have checked both thread's id and hashcode(), but they don't equal 954.
Also, when using evaluate, there is also a number after #, I think they have the same meaning but still couldn't find out what's the meaning.
Interesting question. I just always took for granted that it is some id that uniquely identifies the object.
Based on that assumption it could for example be the uniqueId() returned by the Java Debugger Interface for an ObjectReference:
https://docs.oracle.com/javase/7/docs/jdk/api/jpda/jdi/
But that is really just an assumption.

Why the java array documentation doesn't show the field "length"? [duplicate]

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 7 years ago.
I have seen some examples of getting the length of an array using the field length, for example: array.length. I have always used this field but checking the array documentation I did not see that variable. Why is it that the documentation doesn't show it? It only shows a bunch of methods but I can't see the variable length. Is it in another class or what? I have seen questions like this before but the answers are not well explained so I can't understand them.
Because length is not actually a field. The compiler recognizes the identifier specially and translates it to an arraylength instruction rather than a getfield instruction.

Weird Java extended ascii error [duplicate]

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

Categories