Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to set a property to an other property that could be null. I wonder what is faster, to wrap it around an if statement or simply just set the property without the check.
with if statement
if (foo.getName() != null) {
bar.setFoo(foo.getName());
}
simple
bar.setFoo(foo.getName()); //getName() can be null
If you are going to setFoo() either way, you might as well just set it and skip the condition. I would use the condition if you wanted to take an action if !null and a different action if null.
when foo.getName() != null would return true, the setter would be called, so you will have 2 commands that will be executed, however the second example will execute only 1 command. I think the second one is more performant
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I saw below code in some codebase, and I am not sure how we can return Optional for a String return type.
public String generateTemplate() {
Optional<String> template = Optional.ofNullable(lenderAndTemplateMap.get("LENDER_1"));
return template.orElse("TEMPLATE_2");
}
Although the line reads as "return template [if possible], or else return "TEMPLATE_2"", the Optional itself is never actually returned. It's just a call to Optional.orElse(), which returns the internal value if it's present, otherwise it returns the value passed in. Both are of type String, therefore it's a valid return statement.
As a side note, in your specific example Optional isn't really necessary. You can use a simple null check or Map.getOrDefault():
return lenderAndTemplateMap.getOrDefault("LENDER_1", "TEMPLATE_2");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Wasn't sure how to search for this and was unable to find the answer to set the following to true (Java):
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.getWasRefunded();
}
This is a lesson to teach you how to read the code if you see the method that starts with "get" then you use it to get some value from the method that returns it. When the method starts with "set" then this method usually returns void and takes parameters. Like this
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.setWasRefunded(flagRefund);
}
Usually, get methods are used to retrieve information, not to set information. Look if there is some set in your code method to achieve it.
Also, you don't pass any value to your method getWasRefunded as parameter so it is impossible that it could set any info.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I used in my code "".equals(val) but if it is "".equals("abc")
then, what should be the correct expression for it.
Having a string literal on left-hand side of expression will not generate the NullPointerException.
if(a !=null && a.equals("value")) you can replace with if("value".equals(a)).
PS
Having the constant on the left-hand side is cleaner approach as prevent from making errors. It is can be called as yoda condition.
"abc".equals("def")
is equivalent to
"def".equals("abc")
if that's what you are asking...
Comparing two string literals? Just use the boolean constant, you know they're the same (or not) already.
Comparing a string literal and a string variable? literal.equals(variable). This prevents null pointer exception.
Two variables? Check for null, then nullCheckedVariable.equals(otherVariable).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am doing some validation using simple Java, I am coding as below
if (something.equals("ABC"))
expectedvalue = "TEST1";
else if(something.equals("DEF"))
expectedvalue = "TEST5";
// and so on....
My issue here is that, if something.equals("ABC"), expectedvalue can be any of these two values -> "TEST1" or "TEST2"
During validation, in the output, I do not get error if I give "TEST1" for expectedvalue. It should do the same even if I give "TEST2". How to do that?
You have to have a Set of allowed values.
Set<String> allowed = new HashSet<String>();
allowed.put("TEST1");
allowed.put("TEST2");
//and then you can use this set
if (allowed.contains(yourValueToCheck)) {
// do what you need
Also note that it is safer to compare strings like this:
if ("ABC".equals(something)) // to protect yourself from NullPointerExceptions
Perhaps you could change your test to test with an OR statment?
if(expectedvalue.equals("TEST1") || expectedvalue.equals("TEST2"))
.....