Java serialization 🥣

preview_player
Показать описание
Java serialization tutorial for beginners

#Java #serialization #tutorial

00:19 serialize
08:47 deserialize
13:40 advanced stuff

Coding boot camps hate him! See how he can teach you to code with this one simple trick...

Bro Code is the self-proclaimed #1 tutorial series on coding in various programming languages and other how-to videos in the known universe.
Рекомендации по теме
Комментарии
Автор

Java Serialization

import java.io.*;

public class SerializeDemo {

public static void main(String [] args) {

//Serialization = The process of converting an object into a byte stream.
(saves the state) the object after program exits
byte stream can be saved as a file or sent over a network
stream can be saved as a file (.ser) which is platform independent
of this as if you're saving a file with the object's information)

to Serialize

Your class should implement Serializable interface
add import java.io.Serializable;
FileOutputStream fileOut = new FileOutputStream(file path)
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(objectName)
out.close(); fileOut.close();


//Deserialization = The reverse process of converting a byte stream into an object.
of this as if you're loading a saved file)

to Deserialize

Your class should implement Serializable interface
add import java.io.Serializable;
FileInputStream fileIn = new FileInputStream(file path);
ObjectInputStream in = new ObjectInputStream(fileIn);
objectNam = (Class) in.readObject();
in.close(); fileIn.close();


// important notes 1. children classes of a parent class that implements Serializable will do so as well
static fields are not serialized (they belong to the class, not an individual object)
Fields declared as "transient" aren't serialized, they're ignored
the class's definition ("class file") itself is not recorded, cast it as the object type
serialVersionUID is a unique version ID

//SerialVersionUID = serialVersionUID is a unique ID that functions like a version #
that the sender and receiver of a serialized object,
loaded classes for that object that are compatible
object will be compatible between machines
must match. otherwise this will cause a InvalidClassException
SerialVersionUID will be calculated based on class properties, members, etc.
serializable class can declare its own serialVersionUID explicitly (recommended)

User user = new User();

user.name = "Bro";
user.password = "I<3Pizza";

try {
FileOutputStream fileOut = new
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(user);

out.close();
fileOut.close();
System.out.println("Object saved!");
} catch (IOException i) {
i.printStackTrace();
}

long serialVersionUID =
"+serialVersionUID);
}
}

import java.io.Serializable;

public class User implements Serializable {

private static final long serialVersionUID = 123;

String name;
transient String password; //transient values are ignored

public void hello() {
System.out.println("Welcome "+name);
}
}

import java.io.*;
public class Deserialize {

public static void main(String [] args) {

User user = null;

FileInputStream fileIn;
try {
fileIn = new
ObjectInputStream in = new ObjectInputStream(fileIn);
user = (User) in.readObject();
in.close();
fileIn.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

long serialVersionUID =
"+serialVersionUID);

System.out.println("name: " + user.name);
System.out.println("password: " + user.password);

user.hello();

}
}

BroCodez
Автор

By far, Bro Code has the most succinct, and clearest explanations of Java on YouTube! No more than needed, no less than required. Good job!

exxzxxe
Автор

I have a Software Engineer interview in 2 days, and I never knew what Serializing and Deserializing actually meant in Java, until now. THANKS A TON Bro!

haramritk
Автор

such an amazing tutorial! I was having some trouble working with SQL databases and had to resort to serialization for my cs project, saved my life!

lucyl
Автор

This is the only channel on YouTube I literally feel I must comment on every time I watch, else I'm a bad guy XD
Your videos are great ... Seriously !!

preraksemwal
Автор

Guys, make sure your package file need to be the same in both the serialization folder and deserialization, then you won't get ClassNotFoundExpection for this. And bro you your video is fantastic. Love from China

chuanxinjin
Автор

You explain it better than my teacher bro. Thank you.

renecabuhan
Автор

Man, you're such a life saver. God bless you.

HipoteseNula_
Автор

Hey bro, thanks a lot for your amazing videos!
I just want to add another detail: if a class has fields that are other custom-made classes, then these classes ALSO have to implement Serializable

mpelalidiko
Автор

Your videos are very very helpful but its been a while that you did not post anything about Java, your community is waiting and thank you for that

fanaha
Автор

Clear how-to and break down, still technical but not boring! Thanks Bro Code!

ducare
Автор

Great tutorial series! I'm enjoying it

raderho
Автор

Absolute the best serialization tutorial video I have ever watched! Thank you

fantasy
Автор

Came back to rewatch the video and brush up on that topic. Thanks, Bro!

Snowmanver
Автор

Best explanation I ever had for this topic! Thank you! Can't believe this is on youtube!

danielmilewski
Автор

wanted to learn this for a save function, tysm bro <3

bitcoin
Автор

Legend. Solo carrying My Butt through advanced programming <3

alfredhitch
Автор

You're a champ bro! Tried to create a savefile through parsing the objects variables and then reread them and create through it the new objects. This is way easier. Thanks!

hansmuller
Автор

Bro, I need a teacher like him in my classes

ikersantana
Автор

Please, please, please make a video on ML or Neural net, please teach how to make a very simple neural net in *JAVA*
Even if you suck at those ML things, please figure it out, and make a tutorial on it,

noah