Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about dot operator (K&B7, chapter 3, self test question 3)

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given this code:


I can not understand the operation of the parameter within the System.out.println.
The solution of the book says:

The S.O.P. can be read as "Use the n3 object's Network reference (the first p), to find that object's reference (n2), and use that object's reference (the second p) to find that object's (n1's) id, and print that id.


but I can not figure out what really happens.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The print-statement from line16 can be rewritten as:

Small note: you made a little typo in the signature of your main method. And although the code snippet compiles without any errors, you can't execute the program. Can you spot your typo?
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Small note: you made a little typo in the signature of your main method. And although the code snippet compiles without any errors, you can't execute the program. Can you spot your typo?


public static void main(String args[])
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
Roel De Nijs wrote:
Small note: you made a little typo in the signature of your main method. And although the code snippet compiles without any errors, you can't execute the program. Can you spot your typo?


public static void main(String args[])



Although that's correct, for better readibility String[] args is preferred. But be prepared, because on the exam you'll see all kinds of combinations and you need to know when it's valid and when you'll get a compiler error. Or when a method seems to be a main method, but in fact is just an ordinary method which happens to have the same name as the main method. E.g.

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is this? new Network() ?
 
Paweł Baczyński
Bartender
Posts: 1877
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:What is this?

You can read about this here: Using the this Keyword.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, maybe I haven't asked my question right. I know about this, but I usually see "this" as this.property and this() (as calling constructor) so far. I encounter only "this" at first time. Therefore I want to know that what is this in this case? new Network() ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Therefore I want to know that what is this in this case? new Network() ?

No! It's a "special" reference to the current object, the this object (hence it's name ). Adding a few print-statements to the code snippets illustrates this clearlyOutput: this=Network@55ca6954 n1=Network@55ca6954 n1.p=Network@55ca6954

So you could consider it as a very special reference variable, having a reserved Java keyword as its name.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Therefore I want to know that what is this in this case? new Network() ?

One veyr important rule to remember: besides a few exceptions, you always need the new keyword to create an instance. So just using this would never result in a new object.

Time for another pop quiz question! Can you list the few exceptions on the above rule (with an example)? I'm thinking of 4 very common ones (which you all need to know for the OCA exams). Ready, set, go!
 
Blake Edward
Ranch Hand
Posts: 105
4
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at this yesterday because I couldn't figure it out. I drew two columns: stack and heap. I filled them in as I went through the code and then I clearly saw n3.p.p.id and understood the answer was 1. Pencil and paper has been working great for me because I can build images and pictures that stick in my head and help me with other snippets. This does not happen when I use a text editor, although, I use one to check answers and experiment with code.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:Pencil and paper has been working great for me because I can build images and pictures that stick in my head and help me with other snippets.

That's a very useful technique while studying. And in this forum you'll find plenty of drawings to better understand the code snippets. Examples here, here and here. And you'll get a pencil and paper (or equivalent, e.g. erasable pen and small whiteboard) on the actual exam, so you can definitely use the same technique when it matters as well.
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Output: this=Network@55ca6954 n1=Network@55ca6954 n1.p=Network@55ca6954

Thanks for your example, Roel!


Roel De Nijs wrote:
Mushfiq Mammadov wrote:Therefore I want to know that what is this in this case? new Network() ?

One veyr important rule to remember: besides a few exceptions, you always need the new keyword to create an instance. So just using this would never result in a new object.

Time for another pop quiz question! Can you list the few exceptions on the above rule (with an example)? I'm thinking of 4 very common ones (which you all need to know for the OCA exams). Ready, set, go!

My poor English always create problems for me I investigate about exception and read all of topic about exception in Mala Gupta's book (because of my OCA SE 8 book is in work) since from yesterday. But I didn't find any answer for this question. Today I look through your question again and I see that I don't understand your question right. Probably you mean which exception we throw using new keyword.

Maybe I understand wrong again, if it happens please correct my mistakes
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Maybe I understand wrong again, if it happens please correct my mistakes

There's nothing wrong with the code you posted, but it was not what I was looking for.

Let me try to explain it again and be more clear this time. You didn't know what only "this" meant and you suggested it might be new Network(). First I explained this is a special reference to the current object. And then in the next post I tried to explain that this could never be new Network(). Why? Because there's 1 very important rule to remember: you always need the new keyword to create an instance. So just using this would never result in a new object.

But there are some exceptions to this rule. And that's the pop quiz question: can you list the few exceptions on the above rule (with an example)? I'm thinking of 4 very common ones (which you all need to know for the OCA exams). Normally you create new objects like thisThis code creates 2 new objects, both times using the new keyword is required (as stated in the general rule). So now you should try to list the exceptions on this rule, meaning give some code examples which creates new objects without using the new keyword. And you are lucky, I'm in a generous mood. So I'll provide the first example:A new String object was created, but the new keyword is not required!

