As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
I find the way Generics are implemented in Java to be way too complex and very ugly(syntax).
I think the concept of generics is alright, but implementation kinda sucks.
Was wondering which other language might have the best/simplest/cleanest generics implementation. .... or maybe an alternative to generics per say with the same purpose.
There are two options to simplify the type system in the fashion you ask for.
Type Inference and Dynamic Typing.
Type Inference
The idea behind type inference is to have the compiler to to figure out the type of your objects for you so your code can be simplified by omitting the type information. It has other advantages as well.
Some popular languages with type inference:
Haskell
ML
OCaml
Dynamic Typing
In a dynamically typed language, generics are not needed because you get it for free with the dynamic typing. If you want to understand more about how this simplifies the type system, study up on duck typing. Here's a short intro. to the rational behind duck typing.
Some popular languages with dynamic typing are:
Ruby
Python
Objectiv-C
Javascript
You're right that generics in Java was implemented very poorly. This was by design. Why a crappy generics implementation? When they were doing the designs for Java generics, there were compatibility issues with old code, and extensive changes were needed to existing VMs to implement it. Eventually they gave up and gave Java the deficient implementation is has today.
http://code.stephenmorley.org/articles/java-generics-type-erasure/
http://www.ibm.com/developerworks/java/library/j-jtp01255.html
Why do some claim that Java's implementation of generics is bad?
C++ and C# have a much better generics implementation. Check them out if you want a Java-like language and a similar generics implementation.
If you are looking for something more concise and elegant I would suggest you to look forward functional languages like OCaml or Haskell (maybe F# if you want an API similar to the Java one) that have the so called type reconstruction which is how the real generics thing should be implemented.. maybe it's a little bit more strict but you don't lose expressivity, you just gain in syntax.
In any case your question is quite silly, you don't choose a language according to just "generics", it's a long and complex tale which usually includes what are you going to develop. Every program has its story and its needs.
I think that the generics within Eiffel may be the cleanest.
The creator of Java Generics created the Scala language. "Generics" in Scala (Type Parameters) are much cleaner comparing to Java, as almost any other thing on it.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In class the prof said one reason he likes C better than Java is that C has a preprocesor, and in particular macros. Is there any benefit to macros over declaring afinal/const variable with the desired value? I know Java doesn't have global variables so would there ever be a situation where using a variable in substitution for macros would not work?
Using a macro system in C has been a long standing tradition that often leads to incomprehensible build chains, and very project specific build configurations. I would argue that the reason that macros exist in C is to overcome the shortcomings it has as a language for when you need things that macros provide (for example, you can use interfaces in Java, which solve the problem of needing to make a "generic" instance of something).
Still, there are times when having a macro system just seems to fit a problem easier. I do know of some shops that do this -- for example -- when they need to support multiple different versions of an API. Generally you can overcome this problem in Java by proper use of interfaces and a good abstraction layer in your design. However, there are macro systems for Java out there! You can use them, if you please. You might also note that, as far as macros go, C macros aren't really anything very special.
(In my opinion your teacher was somewhat off base...)
There's no reason you can't run your Java code through a macro preprocessor; it's just not done as part of the language.
With that said, most of the reasons macros are valuable in C are matters of the C language, and most people's use of them is extremely ugly and counterproductive, though they do have many good uses too.
Anyway, I would really question the expertise of someone who compares C and Java on the basis that one has macros and the other doesn't. In the big scheme of things, that seems to be one of the most trivial differences between the languages. Highlighting it suggests to me that the person does not understand the real differences between the languages; perhaps he likes C but doesn't know how to name any of the actual features C has over Java - things like storage duration and representation of types.
Macros in C are not just global constants. They can also define functions, like
#define SHOW_DEFINE(x) printf("%s=%s\n", #x, STR(x))
This can be quite helpful during debugging. It is unfortunatly also easy to hurt yourself quite badly with Macros.
#define MULTIPLY(X,Y) X*Y
works fine for, lets say MULTIPLY(1,2) but leads to problems if one uses MULTIPLY(1-1,2-2) = 1-2*2-2 = 1-4-2 instead of the expected 0*0.
Usually, with modern tools such functions are not longer necessary and with a good design and some abstraction and the interfaces that Java provides it is in general not necessary to have macros in Java. Java is designed, afaik, to make it as hard as possible to shoot yourself.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I often use the Stack data structure in both Java and C++. This is a standard data structure, very common in implementing many algorithms.
My question is (and the thing that drives me crazy) why does C++ use "top" as a function-name that returns the top-most element value without removing it, and Java uses "peek" as it's method name?
I know there is no standard for data structures, but hasn't computer science come far enough along that there should be a standard? Or am I just too much of a novice to know about a standard...
Do those of you that are professional programmers write your own data-structure libraries that adhere to a common interface across languages? That seems like the best thing to do, in my mind. I write code in C++, Java, Python, C, Perl, and PHP. I just don't see any other way but to write a custom interface for all of these languages. I like "peek", but is there any standard I should be aiming for?
Writing a custom interface just to make method names the same would be a colossal waste of time. What exactly would be the point? You wouldn't be able to easily copy-and-paste most code between the languages you've mentioned even with such a feature.
Personally, I don't like the name of the STL vector method push_back(). I would prefer if it were just called add(), for one thing it'd be less typing. It never occurred to me that I might change it, however. Doing so would just make my code less portable and less readable for others. Now, I suppose this could be done fairly easily with a pre-processor macro, but even that would be a waste of time in my mind.
No there can't be, won't be, and never will be a standard. Anyway, both names are valid, and if you ask me, top makes more sense. Also, as #mimicocotopus says, it's not like having the same method names would let you copy paste code from one language to another. Also, languages like C++ and Java are very distinct, and support different features. If a standard had to use the lowest common denominator, it couldn't take advantage of all of the features of the language it was implemented in.
Anyway, remember what happened last time we standardized something? Cross browser compatibility and porting C code. It gives me shudders just to think of it.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
When i came across the reason why Multiple Inheritance was not included in Java, the reasons given were to keep 'simplicity' and 'reduce complication'.
However working with Java environment coming from a C++ background, don't you think that Interface concept to support multi inheritance has complicated the matter rather than solving it? Does it lead to the inference that:
We must NOT use multiple inheritance in Java, and our code architecture should be designed accordingly?
Use concept of Interface for multiple inheritance, which i think is less favorable (atleast for me) compared to the st
You should read Bjarne Stroustrup's point of view about multiple inheritance:
Do we really need multiple inheritance?
Not really. We can do without
multiple inheritance by using workarounds, exactly as we can do
without single inheritance by using workarounds. We can even do
without classes by using workarounds. C is a proof of that contention.
However, every modern language with static type checking and
inheritance provides some form of multiple inheritance. In C++,
abstract classes often serve as interfaces and a class can have many
interfaces. Other languages - often deemed "not MI" - simply has a
separate name for their equivalent to a pure abstract class: an
interface. The reason languages provide inheritance (both single and
multiple) is that language-supported inheritance is typically superior
to workarounds (e.g. use of forwarding functions to sub-objects or
separately allocated objects) for ease of programming, for detecting
logical problems, for maintainability, and often for performance.
quoted from http://www2.research.att.com/~bs/bs_faq2.html#multiple
Multiple implementation inheritance and multiple interface inheritance are not the same beasts.
However, it would significantly complicate the GC and other language implementation if they were to add multiple implementation inheritance.
I think the choice of Java (and many other OO languages) designers was mainly motivated by the fragile base problem. It's true we don't need multiple inheritance, but for that's worth to note we don't need single either. Object Oriented programming it's about identities of entities. Inheritance can be seen as syntax sugar in this regard.
The interface concept in java is NOT meant for providing multiple inheritance feature.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
I know that is Collections.sort() method in Java but I think quicksort is worth to remember and try.
My work target is general Java: web, database access, integration, not game developer, scientific application or another one that depends on advanced algorithms.
Which algorithms should I learn to pass without stress Java developer interview?
Fizz Buzz
I usually don't care, if a developer knows the basic algorithms by heart. I do care, if he is capabale of understanding requirements and translating them in correct, testable and understandable pieces of code.
Ah, and I do care if he knows how to implement the most common design patterns. And he should know when and how to use collections, threads and - String#split - it's amazing how many "developers" don't know how to read and process a simple csv file.
Although I fully agree with Joachim comment, I would go for : collection selection. This is not an algorithm per se, but rather a good view of which collection is good for which purpose :
sorted content with constant lookup time ? TreeSet !
mapped data with memorization of insertion order ? LinkedHashMap !
using that, and some knowledge of design patterns behind collections, you will far too often reply to algorithms questions using the knuth answer (or the subtle variation : as long as Sun developpers implemented it correctly, I only have to choose wisely).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
What are good resource to read up on for implementing numerical algorithms in pure java?
I know nothing about the interactions of the JVM & GC, and would love to learn more.
I enjoyed Object Oriented Implementation of Numerical Methods by Didier Besset. To be honest I use C++ almost exclusively but found this book interesting and fun nonetheless. It doesn't always use the best algorithm for the job, however.
I would also point you towards Matrix Computations by Golub & Van Loan. Not about Java by any means, but a mandatory text for anybody working in this area.
Pick up a copy of Numerical Recipes in C++. NR doesn't always contain the best algorithms for the problems it tackles, it's a pedagogical text not a library of optimized code. But the explanations are generally good and the range of topics is wide. By picking up the C++ version you can learn some Java while you translate the code. Also pick up a good book on the basics of floating-point arithmetic and numerical analysis.
Numerics: http://math.nist.gov/javanumerics/
GC intro: http://www.ibm.com/developerworks/java/library/j-jtp11253/
Java Number Cruncher is a good start. I don't think it deals with GC issues, though. Anyway, floating point precision issues are quite likely more relevant in numerical algorithms, usually. The book does demonstrate that doubles can actually be inferior to floats in some cases.
Apache Commons Math has some nice linear algebra libraries, among other things.
Linear algebra is the basis for lots of serious numerical work (e.g., finite difference, finite element, and boundary element formulations for problems in solid and fluid mechanics and heat transfer). AFAIK, most of that work is still done by commercial and in-house packages written in FORTRAN. Java and even C++ and C were not considered performant enough for such things back when I did them for a living. I'd be surprised if that has changed.