r/javahelp • • Feb 07 '21

Unsolved Serialisation Java8

Yesterday i took an interview where i was asked "why serialisation is said to b lazy" ?

I certainly could not answer this as i have never ever come across anything where the serialisation is considered lazy. After the interview i did a quick google and yet no ans.

Can anyone please help know the right ans for this with detailed explanation

java8

22 Upvotes

21 comments sorted by

β€’

u/AutoModerator Feb 07 '21

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

22

u/Robyt3 Feb 07 '21

Using Serializable is lazy, because it's a quick and dirty solution with many drawbacks.

https://github.com/HugoMatilla/Effective-JAVA-Summary#74-implement-serializable-judiciously

5

u/Jyotikb25 Feb 07 '21

thanks alot for answering , also the word lazy in the context ,what exactly is its significance? does it mean "not long term solution" or something else

11

u/marvk Feb 07 '21

It has a lot of drawbacks but is quick to implement. Doing it is lazy. Using a different method of serialization like serializing to json requires more effort, but has more advantages.

4

u/Jyotikb25 Feb 07 '21

Okay, Got it. Thanks 😊 this was helpful.

4

u/HecknChonker Extreme Brewer Feb 07 '21

Serializing to and from JSON is fairly trivial, and really doesn't take that much effort.

String json = objectMapper.writeValueAsString(object);

https://www.baeldung.com/jackson-object-mapper-tutorial

3

u/marvk Feb 07 '21

Sure, when you have POJOs assembled from primitives or simple Objects it's trivial. But often times you end up needing a few adapters.

4

u/[deleted] Feb 07 '21

This is the reason why I don't like to skip any questions, I don't know when I can miss something important for me. You just share the link with us that I didn't know people make summaries of books. I didn't know I can find that on Github too.

Thank you!

Do you know for some collection of book summaries or any other good links on GitHub?

3

u/Chris_TMH Feb 07 '21

Maybe something to do with lazy loading? Where data is only fetched when needed. I guess with serialization, not all objects in memory are somehow already serialized - they are only serialized when being sent/received.

1

u/Jyotikb25 Feb 07 '21

Thanks, this makes sense. So can i consider this same meaning of lazy in for Springboot @lazy annotation which is used to avoid circular dependency.

1

u/Chris_TMH Feb 07 '21

Maybe? Just my guess. Were you able to ask for clarification?

1

u/Jyotikb25 Feb 07 '21

I tried asking in the end of interview but all in vain.

6

u/sindisil Feb 07 '21

Others have answered your specific question (i.e., that Java Serialization is "lazy" because by using it you're accepting many potential problems in trade for easy implementation). The more important lesson is the fact that you're asking here, rather than having asked your interviewer for clarification (assuming it wasn't a written or automated interview).

A key part of growing as a developer (and as a person, IMO) is becoming comfortable with asking clarifying questions, without (or despite) worrying that people might think less of you for not understanding right away.

Good on you for asking here though. Many people would just either drop it or start parroting it without trying to understand the issue.

8

u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 07 '21

Generally, if you don't understand a question, it's best to simply ask what they mean exactly. If you're just going to guess, you're probably going to guess wrong.

In this case, it's a strange question. While serialization can be tied to lazy loading, there's no hard link to serialization and lazy loading. Serialize happens when you 'do' it, not before, not after.

0

u/[deleted] Feb 07 '21

[deleted]

2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 07 '21

Which is what I said in the piece you cut from my quote. If it’s unclear; ask.

3

u/ankit_jajajaja Feb 07 '21

Hey guys i am mostly confused with this word Lazy. I hear it many times like lazy initialisation. What does it means ?

5

u/tiedor Feb 07 '21

Don't know what is the meaning in OP's question. But generally speaking, is lazy when it's done only when needed. It's the opposite of eager, meaning it's done as soon as possible, even if you will end up with not needing it.

3

u/marvk Feb 07 '21

In this context, it has no secret meaning. It literally means either one or even both of

Unwilling to do work or make an effort; disinclined to exertion.

Showing a lack of effort or care.

-2

u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 07 '21

That's just plain wrong. "Lazy initialization" has nothing to do with what you're describing. /u/tiedor is correct.

3

u/marvk Feb 07 '21

I said "in this context" as in "in the context of the original question".

3

u/Mimehunter Feb 07 '21

The original may have actually meant ' when needed' - the interviewer could have been trying to see what level of knowledge the applicant had of the terminology.

Then again, you could be right.

Then again (as an interviewer) sometimes you leave it open ended just to get them to talk.

But only ops interviewer could say for certain