Refactoring code in terms of cpu cycles JAVA [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 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.

Related

Which is better check the integer is 0 or check the variable is null? [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 2 years ago.
Improve this question
I tried to search the internet but I didn't see it's been asked. So our lecturer told us, we must write our own linked-list from scratch. Inside the linkedlist have head and tail pointing at another when you add item. Suppose if inside I already add a working integer counter. My question is I have 2 option to check if the linkedlist is empty:
check the head is null
check the counter is 0
my question is which is better in term of efficiency? I know the checking is millisecond matter, but I want to know, in theory, which one got better advantage over another? Sorry I'm not taking Operating System, I not know much the theory.
If you're asking which has better performance, they'll be exactly the same. In both cases you're doing a field or property access followed by a numeric comparison against a constant value. (null is just 0 as a memory address.)

Why is the catch Exception always called e [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 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.

Do I have to worry about default actions using ENUM? [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 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.

How to compare a String to two values in a single if [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 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.

Why java has no not function/ alternatives for same [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 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.

Categories