Deepak Amar

Greenhorn
+ Follow
since Sep 17, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Deepak Amar

So I was looking at the Java 8 features.

While I was looking at the examples of Function and BiFunction usage in ConcurrentHashMap, I came across below two methods:
   
And:

Why is Generics used the way it is used?
Somewhere we have "? super" and somewhere we have "? extends". On what basis these parameters are decided?
Why can't all be "? super", or all be "? extends"?

And to add to this, I do understand how generics work and why we have "? super" or "? extends". But just curious to know why is it implemented that ways?
4 years ago

Stephan van Hulst wrote:The reason is that all the use cases that you can't use an enum for, are really bad use cases in the first place.

You mentioned that you only want a single SessionFactory. Absolutely fine! Just create a single instance and pass it around. Keeping it in a static field and letting dependent classes perform service lookups makes your code very messy and buggy.

The singleton pattern is dangerous to use for entities and services. And for value types enum is often easier. Unless you want to use composition, but you can always ask: why do I want to limit the number of objects created in the first place?



Hello Stephan, if you see for example, Runtime class which is singleton but then they did not use enum for it? Its not easy to add all the functionality in enum, that's what I meant. But still enum is considered best.
4 years ago
Wherever I have read, it is mentioned that the best approach to ensure that singleton is achieved is by using enum.

But then what puzzles me is, enum can never be compared to a class, class has so many enriched features which an enum can never support. Examples:

1. Composition
2. Inheritance- it just allows to implement an interface

And there could be more.

Like for example SessionFactory in hibernate, you want to have a single instance of that per application, but then SessionFactory cannot be represented by just an enum.

And enum is not a full fledged class.



Can you guys tell me is that is correct? I mean enum cannot be used for the same in most of the scenarios, like for example I added SessionFactory in Hibernate.
4 years ago
I did notice a weird behaviour about the static and non-static inner exception classes.

For example the below code will not compile:

   

But the below code will compile:



Why is this behaving this way?

One thing is for sure that we cannot have Generic exceptions due to type erasure but then the above is not a Generic exception class but it is inside the Generic class. But if it is static it is allowed but if it is non-static it is not allowed?

Follow up question, is generic exception not allowed just because of the type erasure feature of Generic like it mentioned here:
https://www.mscharhag.com/java/java-exceptions-and-generic-types
Or there is some other reason to it?
4 years ago
Can someone explain the internal working of below logic?

I see this getting used in AtomicInteger as well ReentrantLock.

Snippet from Unsafe class:
   

Why do we do this?

Additionally there is a post which I tried to understand but that is not straight forward.
http://www.moserware.com/2008/09/how-do-locks-lock.html

There is no enough information available online as well

4 years ago

Kristina Hansen wrote:This might could help: https://stackoverflow.com/questions/12438567/java-bytecode-dup



I had seen that, but it is still confusing. It says after duplicating it pops off the first element and initializes it by calling the init method. Then it goes on to say that "the init method will not return the object to put back onto the stack, so the object must first be duplicated so as to keep it on the stack".

So that means we are just duplicating the reference to the object and not the object itself?  
Because if it was just the object then whatever init did would be of no use right as it is anyways not returning the object back?
4 years ago
Can someone explain why dup is used in bytecode?
why there is a need to duplicate?

Java Code



ByteCode




4 years ago
Campbell and Stephan, thank you so much for your replies those were really helpful.

Stephan, Specially the example with which you explained the logic.

Campbell, the older posts were also quite helpful.

Thank you so much, you guys saved my day..
4 years ago

Stephan van Hulst wrote:Like Campbell, I'm confused by what specifically you are referring to. I first though you were referring to the methods that accept a BiFunction or a BiConsumer.

If you are indeed referring to bitwise operators instead of binary operators, then it's because maps grow in powers of two, and bitwise operators are very natural for working with powers of two.



I just have a question regarding this "bitwise operators are very natural for working with powers of two". Why do we say that?
4 years ago
HI All,

Could be a very basic question.

But reading through the HashMap source code, I found a lot of Binary operators, could someone explain the purpose of that?

Things which are done using Binary operators, can't that be done without using binary operators?

Thanks in advance.
4 years ago
Hi All,

I was going through the source code of HashMap, but the binary operators confuses a lot.

I do understand the general purpose of below, fair distribution and bring hashCode within the bucket limit.

Can someone explain the comments here and what is the benefit of doing the way it is done right now?




It would be a really big help if someone could help me understand it.

Thanks in advance
4 years ago
The reason I did that was because the old question was added in a wrong FORUM and there was no way that I could delete it. So I had to create the new one and mark the old one as default. And now after you have merged the two it is looking messy. Can you please delete the duplicate from this post? I am not able to do that. It is not at all user friendly.
8 years ago
Guys have read a lot of posts but still haven't got my answer.

In which scenarios we should consider copy constructor and in which scenario we should consider cloning. And why would we choose a particular way. And why was cloning required as we already have something called copy constructor?

I know that there are some issue related to shallow cloning in Java. But still why do we have cloning?
8 years ago
Guys have read a lot of posts but still haven't got my answer.

In which scenarios we should consider copy constructor and in which scenario we should consider cloning. And why would we choose a particular way. And why was cloning required as we already have something called copy constructor?

I know that there are some issue related to shallow cloning in Java. But still why do we have cloning?
8 years ago

Campbell Ritchie wrote:I failed to get either of those links you posted to open. Please provide us with the correct URL.



I cannot post the actual URL as its confidential..
8 years ago