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
Right now, I'm developing a heavy 2D game write in java+LWJGL (ok, it could have looked funny when I said "heavy 2D game", but trust me, I'll be using LOTS of resources), and, well, let's say I have TOC (TOC is in Brazil, maybe in english it's... OCD?), so I have to write, for everything I'll use third-party, an API. Like, I have an API for LWJGL (an API for an API of OpenGL, lol), and a lot of other API classes. And, inside these APIs, on constructors, I never use primitives (constructors only, fields I still use primitives), but instead I use Number class (so I can call the methods using Integer, Float, Double, and whatever I want). Then, inside the constructor, I translate the number, using number.doubleValue() or number.intValue(), depending on what I want.
So, do this can affect dramatically the performance of my game? Until now, in the early-middle development stage, my performance is still OK but I'm worried of it causing me to re-write most part of the code late. Oh, and sorry for my soo bad english, it's not my primary language.
Don't use boxed primitives unless you really need them. A good reasons may be
you need an representation for the undefined state (use null)
you put them into Object-accepting classes (List, Set, Map, ...) and want to avoid boxing (by working with boxed values all the time)
Anything performance-related must be benchmarked first, otherwise you can find yourself optimizing a piece of program taking no measurable time or even "pessimizing" it. Low-level optimizations in Java are pretty hard, so you'd better concentrate on clarity and readability, so you can measure the speed, identify the bottlenecks, and optimize them afterwards.
Concerning boxed primitives, IMHO the biggest performance impact doesn't come from object creation but from the indirection (creation happens once, repeated cache misses are costly).
I disagree with dognose concerning the "fancy OOP stuff". Everything the classes offer can be used without boxing and working with boxed primitives is a pain the OOP methods can't ever make good (the unexpected NullPointerExceptions, non-working conversions, etc. are IMHO too bad).
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 3 years ago.
Improve this question
I'm fairly new to Java but love it so far. My question is, i'm a little unfamiliar with Classes. I know what they are, and generally how to work with them as I'm not brand new to programming, but I would like a professionals opinion.
I'm currently writing a small multi threading program to launch parallel power shell sessions by spawning cmdlines for target machines in a csv, capture the output and write to a csv.
Should I put everything into one class and breakup the logical operations to methods within the class and string them together? Or should I make a Thread executor class, cmdline powershell class, a csv operations class, etc (My thought behind that was to allow code reuse, but that'll be kindove time consuming and in my mind impractical since i'd have to specify the datatypes and return types for new situations in the future).
Any help would be appreciated!
There is no "way" so to speak,
It's all your preference.
But just don't cram everything into one class.
Generally, you want to be as neat as possible.
In the future, you will thank yourself for using different classes.
If your project grows, and a bug is born, you don't want to be looking through one very long class, but instead simple broken up pieces.
Let's say you have these classes:
GPS,
Main,
Search
And someone reports a bug with the GPS not working.
Instead of looking everywhere saying, where did I put the GPS code,
it's right in front of your eyes!
I've went to everyones links and found the info very helpful. So far I've come up with this.
Make a package that contains classes that perform a specific set of tasks (also don't make utility kits that are very general). The package in my case would be called com.jt.threads.powershell or something.
Keep classes small and breakup the program by conceptual types. (ie. data reading and writing operations on a filesystem should be in one class with the focus on helping the package perform a certain task or range of tasks.)
Methods within classes should focus on getting, setting, changing the objects attributes or adding logic.
The program entry point should join it all together, except in the case of large applications, in which case an interface should be used (still learning about them).
With true OOP, i don't think it's a good idea to create code for reuse, unless it's supporting a range of very very very similar tasks (that way if I have to change something, it won't break other classes outside of the package).
Thank you all! I feel a lot better knowing that I'm on the right track. I was worried that by NOT making code reusable in a lot of situations that I was doing something wrong. I started programming in Python 6 months ago for my job, but I totally ignored classes and I want to have good programming habits and apply OOP as best I can going forward! Python is definitely convenient and a great starter language, but I wish I learnt Java first so I can get a solid grasp on OOP.
There is no “The way” to organize or group classes. Anything goes as long as it works as expected and you understand what you write.
As a Programmer you only need to,
1. Know and understand what you write.
2. Know and understand what other Programmer as written.
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 6 years ago.
Improve this question
Most of the people says that abstraction is hiding something and showing only functionality to the user. Can anyone explain me what are all the things you are hiding and what are all the things you are showing?? please don't explain with the examples of animal, engine, vehicle.
I think this is a case where a concrete example would help a lot.
HashMap has an internal structure for handling hash collisions, which is an implementation of a singly-linked list. Now, do you know how that internal structure works, what it's called, what its fields are called, etc? More importantly, do you care, so long as the HashMap "just works"?
If the answer to both of those is "no" — which is what it should be for anything other than curiosity/learning purposes — then those details have been hidden from you and exposed via the abstraction of Map's interface.
The result is a class that's easier for you to reason about (because you have less to learn), and easier for the library maintainers to maintain (because they don't need to worry about a change they make breaking your code, so long as they still abide by the interface).
Abstraction is an overloaded term.
Abstraction, in object oriented languages, basically means leaving away unnecessary details when modeling real world objects. You could also think of it as a simplifying process.
Abstraction, in computer science as a whole, also means hiding complexity by providing some sort of simpler interface. Your question seems to aim at "data abstraction" which means hiding the exact way data is represented with an abstraction layer. This could be e.g. the Number data type in databases. You just know it is a number, but not how it is stored on disk.
Abstraction sometimes is used equivalently to encapsulation, too.
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 7 years ago.
Improve this question
I am used to programming in static languages like Java, where changing the signature of a method will cause a compilation error for every line of code that calls the method I changed. This makes modifying large projects much more easy, because I can make a change, and then let the compiler tell me about all the places that I need to fix.
When dealing with a large project in a dynamic language like Python or Ruby, how do you make code changes, and still remain confident that you are not going to be surprised with a run-time error in production because of some scenario you forgot about?
I've seen my fair share of NullPointerExceptions and ArrayIndexOutOfBoundsExceptions in Java, so it's not like these things never happen in a static language, I would just think they happen a lot less.
Here are some ideas for providing some level of the protection that you are used to in Java:
As stated in a previous comment, you should definitely provide adequate unit and integration testing to prevent any issues during a refactor. Testing is even more important in a dynamic language than in a statically-typed language. You should check that values are properly passed and handled in each of your functions.
Use PyCharm and search for usages on a method prior to making the update. This is not full-proof, but does find a good amount of method usage to allow for an easier refactor.
Do a global find for the method name in your editor or search program of choice.
Provide exception handling in your functions for cases where the type is incorrect or a value is unset.
Handle args and kwargs passed into your function carefully. Perhaps provide an error or debug log if you receive an unexpected input.
Provide default values for undefined parameters to a function.
Here is an example of providing a default value for a parameter to ensure that it is defined and initialized to None (similar to null) in the function if it is not passed in with a value:
def my_function(my_parameter=None):
# Do something with my_parameter
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
In most cases, is it better to reverse an integer by extracting digits and putting them onto a string and use valueOf() to obtain the value; or to reverse the integer directly as an integer?
I'm inclined to say the former is better due to it being ubiquitous in the answer solutions of my book whenever they need to reverse an integer.
What is the drawback of using the other approach besides being a bit more time-consuming to write and read?
The first way (converting the integer to a string, reversing the string, then converting it back):
Is easier to understand.
Is likely to require less code.
Is likely to be slower.
The second way:
Is harder to understand.
Is likely to require more code.
Is likely to be faster (for one thing, it requires no memory accesses).
Of course, modern computers are so complex that the only way to tell whether something is faster is to actually measure it. I said the second way is likely to be faster, but that's just a guess.
Conclusion:
If you need the code to be written quickly, do the first one because it's easier.
If you need the code to run quickly, measure how long each one takes to run, and use whichever one is faster.
But, in practice, apart from programming exercises, you will never need to reverse an integer. So if you're asking which algorithm is used more often in the real world, the answer is neither.
The String method is easier to write and more readable by other programmers (and yourself in 2 weeks). The direct conversion method using modulo is faster and uses less memory but it's a bit harder to read and write.
Note that with JIT compilation and the improvements in JVM in general, the first method could be internally optimized to closely mimic the latter. And we all know that a general rule of programming is to avoid "premature optimization". Thus the more readable method using String is probably best.
What do you mean "better" and what do you mean "for general purpose"?
For the general purpose of explaining how to code in a textbook, a for loop and modulus is best.
For the general purpose of being clearly correct, I would suggest converting to string since this is a string operation, not an integer operation.
I haven't seen this operation in production anywhere so I can't attest what anyone actually does with it. I think this was a trick question - your "general purpose" may not exist after all.
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
Does anyone know how many clock cycles it takes for a variable assignment in C language on a x86 platform? It is generally considered to take less when compared to java, what is the reason behind it?
The difference between C and Java is not depending on the difference between the languages themselves, but rather on the difference of technology behind those two languages:
C is compiled in binary, which is the code that gets directly executed by the processor. By opposition, Java is (most generally) never compiled totally, it is instead pseudo-compiled into bytecode.
This bytecode is designed to be interpreted by a virtual machine (the JVM in the case of Java), allowing for a much easier portability: while you need to adapt your C code to make it portable (see NetBSD for an example), or make different versions of it for each target; you just need a different JVM to run the same java bytecode on a different target.
It is worth noting that Java follows the JIT model, allowing for optimizations that are normally impossible, since they rely on conditions only known at run-time.
Now, in the case of your question, the real things to compare are: for a given machine, how much cycles does it take for a value to get copied in memory (RAM, even though that some C compilers can use CPU registers to store variables used a lot in a short time-span, like loop counters for example) with the assembly instruction versus how much cycles does the JVM take to do the same task upon reading the Java bytecode instruction to do so.
And I would say that with a good JVM implementation, there would be no difference for the allocation itself, as far as I understand. Now, there are other criteria to consider: Java usually makes heavy usage of objects, that take a lot of place in RAM, due to their complex nature, and therefore, take also more time to allocate. Also, I believe that Java makes more checks to avoid common mistakes, such as accessing a non-initialized variable, and those cost time too.
But keep in mind that a badly coded C program can take much more time to execute than a very well coded Java program.
Only after you understand this statement:
"Exactly zero lines of C code have ever been executed by a computer. Also, exactly zero lines of Java code have ever been executed by a computer."
You will never understand any answer to your question.
There answer to your question is:
"An assignment, written in C, takes an unknown number of clock cycles to complete."