Immutable Objects in Java

  Рет қаралды 28,027

Marcus

Marcus

Күн бұрын

Пікірлер: 48
@fallenIights
@fallenIights 4 жыл бұрын
Watched all 30 of your FREE Java for Beginner videos. Great work, Marcus! 💯
@MarcusBiel
@MarcusBiel 4 жыл бұрын
Thanks. Currently I am working on a SaaS... One this is running, I want to do more videos. Did you also read the free book on my website? It has additional and updated info.
@sermojohn
@sermojohn 8 жыл бұрын
Thanks, great video! My question is: After you have created a lot of immutable instances of the same bank account, how do you know which is the actual balance, taking into account concurrency issues?
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Thanks for asking this again. For some weird reasons I hadn't see the earlier comment from Ioannis. I will answer right now!
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Well, in my course I have not talked about concurrency, which is why I haven't talked about concurrency related issues in this video either. Anyway - very good question. Everyone just states "Immutables can be shared freely without any synchronization" - but what this means is "without any lock or synchronizhed keyword" - from a business point of view, you still have to organize (synchronize) the immutable objects, that is correct. For this, you could use the Compare and Swap (CAS) algorithm. That can be quite easily done using an AtomicRefererence - docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html. So the AtomicReference instance acts as a "pointer" to the actual balance. Hope this helps!
@anoohemandas135
@anoohemandas135 5 жыл бұрын
Marcus please include working Immutable bank examples at the end of each topic so that learning by example is easier
@dilneicunha
@dilneicunha 8 жыл бұрын
Hi Marcus, great class! I always use immutable objects especially in my DTO's, like writing my views with DTO's, as I have no set methods the actual data value that enters the inputs is the real value that comes in the controllers. This seems perfect because we prevent the data being manipulated.
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Great that you liked it. As a quick recommendation, avoid the use of DTOs - they are just a hack, a helper mechanism, not very domain driven.
@dilneicunha
@dilneicunha 8 жыл бұрын
Marcus, why avoid DTO's? if they are a pattern, and using them I can have my Immutable objects, always used DTO's web projects generally action-based projects, I use them to make my views because I can have a real separation of responsibilities between the view / controller and the layer of business. Could you explain me better?
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Well there are a lot of patterns that today are seen as an anti-pattern, for example also the singleton. With DTO's the reason you use them is usually because your entities become to complex to be transferred to the UI. But why are they too complex in the first place? Focusing on designing SIMPLE classes! So in your case, you have complex classes and you map them to simple DTOs to improve performance. But now you need a mapping class, and the mapping logic, which again costs performance (and code). Keep things simple in the first place, and you won't need this additional code. My recommendation is you read about "Domain Driven Design" - it might be a bit tough to understand at first, but it is REALLY worth it! www.amazon.com/gp/product/0134434420/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134434420&linkCode=as2&tag=marcusbiel-20&linkId=3eeca79bf882f7fc556ea9c1468a3c5f
@dilneicunha
@dilneicunha 8 жыл бұрын
Thanks for the explanation, and the book link, I'll read, sure, I agree with everything you said, but in parts :) For action-based applications, I still like to use DTO's even sometimes become a little more complex the application, in my case I have complex screens, usually I have other patterns associated with software architecture, such as assemblers I use to convert DTO's entities. gist.github.com/dilnei/39077f6e133b412a72f7 gist.github.com/dilnei/175c1c9ee4def6d0b454 But it is difficult to expose an entire architecture here for you, and talk about the reasons that led me to use this model, but expose objects (entities) directly on the view does not seem good, although I use exactly this way when the application faces (JSF ), I do not see how a good software design, because I lose immutability, due to having set methods for each entity's declared in the controller view.
@AS-lu7wl
@AS-lu7wl 2 жыл бұрын
HI Marcus, I finished these videos- What do I do next?
@St.Pa-al
@St.Pa-al 4 жыл бұрын
Thanks Marcus.
@kaus05
@kaus05 3 жыл бұрын
Why was the balance field made final
@otaldepeewee3596
@otaldepeewee3596 Жыл бұрын
I have two challenges in life, becoming fluent in English and learning Java. I currently do this at the same time.
@mojtabavahdati7648
@mojtabavahdati7648 3 жыл бұрын
Thanks, this is really helpful Mr.Biel. Does the word "stable" mean "resistant against changing" and thus "resistant against getting into a wrong state" in your video?
@ahmeturun3153
@ahmeturun3153 8 жыл бұрын
Nice explanation 👍 visuals are great
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Thanks. Great that you like it :)
@anoohemandas135
@anoohemandas135 5 жыл бұрын
If you could post working examples of Immutable it would be great to understand.
@harishchowdarysure8858
@harishchowdarysure8858 7 жыл бұрын
I have one question we are using hp fortify tool it was showing cross browser scripting vat server side please find me one solution.
@subramanianchenniappan4059
@subramanianchenniappan4059 5 жыл бұрын
Thanks. I am a java developer
@MezPahlan
@MezPahlan 8 жыл бұрын
Great explanation! Really enjoyed that. Is the craftsman drawing of you?
@MarcusBiel
@MarcusBiel 8 жыл бұрын
+Mez Pahlan sure, that is me. Compare the face with my photo! ☺️
@vasilchervenkov4633
@vasilchervenkov4633 6 жыл бұрын
@@MarcusBiel haha
@mannysingh6618
@mannysingh6618 6 жыл бұрын
Have to be careful with immutables as if not used correctly, memory can be affected. It really depends on the type of system that you're designing..
@MarcusBiel
@MarcusBiel 6 жыл бұрын
Actually I explained this in detail in the video: kzbin.info/www/bejne/Y4Xcla2fns9qqZYm53s and here: kzbin.info/www/bejne/Y4Xcla2fns9qqZYm13s
@mannysingh6618
@mannysingh6618 6 жыл бұрын
Marcus Biel No worries, not being critical, like your videos, love to Germany 👍
@MarcusBiel
@MarcusBiel 6 жыл бұрын
All good. Just wanted to clarify that this is part of the video already :)
@tech_excellence
@tech_excellence 7 жыл бұрын
Hello Marcus, is this the same as The creational pattern --> immutable objects pattern ?
@MarcusBiel
@MarcusBiel 7 жыл бұрын
+Efim Graur there is no creational pattern called immutable object. Compare: sourcemaking.com/design_patterns/creational_patterns You might also want to check out my latest video where I speak about immutable classes at TDC conference. Basically the same, just slightly updated and live spoken.
@tech_excellence
@tech_excellence 7 жыл бұрын
Hello Marcus, thank you for your response, I asked this silly question because I read chapter 2 of www.amazon.com/OCP-Certified-Professional-Programmer-1Z0-809/dp/1119067901 and there it says that: ---> The next creational pattern we will discuss is the immutable objects pattern. Problem How do we create read-only objects that can be shared and used by multiple classes? Motivation Sometimes we want to create simple objects that can be shared across multiple classes, but for security reasons we don’t want their value to be modified. We could copy the object before sending it to another method ..............
@MarcusBiel
@MarcusBiel 7 жыл бұрын
I am not sure if I really understand your question here. I assume you mean "How can we use immutable objects" concurrently? Well, immutable objects can be shared freely, without any kind of "technical synchronization" - but you still have to manage (synchronize) the global state between the different threads, yes. And yes, that's not easy, that's a rather advanced topic. In short, for optimal performance, you would use a lock-free algorithm in most cases. One such algorithm is the CAS - compare and swap algorithm. You have one object that holds the "global state" - and all other objects/thread will "optimistically" assume that no other thread has updated the state in between, and perform their own state change on the object. If however, they realize that the state was changed in between - they will rollback their change, retrieve the newest global state, and start over. This is a very rough quick explanation, but I hope this helps to understand the concept. This lockfree algorithm works perfect in low to medium contention - when there is low to medium "traffic" going on - for high contention, a locked algorithm where each thread has to wait for its turn (in a way like a singled threaded queue), is actually faster - compare that to a police man handling the traffic vs. traffic lights. The police man is best when there is low to medium traffic, the traffic light is best for high traffic, as its smart algorithms can handle the traffic much better - but might force cars to wait without any reason at nights. Anyway, back to Java - Java offers a number of "Atomic" classes to implement the CAS algorithm - check "AtomicInteger" for instance. Hope my eplanation helped more than it may have confused you:)
@parmodprajapati
@parmodprajapati 5 жыл бұрын
when we use immutable class with another example Please
@AlexHeavens
@AlexHeavens 8 жыл бұрын
Thanks for the video :) This works great for simple classes, but do you have thoughts on how to construct an abstract class that is (if at least only by trust) immutable? One of the really big benefits of Java is it's extensibility, which a common abstract class aides. However, without trusting a factory passed to this common abstract class to always (scouts' honour) return a brand new object, I haven't thought how to achieve this yet...
@MarcusBiel
@MarcusBiel 8 жыл бұрын
+Alexander Heavens 1. An abstract class is NEVER immutable. 2. Focus on designing simple classes. Not only for immutables, but generally. Complex is EVIL. We never want to have a complex class. 3. Inheritance is evil. This I haven't said in this video, but check out my video about inheritance. 4. Generally you can use any mutable class as IF it WAS immutable (just create a deep copy for every change).
@tulsi6317
@tulsi6317 8 жыл бұрын
I faced this question in interview panel.... Thanks
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Your welcome. Hope you were able to answer it BEFORE you watch this video also :)
@tulsi6317
@tulsi6317 8 жыл бұрын
MrEternalFool in Ernst & Young Pvt. Ltd company
@tulsi6317
@tulsi6317 8 жыл бұрын
Marcus Biel noo 😑😑
@MarcusBiel
@MarcusBiel 8 жыл бұрын
Nooo what?
@tulsi6317
@tulsi6317 8 жыл бұрын
That time, I am not aware of this question. I told we have to declare a class as final. 😃
@suar_pilla
@suar_pilla 7 ай бұрын
Gem!
@harishchowdarysure8858
@harishchowdarysure8858 7 жыл бұрын
hi sir how r u?
@ThatThing1675
@ThatThing1675 7 жыл бұрын
You speak very slowly. It drags the video out unnecessary and thus makes the video more boring and uninteresting. You should try increasing the tempo
@kiaramutschler9246
@kiaramutschler9246 6 жыл бұрын
When learning something new I would say most people actually like it slow! If you don't you can always speed the video up. I watched it at 1.25 speed :)
@MarcusBiel
@MarcusBiel 6 жыл бұрын
Exactly, thanks Kiara. Just speed up the video, and the problem should be solved for you.
@jvsnyc
@jvsnyc 4 жыл бұрын
@@MarcusBiel Agreed that it is asymmetrical. If one does a mellow, paced video, speeding it up by watching at 1.25, 1.5, 1.75 or even 2x works fine. If one is too frenetic and rushing, not only is it more stressful, and more likely you will make mistakes and need to edit, but it really does NOT work to watch at 0.5 or 0.25 speed for those who need it slower. So this is an example of something superficially looking symmetrical that is not. Slow and steady wins the race.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1,1 МЛН
Shallow vs Deep Copy in Java
8:22
Marcus
Рет қаралды 88 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 62 МЛН
Memory Fundamentals - part 1 of Java Memory Management
18:31
Virtual Pair Programmers
Рет қаралды 146 М.
Immutable Object and Types of Immutable Objects | Java Tutorial
14:22
Naresh i Technologies
Рет қаралды 43 М.
How to create an Immutable Class in Java from Scratch?
14:04
Riddhi Dutta
Рет қаралды 6 М.
Java Comparable interface with funny animation
10:37
Marcus
Рет қаралды 47 М.
Is Functional Programming a Good Idea?
10:28
Sammy Engineering
Рет қаралды 19 М.
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 623 М.
Java for Beginners 14 - public static void main
21:00
Marcus
Рет қаралды 4,4 М.
Java Tutorials : Create Immutable class in Java
7:21
Coding Simplified
Рет қаралды 39 М.
Garbage collection in Java, with Animation and discussion of G1 GC
44:20
Ranjith ramachandran
Рет қаралды 492 М.
Dr Gabor Mate answers question about October 7th during conference
12:53
Middle East Eye
Рет қаралды 570 М.