Forum Stats

  • 3,837,065 Users
  • 2,262,222 Discussions
  • 7,900,195 Comments

Discussions

Java special class "e" vs alias/reference variable

3462211
3462211 Member Posts: 27
edited May 25, 2017 2:27PM in Java Puzzle Ball

Hello,

How do I know when to use Java special class e and when to use alias(reference variable) of an object's instance in lambda expression?

On page 4 of the slides for lesson 4-2, where Java special class e is used in lambda expression.  Here it's clearly referring to the direction of the blade is going.  Why is that alias b of the object blade is not being used?

blade.setOnMousePressed

(e -> setDirection (Dir.NE  ));

On page 9 of the slides for lesson 4-2, where alias b of BumperList is used in lambda expression.  Obtaining the shape of the objects stored in the BumperList is an event.  Why is that Java special class e isn't being used here?

BumperList.stream()

.filter

   (b -> b.getShape() == shape.STAR)

.forEach

   (b -> b.setShape(Shape.RECT)  );

Thanks in advance,

Carol

3462211

Answers

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 23, 2017 2:38PM

    These are great questions.  They're so technical!  The details might make more sense when you experiment with lab 4, since you'll have all the methods and classes available to you (although in the context of banking software).  But I'll try to clarify what's happening in the case of Java Puzzle Ball.

    The code on slide 4 is a slightly shorthand way of writing the lambda expression.  It could also be written as:

    blade.setOnMousePressed((MouseEvent e) -> setDirection(Dir.NE));

    e is the reference variable used to represent an instance of the special class MouseEvent.  I may have said "event" in the video, just as general term to mean any event when the mouse or keyboard is pressed.  We could name the variable anything: a, b, c, d, e...  I chose e just to re-enforce that it's an event.  The field blade is an instance of an ImageView.  ImageView is a special type of object in JavaFX,  Adding this component to the Ball class makes the ball clickable and capable of detecting mouse presses.   ImageView is going to be a lot like the Button fields you'll encounter in Lab 4.  Once the mouse press is detected, the lambda expression is free to call whatever methods it can.  setDirection() just happens to be written in the Ball class.

    The code on slide 9 is also a slightly shorthand way of writing the lambda expression.  It could also be written as:

    bumperList.stream()    .filter((Bumper b) -> b.getShape() == shape.STAR)    .forEach((Bumper b) -> b.setShape(Shape.RECT));

    b is the reference variable used to represent an instance of a Bumper.  Much like the abstract Account class, this scenario assumes there's an abstract Bumper class which all types of bumpers inherit from.  We could name the variable anything: a, b, c, d, e...  I chose b just to re-enforce that the stream is dealing with a bumper.  We're able write the shorthand version of this code and omit declaring b a Bumper because the ArrayList<Bumper> bumperList is already understood to contain only Bumpers.  Obtaining the shape of a bumper is done by calling its getShape() method. This will be a lot like how Lab 4 requires you to call the getBalance() method on an account. The getBalance() method is written in the Account class, like how getShape() is written in the Bumper class.  In order to call this method from another class (like from the ButtonController class) you need to use the dot operator on an instance of an account.

    Does that help?  I hope I didn't get too confusing.

    Nick

    34622113462211
  • 3462211
    3462211 Member Posts: 27
    edited May 24, 2017 12:29AM

    Thanks a lot for the brilliant explanation Nick.  That really helps to clear my mind.  I do find the longhand version of the lambda expression so much easier to understand than the shorthand ones.  I guess the shorthand version is only used to demonstrate lambda expression for this course.  Is the shorthand version or the longhand version the normal way of Java programming?

    Thanks again,

    Carol

    3462211
  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 24, 2017 4:45PM

    Thanks!  Glad I could help clarify.

    I prefer the minimalism of the shorthand version, but the longer version does re-enforce helpful information...  I'm not sure which one is "normal".  Sometimes, programming styles are just a matter of personal preference.

    Have you seen the HBO show Silicon Valley?  There's a scene in Season 3 where a couple breaks up because they disagree on whether to use tabs or spaces while programming: https://www.youtube.com/watch?v=SsoOG6ZeyUI&t=30s

    Nick

    34622113462211
  • 3462211
    3462211 Member Posts: 27
    edited May 25, 2017 1:35AM

    I've just finished both lab 4 and quiz 4 Nick.  I think one of the questions in quiz 4 wasn't covered in the course material.  I'd like to know the answers of those 2 questions I didn't get it right.  Of course after June 1, when quizzes are no longer available for students to take.  Is it possible?

    Thanks for the funny youTube web link.  It was really funny to watch!  I think the longer version of lambda expression is a necessary evil to start with for someone like me who don't know anything about Java.  It really helps me to understand the logic of your code. 

    I probably have a thick head.  I fully understand the filter bits for stream() now, but still somewhat puzzled with MouseEvent e.  I thought you stated in the slide 4-2 that an event is a special class in Java, it's created by clicking the mouse on an object, such as blade.  If my understanding is correct, event is sort of an abstract class and MouseEvent is a sub class object derived from event.  So whenever the method setOnMousePressed is called on object blade, it's automatically assume a MouseEvent object is automatically being created.  That is, it doesn't matter which alias(reference variable) I use in the shorthand version of code.  Java compiler automatically know sub class object MouseEvent is automatically created.  In a word, If I were to write a shorthand version using w as the reference variable for MouseEvent, there won't be any logic or compiler error.  Am I correct?  Or They're just a bunch of nonsense junk thought out of my head? 

    blade.setOnMousePressed(w -> setDirection(Dir.NE));

    Carol

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 25, 2017 2:27PM

    Congratulations!

    I think it would be fine to go over a couple quiz questions.  The scenario that I'd want to avoid is when someone posts all the answers to a quiz.

    Impressive!  Yes, I think your observations are absolutely right!

    Nick

    34622113462211