• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it possible to use new ***() as the argument directly?  RSS feed

 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When reading the description saying Event object holds data about the event from the book called<Head First Java>, I feel confused because it recalled me  another description saying an object's life has no value , no meaning , no point, unless somebody has a reference to it. How to understand these two sentence saying both "holds data "and "has no value", which seems to me that they are conflict?
-222.PNG
[Thumbnail for -222.PNG]
-111.PNG
[Thumbnail for -111.PNG]
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "unless somebody has a reference to it" part is important - all objects (event objects or otherwise) are worthless unless you have a reference to it. If you do have a reference to them, presumably all objects do have some value. (The "value" here means not "data", but "worth".)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:. . . all objects . . . are worthless unless you have a reference to it. . . .
I think I can find exceptions to that rule.

In the other discussion, I showed an example of a GUI which can be shown by writing new MyGUI(...); and nothing else.
I think a reason for confusion is to do with the event model of GUIs. A GUI tends to produce many event objects; if you have a GUI and move the mouse slowly across it (10 seconds), the GUI will produce several hundred mouse motion events. You usually don't need to know about the mouse moving across a GUI, so allow those event objects to disappear unused. Those objects have potential worth, but you ignore that potential worth because you don't need to use it. The mouse motion events contain information about the location of the mouse at that moment, but again you ignore that because you don't need it.

This question looks similar enough to OP's other discussion that it is worth merging the two topics.

OP (or somebody): please tell us the page numbers in HFJ too. That will make it easier to find the section you are discussing.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, a.add(new ***()).
When using this object as a arguement directly , how is it copyed and passed into the parameter? Why doesn't the object need to be assigned to a reference firstly and then use its reference as the arguement(like *** b=new ***(),  a.add(b))?
The description saying an object's life has no value , no meaning , no point.... inspired me to come out with this question.
object.PNG
[Thumbnail for object.PNG]
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crystal Zeng wrote:For example, a.add(new ***()).

