Why can't the compiler warn against this programming error? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
If I write the following code:
int a = 5;
if (a == 1 || a == 1) {
// do something
}
Why can't the compiler point out that the second part of the if statement is unnecessary, or warn that the programmer has likely made a mistake?

The compiler does not do it. But the IDE might detect the underlying problem. For example:

Related

Bytecode size of with brackets vs without brackets in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Which has a larger bytecode size or the same in Java?
if (a > b) a = b;
vs
if (a > b) {
a = b;
}
These compile to precisely the same thing.

Best way to write Java 8 map statement that returns constant value [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have the following statement. The map always returned "Closed". But I think this statement s -> "CLOSED" could be rewritten using a better style.
Is there any better way to represent this?
String status = myOptional.map(s -> "CLOSED").orElse("OPEN");
Just use a ternary operatory to check for its presence :
String status = myOptional.isPresent() ? "CLOSED" : "OPEN";

Character length finding on string [closed]

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
Can anyone help me to write code that find how many characters string has.
All you need is - int len = yourString.length();
StringUtils should provide you with something that you are looking for:
int count = StringUtils.countMatches("haahaahaa", "a");
System.out.println(count);

Java BigInteger OR function [closed]

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
I am using Java and have some problems.
I made two BigInteger variables, p and q.
In my code, I want to add if function, like if(p=1 \ q=1).
I tried many ways but there was error. Do you know how to solve this?
Your question is not completely clear, but you need to use the BigInteger.equals() method, as in this example:
if (BigInteger.equals(p, BigInteger.ONE) || BigInteger.equals(q, BigInteger.ONE)) {
// do something
}

Why is there no function Stream.flatMap()? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why is there no function Stream.flatMap() (without any parameters) to flatten a Stream<Stream<T>>?
It would simply be implemented as Stream.flatMap(o -> o).
In my opinion, this is by far the most common use of flatMap(Function mapper).
I would imagine because it's trivial to use
import static java.util.function.Function.identity;
...
streamOfStreams.flatMap(identity())

Categories

Resources