Now it's up to you to find at least 3 other examples to create an object without using the new keyword. Ready, set, go!

Hope it helps!
Kind regards,
Roel
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: But there are some exceptions to this rule. And that's the pop quiz question: can you list the few exceptions on the above rule (with an example)? I'm thinking of 4 very common ones (which you all need to know for the OCA exams).

Oh, sorry, the problem is that I understand "exceptions" as the topic Exception so I guessed such this question is about Exception. This is my thoughtlessness, sorry.

Roel De Nijs wrote:So now you should try to list the exceptions on this rule, meaning give some code examples which creates new objects without using the new keyword. And you are lucky, I'm in a generous mood. So I'll provide the first example:A new String object was created, but the new keyword is not required!


2. I think the second example are relating reference types of primitive
We don't use new because of autoboxing. I came across your post about it two days ago
A tip to help you with these kind of questions is to look at how many times the keyword new is invoked. That number is the number of how many objects that are created But be careful: with autoboxing (as in line 3) the new invocation is done behind the scenes. But line 3 actually reads: Short story = new Short((short)200);. So in this example there are 4 invocations of keyword new, so 4 objects created and thus at most 4 objects which can be eligible for GC at line 16.


3. Third example is array.
but we can create it without using new.

4. Maybe the fourth example is about enum but I don't know it exactly.

Please correct my mistake. Thanks in advance
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Oh, sorry, the problem is that I understand "exceptions" as the topic Exception so I guessed such this question is about Exception.

I assumed the use of "exceptions" made you misunderstand the question. That's why I added an example the 2nd time so it would be crystal-clear

Mushfiq Mammadov wrote:Please correct my mistake. Thanks in advance

You are absolutely spot-on!

I have to say I'm happily surprised you were able to list all exceptions to the above rule. I didn't have expected you could list all of them. Have a cow for this great achievement!

So to summarize: you don't need the new keyword to create a new object/instance in these 4 cases:
1/ strings
2/ primitive wrapper classes (thanks to autoboxing)
3/ create and initialize an array using the shortcut syntax
4/ declare an enum typeWithout using the new keyword, this code creates 7 Day instances/objects. Just to be clear: the objects/instances are created when you declare them (as in this code snippet). When you use one of the enum constants as in Days d = Days.FRIDAY; you are just using the reference variable, you are not creating an object/instance. So WEDNESDAY is actually a reference variable which is referring to a Day object/instance. This of course applies to all other enum constants as well

Hope it helps!
Kind regards,
Roel

(Disclaimer: in Java 8 you can also create lambda expressions without using the new keyword)
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote: I assumed the use of "exceptions" made you misunderstand the question. That's why I added an example the 2nd time so it would be crystal-clear

Example is the most helpful method for me

Roel De Nijs wrote: You are absolutely spot-on!

I have to say I'm happily surprised you were able to list all exceptions to the above rule. I didn't have expected you could list all of them. Have a cow for this great achievement!

In really I didn't expect all are absolutely spot-on too Thanks a lot for a cow! It is my first cow

Roel De Nijs wrote: 4/ declare an enum typeWithout using the new keyword, this code creates 7 Day instances/objects. Just to be clear: the objects/instances are created when you declare them (as in this code snippet). When you use one of the enum constants as in Days d = Days.FRIDAY; you are just using the reference variable, you are not creating an object/instance. So WEDNESDAY is actually a reference variable which is referring to a Day object/instance. This of course applies to all other enum constants as well

I had a doubt about enum. I saw that we initialize enum constant to Object type so I thought enum constant is object but I was not sure. You explain this perfectly and everything is clear now. Thanks for this

Roel De Nijs wrote: (Disclaimer: in Java 8 you can also create lambda expressions without using the new keyword)

Is it possible to be a question about this in actual exam OCA SE 8? or this topic include in OCP exam?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mushfiq Mammadov wrote:Is it possible to be a question about this in actual exam OCA SE 8? or this topic include in OCP exam?

Writing a simple lambda expression (that consumes a lambda Predicate expression) is one of the exam topics of the OCA 8 exam. So yes, you might encounter a few questions on this topic if you decide to take the OCA 8 exam.

(PS. I fixed the wrong name in the quote of my previous post. Thanks for reporting. That could be considered as a - small - mistake )
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:(PS. I fixed the wrong name in the quote of my previous post. Thanks for reporting. That could be considered as a - small - mistake )

You write many posts in a day and every post is broad. It is normal that you forget something in that post, it is not mistake

I saw you correct name yesterday so I remove "P.S." from my post yesterday)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic