Difference between i++ and i-- [closed] - java

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 was asked this question in an interview.Please provide the solution if you know about this thing.Is there any difference between
for(int i=0;i<=N;i++)
and
for(int i=N;i>=0;i--)
which runs faster and why?
PS: Please differentiate on the basis of performance and the way compiler takes it.I am not asking about the basic difference between postfix and prefix notation.

I think this has the potential to become a better question, if we had more info. You should specify what language you are referring to. If this is JavaScript or something else then you need to mention that.
Secondly, consider revising your question. I suggest asking under which situations one is more appropriate than the other.
Whatever the language, the difference between the two is a stark contrast. The first for-loop involves incrementing a variable until a maximum value is reached (metaphorically-speaking, kind of like a race to the top), while the second involves decrementing a variable until a certain minimum is reached (like a race to the bottom).
Incidentally, in some languages, such as PHP performance is faster in a loop increment expression if you write the increment or decrement operators on the left instead of on the right.

This is an excellent interview question because any answer you give is likely to be wrong and more importantly be something you never previously thought seriously about.
The whole point is to throw you off your game. They want to see how you react when you're pushed into an area that you feel like you should be expert in yet find something about which you are not. Knowing the perfect answer to this question doesn't help you because they'll have 12 more questions lined up to throw you off.
What they want is to see how you respond to this situation. Do you make stuff up? Do you think about it carefully? Can you justify why it's not an important concern? Do you insist your way of looking at it is the only valid way? Do you listen when told of another way? Are you a pain to deal with if told to do it another way?
They will care so much more about the answers to this than whether or not you can save them a CPU clock tick.
But if it turns out you are an expert in this one dusty arcane corner you might earn a point.
If that is your real question then it's a duplicate of this question: Which of these pieces of code is faster in Java?

Related