"an object has no point unless somebody has a reference to it"
In this case 'a' (I'm guessing is a list) gets the reference when add() is called.
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Crystal Zeng wrote:For example, a.add(new ***()).

"an object has no point unless somebody has a reference to it"
In this case 'a' (I'm guessing is a list) gets the reference when add() is called.

Where is the reference?I can't see it.
 
Carey Brown
Saloon Keeper
Posts: 3308
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for instance creates a string object and returns a reference but because we're not assigning the reference to anything the program will forget about this String reference.
Creates a String but this time assigns it to the variable 's'. So as long as 's' is alive (i.e. doesn't go out of scope) the program will remember the String reference.
Now the reference is assigned to 's' and then 's' is passed as a parameter to add(). If 's' goes out of scope it doesn't matter because the collection 'a' now has a copy of it. In this case 's' was just a temporary name we gave it so that it made human readable sense when we passed it to add(). We didn't need to give it a name at all if we just gave it directly to add().
Here we pass it directly to add(). The end result is the same as the preceding example but we don't have to give it a named reference.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parameters are variables -- a reference from a parameter is just like a reference from a variable.
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an object has no point unless somebody has a reference to it


This isn't really true. You can create a reference to an object like this:



Now, right now nobody (i.e. no variable) has a reference to the object... but that doesn't stop you from using the object. Let's suppose our Something class has a public method named dump() which dumps some information to the console. Then you can call that object's dump() method like this:



and you'll see that information being dumped to the console. Notice that no variables were used to make that happen.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but such an object has no lifetime beyond the statement.
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:for instance creates a string object and returns a reference but because we're not assigning the reference to anything the program will forget about this String reference.
Creates a String but this time assigns it to the variable 's'. So as long as 's' is alive (i.e. doesn't go out of scope) the program will remember the String reference.
Now the reference is assigned to 's' and then 's' is passed as a parameter to add(). If 's' goes out of scope it doesn't matter because the collection 'a' now has a copy of it. In this case 's' was just a temporary name we gave it so that it made human readable sense when we passed it to add(). We didn't need to give it a name at all if we just gave it directly to add().
Here we pass it directly to add(). The end result is the same as the preceding example but we don't have to give it a named reference.

According to the attachment below, new ***() means creating an object, it seems not to refer to a reference. What's more, when saying new, the constructor runs, but the constructor return nothing , doesn't it?
-333.PNG
[Thumbnail for -333.PNG]
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
an object has no point unless somebody has a reference to it


This isn't really true. You can create a reference to an object like this:



Now, right now nobody (i.e. no variable) has a reference to the object... but that doesn't stop you from using the object. Let's suppose our Something class has a public method named dump() which dumps some information to the console. Then you can call that object's dump() method like this:



and you'll see that information being dumped to the console. Notice that no variables were used to make that happen.

Are you saying the description from the book<Head First Java>is wrong? You know, all the pictures(i.e.img) come from this book.
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crystal Zeng wrote:According to the attachment below, new ***() means creating an object, it seems not to refer to a reference. What's more, when saying new, the constructor runs, but the constructor return nothing , doesn't it?


Java programmers have this tendency to not spell things out. For example they will often refer to a variable as a reference, even though the formal way to understand it is that a variable contains a reference. Likewise if a variable contains a reference to an object, programmers will sometimes abbreviate that by saying that the variable contains the object. This can be very confusing to the beginner.

So no, a constructor doesn't return nothing. The attachment you posted is an example of programmer shorthand. A constructor creates an object and returns a reference to that object, and then that reference can be assigned to a variable. You'll notice that the attachment says that the object is assigned to the reference variable, but in fact it's a reference to the object which is assigned to the reference variable. It gets tedious spelling this sort of thing out all the time so it's more convenient to abbreviate.
 
Crystal Zeng
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So no, a constructor doesn't return nothing. The attachment you posted is an example of programmer shorthand. A constructor creates an object and returns a reference to that object, and then that reference can be assigned to a variable. You'll notice that the attachment says that the object is assigned to the reference variable, but in fact it's a reference to the object which is assigned to the reference variable. It gets tedious spelling this sort of thing out all the time so it's more convenient to abbreviate.
Thank you for your detailed reply. I got it.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...

You could think of the constructor as returning a reference to the object that just got created and walk away happy. However, that kind of ignores the role of the new keyword in class instantiation. You can't just call a constructor like it was a plain old method; a constructor is (almost)* always invoked with the new keyword preceding it. So, I would prefer to think of the new keyword as the magic ingredient that "returns" the reference to the newly minted object. For me, this keeps constructors different from methods and is conceptually consistent with not being able to have a "return something" statement in a constructor.

*I say "(almost) always" because you can use the reflection API to invoke a constructor but that's a whole 'nother story.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The way I look at it, there are three things you can do with a reference to an object:-
  • 1: Do nothing.
  • 2: Use it immediately.
  • 3: Store it in a variable.
  • You are presumably used to storing things in variables:-You can use the reference immediately, and then you decide you no longer need to use it:-That prints the details of the kettle but you then no longer use that Kettle object. That is a valid decision, but obviously less common than storing the reference in a variable.
    The following is a sort of intermediate state between the two: you don't have a reference to the Kettle, but you do know the List is storing it for you and you can find it from the List:-It may seem daft simply to do nothing with a reference, but the compiler will let you writeThat isn't going to do you much good, because the Kettle reference simply disappears into cyber‑limbo never to be seen again, but have a look at this GUI code:-That constructor creates a new Thread (called EDT) and starts it and creates a GUI and that GUI will run happily on its own thread independently of other code. If you call the constructor, the GUI will appear and you will not need to reference the GUI object again. It will work by itself even if the reference disappears.
    Now you can show your GUI like this:-This is a sort of intermediate state between “do nothing” and “use it immediately”, because you could say the code has been written so that the references makes use of itself.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, the fact that new is an operator and has a relatively high order of precedence in an expression supports the idea that it is really what makes an object reference accessible for use. The Java Tutorial has the following under "Creating Objects":
    The Java Tutorial wrote:The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7961
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If we're being pedantic, the new keyword is an operator that takes a class name and an argument list, and returns a reference to a new object. It works by first allocating memory for the new object, running initializers and then calling the constructor on that new object. So really, the constructor doesn't return anything, it's called on an object that already exists.

    Using the new keyword is like using any other expression. You can assign its return value (a reference) to a variable, or you can use it in a larger enclosing expression*, such as a method call. In that regard, it's no different than calling something like isPrime(4+3), except the sub-expression is an invocation of the new operator, instead of the + operator.

    *technically, assignment to a variable is also a larger enclosing expression.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One last swing at this dead horse... 

    Thinking that a constructor actually returns a reference is also inconsistent with the semantics of only being to use a "naked" or plain return statement in a constructor to exit from it. Since you can only do the same thing from a method declared with a void return type, it's more consistent with the idea that a constructor returns nothing, i.e. void, rather than surreptitiously returning a reference to the newly created object.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:. . . new is an operator and has a relatively high order of precedence . . .
    The diagram with the Dog shows how Java® execution goes from left to right:-
  • 1: The declaration is on the left
  • 2: The assignment operator = is found next.
  • 3: Because = has a very low precedence, and everything to its right has a higher precedence, what is to the right is evaluated and then applied to the = operator.
  • From OP's other discussion, which I am merging here, it appears those pictures are in fact quoted from HFJ.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:. . . Let's suppose our Something class has a public method named dump() . . .. . . .
    That is another form of what I called “use it immediately”.
     
    Crystal Zeng
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    OP (or somebody): please tell us the page numbers in HFJ too. That will make it easier to find the section you are discussing.
    Page here.
    -361.PNG
    [Thumbnail for -361.PNG]
     
    Tim Moores
    Saloon Keeper
    Posts: 4019
    94
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Tim Moores wrote:. . . all objects . . . are worthless unless you have a reference to it. . . .
    I think I can find exceptions to that rule.

    By "you" I didn't mean exclusively the developer. You can substitute "someone" (like the JVM) for "you", if that makes it clearer.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Junilu Lacar wrote:. . . it's more consistent with the idea that a constructor returns nothing . . .
    Careful: the OP is a newbie and this may all be going over her head.
    Why should a constructor return anything? Constructors aren't methods. You would have to check the details in the JLS (=Java® Language Specification) but I think Stephan is correct. A reference to the object has already been created and returned: the constructor now completes the details of that object. Whilst avoiding the wonderful double negatives earlier in this thread, it would be better to say that a constructor doesn't have a return type. Not even void.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:If we're being pedantic, ... So really, the constructor doesn't return anything, it's called on an object that already exists.

    Just pointing out a subtlety in this: the object is essentially there but it may not be fully initialized when a constructor starts executing. Implicit or explicit calls to appropriate super constructors up the object's hierarchy are still made before any other statement in a constructor is executed. This is why it's not a good practice to call non-private or non-final methods from a constructor, because those methods may access fields that have not been fully initialized yet.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Junilu Lacar wrote:. . . it's more consistent with the idea that a constructor returns nothing . . .
    Careful: the OP is a newbie and this may all be going over her head.
    Why should a constructor return anything? Constructors aren't methods. ... it would be better to say that a constructor doesn't have a return type. Not even void.

    Yes, all that is true, but to Bear's point, we programmers don't always spell everything out (even though all the replies after that seem to belie that statement).

    I was trying to hedge my assertion with "more consistent with the idea". If I were to spell it ALL out, then I'd preface it with your points and then say "BUT... if you want to think in terms of simply returning a reference vs. returning nothing, then the semantics of a constructor are more consistent with returning nothing because yada yada yada..."
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal Zeng wrote:
    OP (or somebody): please tell us the page numbers in HFJ too. That will make it easier to find the section you are discussing

    It seems we have managed to confuse the OP ... 

    @Crystal: you are the OP (original poster, the one who started the thread).

    Edit: I just noticed that Campbell merged another thread in so maybe it's just me who got confused by it.
     
    Crystal Zeng
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I also wonder why a constructor can return a reference to that object.A constructor isn't associated with return/void type.Someone says this is a habit of abbreviating for programmers,a constructor really returns a reference.Who can provide strong evidence to
    say that a contructor return a reference?
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think that's actually the point we've been trying to make: that it may be more harmful and misleading to think of a constructor as something that returns a reference to a new object. The semantics of constructors just don't mesh well with that idea. It's more correct to think of the new operator as the thing that gives back the reference. The constructor simply provides a target for the new operator to work its magic on.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal Zeng wrote:For example, a.add(new ***()).
    When using this object as a arguement directly , how is it copyed and passed into the parameter? Why doesn't the object need to be assigned to a reference firstly and then use its reference as the arguement(like *** b=new ***(),  a.add(b))?
    The description saying an object's life has no value , no meaning , no point.... inspired me to come out with this question.


    Hoping to wrap this up and summarize...

    A statement like this:
    is pointless because you instantiated a Dog object but did nothing with it. Immediately after the Dog is created, it disappears into thin air (or thin ether, if you prefer).
    Doing this is what the authors of HFJ referred to as "has no value, no meaning, no point."

    On the other hand, when you do this:
    you now hold a reference to the new Dog in the myDog reference variable. Now you can do something with the Dog, presumably something useful, that has meaning, that is arguably not pointless.

    Now, as for this construct:
    The expression inside the parenthesis evaluates to an object reference. That is, the new operator instantiates a Dog object via the Dog() constructor and returns a reference to the new Dog object. That (the reference) is what is passed to the add() method and assigned to the parameter declared in the add() method signature. Inside the add() method, you can use the parameter (which holds a reference to the Dog) to do something useful, meaningful, and not pointless, with the Dog.

    I think your confusion comes from thinking that you can only obtain an object reference via an explicit assignment via the = operator. In fact, another way to assign a reference to something is to pass it as an argument to a method.  So think of it this way:

    Finally, since the new operator returns an object reference, it's possible to use it in place as a one-time-use, floating, anonymous, naked, or whatever else you want to call it, reference. Like so:
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You may not have seen examples of this yet but a String literal is also itself a reference. For example, this is legal in Java:

    The String literal "Foo" resolves to an object reference. Since it's an object reference, you can use the "." operator to access its methods, like equalsIgnoreCase() or equals(). This is useful in avoiding problems with this kind of code:

    This code will produce a NullPointerException if the value passed in is null, making the parameter s == null. To avoid that problem, you can do this instead:

    In this case, equalsIgnoreCase() or equals() will simply return false if the value of s is null. There is no danger of throwing a NullPointerException.
     
    Crystal Zeng
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My confusion comes from the mind that I always think of new **() as an object and an object isn't allowed to be used directly.The book called <Head First Java>never says new operator will return a reference.From the descriptions below, I tend to think of the right side of = operator as an object.
    -444.PNG
    [Thumbnail for -444.PNG]
     
    Crystal Zeng
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can click the picture named -444.PNG.
     
    Knute Snortum
    Sheriff
    Posts: 4270
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal Zeng wrote:My confusion comes from the mind that I always think of new **() as an object and an object isn't allowed to be used directly.The book called <Head First Java>never says new operator will return a reference.From the descriptions below, I tend to think of the right side of = operator as an object.

    Let's not say new **() but pretend there is a class called Foo.  Then if you write new Foo() creates an object, whose reference is stored in the variable foo.
     
    Junilu Lacar
    Sheriff
    Posts: 11476
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal Zeng wrote:My confusion comes from the mind that I always think of new **() as an object and an object isn't allowed to be used directly.The book called <Head First Java>never says new operator will return a reference.From the descriptions below, I tend to think of the right side of = operator as an object.

    This goes back to Bear's original point about programmers using shortcut notations to avoid saying "object reference" vs. "object" all the time. In many cases, in fact I'd say usually, "reference" and "object" are interchangeable. You just have to learn to discern when the distinction between the two matters in the context of the discussion and when it doesn't.

    I think you should put the idea that "an object can't be used directly" out of your head. The bottom line is that you need an object reference in order to use an object and there are other ways you can obtain an object reference besides assigning one to a reference variable.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Crystal Zeng wrote: . . . Page here.
    I couldn't see a page number. There was an image which didn't display properly and nothing else.
     
    Crystal Zeng
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Crystal Zeng wrote: . . . Page here.
    I couldn't see a page number. There was an image which didn't display properly and nothing else.

    Page 361
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!