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
Why does Java not support a nice and readable direct print-out for arrays?
I do know that Arrays.toString can be used to get that, and I also know what the hashcode output means (as explained in Why does the toString method in java not seem to work for an array).
But I am asking myself why the developers did not choose to make arrays directly print the readable representation.
Is it technically impossible? Would it impact performance?
arrays in java are a special case. There is no java source code for an array class and arrays are implemented in the JVM directly.
Given that, arrays don't have any methods themselves and they do not override any of the default Object methods (Yeah they're treated as objects but they're handled with special bytecodes).
As for why the decided to do that it probably has to do with allowing more flexibility with creating arrays of different types and dimensions, maybe processing speed as well.
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 6 years ago.
Improve this question
I have researched a little bit about the Object class but don't have an explicit answer to my questions (mostly documentations on the class members).
What is the benefit of having an "object" class at the root of the class hierarchy and basically why does a class has such an Object?
My guess is, because java is a strongly object oriented programming language and having an "object" at the root would be ideal to this concept. Doesn't coupling increase every time we inherit further from the root?
Well the benefit is, that everything (except primitives) are an Object. So there are certain things you can do with every Object, like synchronizing on it, or comparing two of them for equality or converting one to a String.
Of course this could just work by some kind of build in language feature. But in OO there is already a feature for that: inheritance, so it makes the language simpler by using this concept.
Of course one can have lengthy discussions, about each and every method of Object, if it was a good idea to include it.
So that all objects can inherit the basic methods from the main Objects class and you have the option to override them. Ex. toString();
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
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in 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
hi what is the actual meaning of utility function in java?
The term utility function was used by Joshua Bloch in the book Effective Java to describe the methods on classes such as Arrays, Objects and Math.
The approach predates Java, and is defined by Wikipedia - Utility Class as a set of methods that perform common, often reused functions. I would go on to also point out that the functions tend to require no Object level state, that is they tend to be global functions. And that is why, in Java they tend to become implemented as static methods on a container class. As that way there is no need to instantiate the class first, and it implies through convention that the method should not have any side effects. Of course counter examples of this probably exist, but not to my knowledge within the Java Core libraries.
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
Is there a reason for some wrapper classes to have the same name as primitives with different case (i.e. boolean/Boolean), and some not (i.e. int/Integer)?
I don't want opinions, but knowing if the cause is based on a relevant design decision for what seems to be an inconsistency.
It's likely because Java was created to attract C/C++ developers.
int was from C++, which took it from C.
boolean as opposed to pre-C99 typedef'd bool was first used in ALGOL 60. C++ has a separate data type called bool. I don't think ALGOL 60 actually had the name boolean however. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century.
As far as why the autoboxed Class name vs the primitive name, well the primitive name already existed like I described. Since you would likely have a confusing time telling the different between int and Int in code, it was made more explicit. In Java, by convention, class names are in CamelCase. Thus, Integer is spelled out and capitalized.
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
Some programming languages such as python, Java and C++11 have hash tables (although sometimes under different names with extended functionality) as part of their standard library. I would like to understand from a high level algorithmic point of view what has been implemented. Specifically:
What function of the keys is used to give the location to place the data (i.e. what is the hash function that is used)?
Which algorithms do they use for resolving collisions? As an example, do any of them use simple chaining?
Is there any use of randomness to pick the hash functions?
For Java,
How are the hash functions themselves computed?
They are implemented by the class itself with int hashCode()
Which algorithms do they use for resolving collisions? As an example, do any of them use simple chaining?
Typically simple chaining. Java 8 will support trees for collisions of String.
Is there any use of randomness to pick the hash functions?
No, except for String elements/keys to avoid DOS attacks.