Java serialization ๐Ÿฅฃ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going everybody it's bro here hope you're doing well and in this video i'm going to teach you guys the basics of serialization in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running all right everybody let's talk about serialization so what this is is that it's the process of converting an object into a byte stream so the benefit of this is that an object persists it saves the state of the object after the program exits and the reverse process of serialization is deserialization that's kind of a tongue twister to say so deserialization is the reverse process of converting a byte stream back into an object so think of serialization as you're saving a file with the object's information and deserialization is as if you're loading a saved file so serialization is taking an object and saving it to a byte stream deserialization is taking a byte stream and loading it back into an object so this byte stream can be saved as a file or sent over a network and it can even be sent to a different machine so this byte stream can be saved as a file and it usually ends with a dot ser file extension which is platform independent so this is some very basic info on serialization let's do some practice on this so let's create a new project file new java project i will call this serializer and finish don't create go to your project folder create a new class file new class name this whatever you want i'll name this main check public static void main and then let's create another class maybe a class based on users so file new class and i will call this user so what i would like to do is to save the state of a user that we create so what should all users have let's just have maybe two variables a name and a password let's create a method too uh but this has to be uppercase there all right so let's create a method as well public void we'll call this say hello all this will do is system.out.printline hello name then let's go back to our main class and create an instance of this class so user we'll call this user equals new user and then let's assign these variables so we need a name and a password so user dot name and these are public so we can actually access these from our main class so user name equals bro and then user dot password equals i heart pizza and then at the end let's just call the say hello method so user dot say hello all right simple enough we have an instance of the user class called user so now i would like to save the state of this object so i've listed the steps to serialize an object so step one your object class should implement the serializable interface so let's go to our user class and we're going to implement serializable step two add this import java.io.serializable and we can simply just hover over this and add this import so i'm also going to add this to our main class because we'll probably need it here as well eventually so step three is that we need to add this line of code so file output stream we need a name for this file outworks equals new file output stream and then the file name or the file path so let's add that i'm actually going to get rid of this say hello method because we won't really need it so step three is right here we're going to add file out put stream file out equals new file output stream and then let's add a name uh or a file path here so i will name this file maybe user info dot s-e-r [Music] so we'll also need to add this import actually i'll just import everything related to java.io so then we have our bases covered all right so we will eventually need to surround this with a try and catch block or add a throws declaration but we'll do that later so step four add object output stream we'll name this out equals new object output stream and the name of our file output stream so object out put stream we'll name this out equals new object output stream then within parentheses we're going to place our file output stream instance within the parentheses all right so then we're going to write out dot write object and then the object name so out dot write object and our object name is user and then lastly we're going to close everything that we've opened so we're going to take out dot close as well as file out dot close so most of this is underlined red so we either need to surround everything with a try and catch block or add throws declaration but just for simplicity i'm going to add throws declaration just to keep things kind of simple all right and then let's display a message at the end so we'll say something such as object info saved and then let's run this so object info saved so if you go to your project folder it really depends on where you place the file path so this is going to end up within my project folder you could place the file path for your desktop or something too if you want so i'm going to refresh my project folder and here is that file user userinfo.ser let's open this and take a look at it so this is all java byte code so this file saves the state of this object you can see here that it says i love pizza and a few other things that might be recognizable but it's all in byte code though so we can actually send this byte stream over a network or save it as a file and send it to a different machine so what we're going to do in this project is that we're going to create another project another project folder called deserialize and we're going to open this file and deserialize it and turn it back into an object so let's do that so i'm going to create a new project folder so new java project and we'll call this d serializer finish don't create then let's go to this project folder and let's create another main class so name main or whatever else you want to call this check public static void main click finish and then we're going to create an identical copy of this user class within our serializer folder so i'm going to copy this and then let's go back to our d serializer project folder and i'm going to file new class and i will call this user as well so it matches and then i'm going to copy everything within the user class from the serializer project folder so these should match then which they do okay so i'm going to close out of everything related to the serializer project folder and then we are now within our d serializer project so here's the steps to deserialize a byte stream back into an object it's really the reverse process of serialization but there's one extra step though so step one is that we're going to declare our object but not instantiate it so we're going to say user user equals and instead of saying something such as new user this creates a new user so we're instead just going to say null for now alright so step two your class should implement the serializable interface and we just copied this over from our serializer project folder so make sure you have implements serializable then make sure you have this import but i'm also going to import this here as well for our main class and i'll just import everything related to java.io all right step four we need to add this line of code so when we serialized our object we had file output stream but this time it's file input stream because we're importing something so file input stream file in equals new file input stream and then we need the file name if it's local or the file path so let's actually take a look to see where this is located so this is within my serializer project folder i'm just going to look at the properties of it and this is the location so i'm just going to copy this file path and then paste it within the parenthesis and then i need to surround this with a set of double quotes and then all of these backslashes need to be uh double because that's the escape character step five object input stream in equals new object input stream and then the file in instance so let's add that object input stream we'll call this in equals new object input i really cannot spell today i'm sorry new object input stream file in uh okay so that should be good then object name equals in dot read object and then we're going to cast this as the data type of our class so object name uh that is actually user so make sure it's whatever this object name is so user equals then we're going to cast this as our user data type in dot read object and then we just need to close everything so in dot close file n dot close then we need to surround everything with either a try and catch block or add throws declaration then let's display some of the values from our user object that we're going to deserialize so we'll system that out.printline the user's name as well as the password user dot password and then let's also call the user dot say hello method and then let's run this so remember for this class i did not actually assign any of these values we're actually going to be converting that byte stream back into an object and that's how we get these values so let's run this boom there we go bro is the name password is i heart pizza and it calls the hello method so now if we went back to our serializer class our main class where we assign these values and we were to actually change these and run the program again so let's say we're going to update the name to grow code and the password to something secure such as password123 compile and run this so this will overwrite our ser file we go back to our d serializer class run this and we have some new values for this object so when you serialize it saves the state of the object as a byte stream and deserialize is to take that byte stream and rebuild it as an object next here's some important notes related to serialization that we should be aware of so number one is that any children classes of a parent class that implements the serializable interface will do so as well number two any static fields are not serialized that's because they belong to the class itself and not any one individual object and when we use serialization we're really just taking the values of that object and we're saving them we're having them persist so we can load it later so any static field is really the property of the class and not any one object so number three the classes definition is not recorded when we deserialize so we need to cast it as the specific data type of that object so you can see that in this line so we're taking our user and we need to cast it as of this data type when we read this object and if i were to remove this cast it will cause a problem and it says type mismatch cannot convert from object to user so that's why we cast in this line of code back into our data type of our object so number four is that any fields declared as transient aren't serialized they're ignored so let's actually practice this so let's go back to our serializer class the user class for this and we're going to use this transient keyword for the password let's say that we don't want to send the password over so we're going to type in transient and then let's go back to the deserializer user class and we're going to make sure this is transient as well because we want to make sure that these two classes match exactly so let's take the serializer main class run this so object info saved let's go back to the main class for our d serializer run this you can see here that we have the name but we have no for the password that's because we marked this property this value as transient so this value is ignored anything that has the transient keyword is not serialized it's ignored number five so this one's kind of a big one so there's this value called the serial version uid and it's a unique version id for a class that is serializable so let's dive more into this so here's what i found on the serial version uid it's a unique id that functions much like a version number and it verifies that the sender and the receiver of a serialized object have loaded classes for that object that match exactly and it ensures that the object will be compatible between machines it ensures that these two classes match exactly it's like a secret code it functions much like a hash where it's calculated based on the classes properties members and a few other things we can actually take a look to see what this serial version uid value is going to be so here i am within the main class of my serializer project folder so we can actually declare and assign this serial version uid and it's of the long data type a long is really just a 64-bit integer so it can hold a very very large number so it's of the long data type serial version i misspelled that serial version u i d equals and these are the steps to actually retrieve what the current version is going to be so we're going to type object stream class dot look up and then within the parenthesis we're going to type user dot get class and then at the end of this dot get serial version uid and then we can simply just display this let's take a look to see what this is going to be so this long number here is our serial version uid it is actually calculated for us based on certain aspects of this class much like a hash so this example just so happens to begin with negative 764 and i'm actually going to copy this and paste this within the main class of our d serializer class and then we can take a look at what this number is going to be so since these two user classes match for our serializer and d serializer these both have the same exact number for the serial version uid so right now i'm actually going to go to the user class of our serializer class and let's just change one aspect of this class let's say that we're going to rename say hello as instead greeting and then we're going to run and compile this now for our serial version uid a completely different number is calculated so now this is technically a different version of our user class compared to i our uh d serializer so what happens if we were to actually attempt to deserialize this new byte stream that has a different serial version uid so now we get this thing called a invalid class exception and this is what happens when you attempt to deserialize a byte stream that has a different serial version uid so you need to make sure that these serial version uids in fact match and you can actually explicitly state what you want these numbers to be i'm going to head back to the user class of our serializer class and i'm going to change this back to say hello instead of greeting so if you were to take a look at the class name there's actually a warning and it says the serializable class user does not declare a static final serial version uid field of type long so we can actually and it's actually recommended to do this add a default or a generated serial version id so let's add a default one and we can actually assign this to something so let's say that we're working on version one of this class and then if we want to update this to version two we can just simply change this number so i'm going to assign this user a serial version uid of one and then let's run this so now when it prints the serial version uid this is now one well that's the basics of serialization if you'd like a copy of all this code i'll post all of this in the comments down below but yeah that's the basics of serialization in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 12,485
Rating: undefined out of 5
Keywords: Java serialization, Java deserialization, Java serialization and deserialization, Java serialization example, Java serialVersionUID, Java transient, Object, Java (Programming Language), Serialization (File Format Genre), Java (Software), Interface, Software (Industry), Management, Technology, Marker
Id: DfbFTVNfkeI
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Tue Jul 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.