Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from K&B Chapter 5

 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,




When I run this program it prints null null.
But I can't understand how. Can you please explain what's happening in lines 1, 2, 3.

Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run this program it prints null null.
But I can't understand how. Can you please explain what's happening in lines 1, 2, 3.


Line 1 declares an array of size 2. Line 2 iterates through that array. Hence, the getWeight() method is called twice, at line three.

So, this call getWeight() twice, which prints the "s" variable. And since the s variable is assigned to null, it will print null twice.

Henry
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.

When it iterates through the array, new Mineral() - will this call the constructor of Mineral class?

Thanks.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey A,

The constructor of Mineral class are called while forming the array not while iterating. The the constructor is as called at the line mentioned below -
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we extend a class without body ???
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey vamsi ,

Definitely, you can extend a class without body. The example is as mentioned above.. but ideally, if such need arises, blank interface should be preferred than blank class.

You can see many empty (without any function or variable) interfaces in java as well like clonable and serializable.
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you salil ,but what is the use of such classes and interfaces ??? and what is the use of extending them ?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vamsi acharya wrote:thank you salil ,but what is the use of such classes and interfaces ??? and what is the use of extending them ?


Generally these type of interfaces are called tag or marker interfaces. They maybe used for polymorphism. Read this...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that the exam taking environment is very constrained. In general most questions will be stated using no more than 12 lines of code - of course there will be longer bits of code to read thru, but most questions will be stated using 12 lines of code or fewer.

Because of this, the questions often use weird constructs like empty classes, so that the code can focus on the more important issues of the question. In other words, don't expect the code in the questions to use anything like best practices, best practices are a low priority for the folks who create the actual exam questions. Also expect to see horrible stuff like this:



Just remember - the exam creators are trying to jam a lot of code into 12 lines - it gets ugly!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic