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
I'd like to ask you if I have to fill 'default' in the switch and 'else' in if statement while I'm using ENUM?
In other words is it ok to write a part of code like this:
enum Color {
RED, BLUE
}
void whatColor(Color c) {
if(c == Color.RED) {
System.out.println("It's red");
} else {
System.out.println("It's blue");
}
}
It depends on what you consider OK
If you consider OK that the code works as expected as of now, and the Enum is never ever going to change then yes, it may be said that is OK
Probably it will not be the case so aim for flexibility and change of requirements:
What happens when you add a new color? do you really want to say that is BLUE?
What happens when the color is null, that may represent unkown?
For the first question you may cover the method with a unit test.
For the second question I would at least add a UNKOWN and/or NO_COLOR value to the color and use them for the else branch accordingly.
Related
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 2 years ago.
Improve this question
For example:
try {
int n = scan.nextInt();
System.out.println("\nYour number is: "+n);
}catch(Exception e) {
System.out.println("You entered a value thats not valid.");
It seems like every program with a catch statement in it has the Exception called e. Is this like an unwritten rule or something?
The same reason why camelCase is generally used in java for naming, you can use snake_case but it's not recommended and makes your code harder to read for whatever reason.
String helloWorld;
String hello_world;//seems a bit off doesn't it.
Because of conventions. Naming things is hard. Having a common vocabulary helps understanding each other's code.
IMHO, no there is no such rule, it is just shorter like that... It is up to you and to your programming habit.
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 4 years ago.
Improve this question
This is what I want to happen:
char mirrorWords='x'||'w';
what I want to do with this is:
mirrorWords=='u'
returns false.
and
mirrorWords=='x'
returns true.
and
mirrorWords=='w'
returns true.
if this is possible how do I do it? And if not, well... thank you for your time.
update: I don't know why this is closed... I already accepted an answer.
No you cannot store more than one character in a single variable. However when you do the comparison you can do:
mirrorWords == 'w' || mirrorWords == 'x'
which will return true if either conditions is true, which sounds like what you want
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 8 years ago.
Improve this question
That is the method I've tried. If they type Yes with caps and yes without it will still count and it does not work.
Scanner in = new Scanner(System.in);
String In = in.next();
if (In.equals("Yes") & (In.equals("yes")) {
// Do this
}
else {
// Do this
}
Yes, you can do what you're talking about. This is the syntax:
if (In.equalsIgnoreCase("yes")) {
//Do something
}
It is also possible to do this:
if (In.equals("YES") || In.equals("yes")) {
//Do something
}
However, there's no reason to with the String method's case-insensitive comparison, see above. You really don't want to use your current syntax for 3 reasons: (1) AND operator is &&. Your operator is bitwise AND but this is not a bitwise operation. (2) It's not possible to have a string be equal to more than one thing, so use OR not AND if you're going that way. (3) Watch your parentheses, they're off.
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 8 years ago.
Improve this question
Ok so if you want to check if a list is not empty
we would need to do something like
if(! mylist.isEmpty())
this affects code readability, so how can we write the same thing in a readable way, calling out negation of condition check.
One of possibility is to have a static helper function like:
static boolean not(boolean condition) { return !condition;}
How bad is this idea? Are there other options in apache common or guava etc? Or any other way you have achieved this?
It is not a bad idea in itself and it will not affect anything.
However I think many people will disagree with you regarding the "unreadability" of the normal way of negating things using !.
if ( ! list.isEmpty() )
versus
if ( not( list.isEmpty() ) )
does not make much of a difference IMHO.
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 8 years ago.
Improve this question
Question:i just want to ask 2 question, if i write 2 below codes in java then which one is faster in terms of cpu cycles and why? Someone told me that 1st option is not correct in terms of cpu cycle because in that 2 conditions are checked i.e if and !
boolean flag = true;
//OPTION ONE
if(!flag) {
//error
} else {
//got the answer
}
//OPTION TWO
if(flag) {
//got the answer
} else {
//error
}
There is no practical difference on a modern JVM implementation.
The compiler will do the right thing, don't worry about it. In fact, if it can prove that flag always has a constant value of true at compile time it will eliminate the check and the unreachable branch entirely.