Design on how to store large objects in a list [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 4 years ago.
Improve this question
I need a hint on an Interview question that I came across. I tried to find a solution but I need advice from experts over here. What are the different strategies you would employ had you came across this particular situation? The question and my thoughts are as follows:
Q. You want to store a huge number of objects in a list in java. The number of objects is very huge and gradually increasing, but you have very limited memory available. How would you do that?
A. I answered by saying that, once the number of elements in the list
get over a certain threshold, I would dump them to a file. I would typically then build cache-like data-structure that would hold the most-frequently or recently added elements. I gave an analogy of page swapping employed by the OS.
Q. But this would involve disk access and it would be slower and affect the execution.
I did not know the solution for this and could not think properly during the interview. I tried to answer as:
A. In this case, I would think of horizontally scaling the system or
adding more RAM.
Immediately after I answered this, my telephonic interview ended. I felt that the interviewer was not happy with the answer. But then, what should have been the answer.
Moreover, I am not curious only about the answer, I would like to learn about different ways with which this can be handled.
Maybe I am not sure but It indicates that somewhat Flyweight Pattern. This is the same pattern which is been used in String pool and its efficient implementation is must Apart from that, we need focus on database related tasks in order to persist the data if the threshold limit is surpassed. Another technique is to serialize it but as you said, the interviewer was not satisfied and wanted some other explanation.

JAVA: Create boolean variable or put argument within If statement? [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
I've been running through a lot of code reviews and keep running into a situation where I see conflicting coding standards regarding boolean variables and if statements.
Here is an example of one method(1):
boolean isXTrue = getBooleanValue(DataSetX);
if (isXTrue) {
//do code
}
Here is an example of the other method(2):
if (getBooleanValue(DataSetX)) {
//do code
}
Both do the same thing and function just fine. In some cases, method 1 is a lot more readable since the boolean variable can be named something meaningful, while method 2 saves more lines and unnecessary boolean variable creations.
Maybe I am reading too deep into a simple coding standard, but I'm rather curious that if we use method 1 more often, we could have unnecessary booleans being made.
Sorry if this is a stupid question, but I wanted to get some opinions anyway :)
It's likely that the compiler will optimize both cases so that either way is identical at run time. Of course, that depends on code outside the context that you've provided.
As for the question at large: it's something that you and your coworkers or group need to come to a consensus about. If you're looking for a definite answer about which one to choose, I don't think you're going to get anything convincing other than personal preferences of readability vs line count.
Discuss this with the others that maintain your code base and decide on which should be preferred. Clearly explain why. Then move on to more...err...important issues.
As for my preference? I like option 1. To me, it's more readable, the variable name can be something descriptive like isActive, which makes the code easier to read. Also, inspecting values during debugging is probably easier as you have a definite variable with which to reference prior to its use later in the chain. Again, that's my preference.

What coding issues can be significant in damaging a game's performance? [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 9 years ago.
Improve this question
I have just finished a 2D game in Java that I've been working on for a while.
I found that on some computers it runs fine, and on others (which are not particularly slow in general), the game runs a little slower, and with minor interruptions every second or so. Not completely smooth.
Since I am a beginner to making games, and probably since I didn't plan everything about the game in advance, the code of the game is rather long, clumsy and probably inefficient.
I want to improve this in my next projects. So my question is - In general, what would be the main causes for a common 2D game to slow down on a computer?
What should I pay the most attention to, next time, in order to design an efficient game?
Making a small amount of classes? (Even if the classes are small
ones?)
Avoiding repetition of code? (Even small sections of code, such as
short if statements).
Avoiding too many threads running?
Anything else?
Obviously, all of the above are recommended for an efficient program.
But I'd like to know, what in a game's code, could be especially significant for making an efficient application, and what would be less important and will not save significant amounts of memory.
Any advice would be welcome - could be regarding game design, or regarding more specific coding issues.
I don't know if this matters, but please note that I'm talking mainly about real-time games, using a 'game-loop' that constantly updates the game and the dispaly.
The important thing when trying to improve the performance of any program, not just a game, is - don't guess.
If I or anyone says "it's in your collection classes", or "it's in your rendering", or "it's in your memory management", or "it's in your compiler optimization", can you trust it?
Short answer - No - because it's a guess.
It could be true. It could be false. Nobody knows, in your case.
People who say instead of guessing "Use a profiler" are on the right track.
In my opinion there's an even better method, spelled out here.
If you need to know why, I'll explain it further, but the hard part for any programmer is to stop trying to think it out, and let the tool tell you what to look at.

Is it worth to rewrite an entire program? [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 9 years ago.
Improve this question
I have a Java Project I have been working hard on for about 6 months (3 months hardcore) and it is around 8000 lines of code... However I have gotten much better then I was 6 months ago, and I have multiple classes that are full of messy, unorganized, and uncommented code.
Although my new code is much better, I often find myself lost when looking at some of my older code. Do you think it is worth the time to rewrite about 4000-5000 ish lines of code, that I think are not written well? I do not have much time to work, only about 3 hours a day max.
I don't know if I should accept that my old code was old, and just continue on, or if I should go back, completely rewrite most of my program and then have a easier path in the future. Or maybe there is a different solution? Any thoughts? Thanks! I really appreciate the help because currently I don't know if I should proceed knowing I may be just wasting time.
Yes!
Having been in the same position (more than once sadly) I can say that it is well worth it - if you come back to one of your projects in 6 months time - you'll look at that old code and wonder what the heck you were trying to do. Also, you may want to send examples of your work to prospective employers at some point, and if they look at code which is sloppy - it doesnt look good. Worse yet, if they look at the code and see two wildly different styles (sloppy vs neat) they will think you plagiarised the code.
You will also (most likely) find that rewriting those 4k lines of code, results in far fewer lines of code. Or, that it results in code that is far more reusable. Try to think about DRY when you recode (dont repeat yourself) if you use something more than once, make it a function or a class.
Also, you will save LOTS of time in the future when you come to extend/change your work.
Sure go for the rewrite. You are still learning and probably learned a lot since you first started. Now that you know more about programming and have a better idea of what the software requirements are you can probably make it much better.
Having said that, this is just a project for fun. In the business world, you would have to consider the time it takes to do a rewrite in terms of money spent and time and money wasted not adding new features etc and compare that with the amount of money wasted trying to maintain the old system to keep it running. Only if it's cheaper overall to do a rewrite will most businesses go for it.
It may be very beneficial as the other posters have mentioned. You should take this opportunity to write JUnit tests for all your new code (if you're not already doing so), as this will help you write well-encapsulated and decoupled code. Two really good books for reference are Refactoring by Martin Fowler (http://martinfowler.com/books/refactoring.html), and Working Effectively with Legacy Code by Michael Feathers (http://www.amazon.ca/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052).
Have fun!
This is a common question asked by many developers, and it leads to other more important questions that will aid you on your journey to become a better programmer.
Ask yourself:
do i use javadocs in my code?
do i often repeat code?
do i use established patterns and stick with them for the entire project?
The reason i ask is that the answer to these questions will effect how readable your new code will be in the future even if you do rewrite your code.
A good excercise that will prove very useful in the real world is: can i optimize the existing code without rewriting it? this is a much sort after skill if ever you were to program professionally.

Should tricky questions exist on the OCPJP exam? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
When I practice to take the OCJP exam, I see that the questions that I'm suppose to practice on are often trick questions which try to fool my into answering wrong. The example is from the Bathes / Sierra book and a typical trick question is:
Now I wonder if you can tell me if the questions on the real exam also often are trick questions like these or if the actual exam has another style, or if this is close to what I should expect?
The given example is not tricky. It simply measures that whether you know the difference between constructors and methods. Constructors and methods might have the same name, it is not a compilation error.
The exam may contain questions like these to trip up the participants. However, you should keep in mind that every question in the exam is just for measuring your ability and knowledge in certain exam objectives. You should ask yourself while reading the question: "What objective might this question be measuring?"
Now I wonder if you can tell me if the questions on the real exam also often are trick questions like these or if the actual exam has another style, or if this is close to what I should expect?
The point of grilling you through such questions is to help you get your defenses up. This is not a trick question like #Juvanis has pointed out, but such questions will help you develop a pattern to identify faults in code. Usually a pattern begins to emerge and your brain starts to analyze the code like so...
Do all the necessary imports exist and are they correct ?
Are non-static variables accessed from a static context ?
Check method return values and return type.
Check autoboxing / unboxing errors.
... and so on
The real exam is easier. However if you prepare with harder questions, the chances of succeeding are better.

Categories

Resources