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 Java 8, the Optional class is implemented as a final container class that defines isPresent depending on the value being null. That means that it is not possible to have an Optional(null), which although not commonly used, seems like a design flaw to me. In particular, this defeats the purpose of having an optional in the first place, because you can simply set a variable to null and do the usual if (x != null), without the overhead introduced by Optional. Furthermore, the Optional class has to check if the value == null for every single operation performed on it.
In Scala however, the Option trait is much more sophisticated: It has the Some subclass for existent values and the None subclass for non-existent values. This eliminates the need for null-checks inside the class and allows Some(null) values.
My question is why the Java designers chose not to follow this subclass principle as well.
Java has empty() and ofNullable(null). Other tool methods exist too. Scala uses subclassing, case classes where other languages (still) use other notions. Scala sees typing a bit more operational.
Java Optional is workable; come with code examples. Using a Stream for an Optional might be more in the character of Scala maybe, allowing fluent design with chaining calls in a bit more comfortable way.
It lives with null and is more a recommendation as you stated.
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 1 year ago.
Improve this question
In order to obtain the hash code of an object in Java, we use the hashCode() method.
To access (private) fields in Java, by standard, we are expected to have separate method with get or set as prefixes based on the kind of operation the method is performing. So why is this different for the hashCode method?
The method hashCode() has been introduced with the first Java version, i.e. officially version 1.0.
The getXXX() and setXXX() paradigm was established later as part of the JavaBeans standard. JavaBeans defines a simple yet very useful component model by deriving properties from pairs of getters/setters. JavaBeans was introduced with JDK 1.1.
So hashCode() predates the getter/setter paradigm.
In the end: conflicting paradigms.
Keep in mind that methods such as hashCode() are with java since day 0 more or less. And back then, there wasn't an established standard suggesting: use getters resp. an explicit "methods should be named doWhatever()".
And once released, renaming it to a more appropriate computeHashcode() or something alike would have broken all existing source code.
In other words: there is also a mindset of using very short method names, such as size() in the Collections interface.
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 5 years ago.
Improve this question
Java has an Optional type, which alleviates a lot of the pain null causes. Is there any valid use cases for null anymore? Why would one use null but not Optional?
One possible venue is perhaps performance. null probably is faster than an Optional.
Optional object's typical use case is for method's return types only.
Also, it is NOT a good practice to use Optional as method arguments or constructor arguments because we can't enforce non-null optional objects i.e., the Optional object itself can be null. In other words, even if you pass an Optional object as method parameter as we can't guarantee that the Optional object itself is NOT null, you might still end up with a null check. I suggest you can look here on a detailed discussion on this subject.
The other point is that Java's Optional is NOT Serializable, so you can't use Optional on the variables/fields whichever require Serialization. You can refer more on this here.
So, the important point is that we still need null references wherever Optional does not make sense.
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 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
In many places Java utilizes the approach to have both, for example, Collection and Collections class.
Collection is interface, it defined some methods.
Collections class also provide some method.
Why didn't they choose to place all methods inside the interface?
because the names are too puzzled word. Collection is like Collections.
I know the historical reason. like interrupt() and interrupted() , because java must fit to old version, the methods' names are likely, make developer difficult to write and read.
But the collection framwork must have reason in this way.
For starters, an interface cannot have static methods. Note: until Java 8.
Arguably, some of the static methods of Collections should have been made instance methods of Collection, but that would create a lot of "clutter". Plus, extra work for implementations not derived from AbstractCollection etc.
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 am well aware that the void keyword is used to indicate in a method declaration that the method will return no value. My question is not about - to use an analogy - how to use the car, but rather about what's underneath the hood. I would like to know why void actually needs to be specified, i.e. does Java reserve an internal variable of some sort to keep track of return types? Will setting a method to void make it so that it doesn't have to do this?
Another way to put it is to ask why, exactly, can't Java omit the void keyword and assume that if there is no returned type than the method is void? I think that there is something to do with how Java might "prepare" to handle the return type, and possibly something to do with optimization... please use full detail.
Its a java language Specification
void used with only methods declaration
if we omit void then its treated as constructor.
SEE THIS
my answer is a bit of a guess. But I'd say its to protect the developer against just forgetting to declare the return type. Sure the compiler can even by default be programmed to render undeclared return types void. But that would be at cost to the aid for the developer.
Furthermore, IDEs would have to anticipate on the same.