aspose file tools*
The moose likes Beginning Java and the fly likes strange form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "strange form?" Watch "strange form?" New topic
Author

strange form?

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
i don't understand this:



especially the part that reads:




i never saw anything like this.

does anyone understand please?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
That's a class cast. "z" is of type Object in method accept, and in order to invoke the punkty method on it, it needs to be cast to a class that has that method - Zawodnik apparently does.

Some more information about objects, class casts and other topics you will find useful: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


Ping & DNS - my free Android networking tools app
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
Ulf Dittmer wrote:That's a class cast. "z" is of type Object in method accept, and in order to invoke the punkty method on it, it needs to be cast to a class that has that method - Zawodnik apparently does.

Some more information about objects, class casts and other topics you will find useful: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


So class casting would mean making an instance of one class be an instance of another class right?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
No. A class cast tells the Java compiler that an object it thinks is of type A is actually of type B. The instance of the object is not changed in any way. Of course, this only works if the object is actually of type B, otherwise you'll get exceptions at runtime.

In this case, the compiler thinks the type is "Object" (which is correct, as every object is of type "Object"). whereas you know it's actually "Zawodnik".
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
You can check at run-time whether an object is instance of a given type class or interface.


Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
Ulf Dittmer wrote:No. A class cast tells the Java compiler that an object it thinks is of type A is actually of type B. The instance of the object is not changed in any way. Of course, this only works if the object is actually of type B, otherwise you'll get exceptions at runtime.

In this case, the compiler thinks the type is "Object" (which is correct, as every object is of type "Object"). whereas you know it's actually "Zawodnik".


ok so initially the compiler thinks "z" is of type Object (which is a higher class) because of the parameter in the method . right?
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
@Ivan Jozsef Balazs
where did you get "za" from? it's confused me more .
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome Emanuel

Have you read the book "Head First Java" by by Sierra-Bates? If not, please do so. Devote the next 10 days to understand the fundamentals. After you are thorough with all the basic concepts of Java language, will you be able to appreciate fully what others have tried explaining.

My answer to your question:

What you are confused about is called "Reference variable casting" in Java language. If I start explaining that in detail, it is bound to confuse you even more until you have your fundamentals about basic OOPS concepts like Inheritance, Polymorphism, Abstraction, Encapsulation etc. crystal clear in your mind. Hence, I advised you to read the book. As and when you have doubts, you can ask on the forum.

~ Mansukh
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
Mansukhdeep Thind wrote:Welcome Emanuel

Have you read the book "Head First Java" by by Sierra-Bates? If not, please do so. Devote the next 10 days to understand the fundamentals. After you are thorough with all the basic concepts of Java language, will you be able to appreciate fully what others have tried explaining.

My answer to your question:

What you are confused about is called "Reference variable casting" in Java language. If I start explaining that in detail, it is bound to confuse you even more until you have your fundamentals about basic OOPS concepts like Inheritance, Polymorphism, Abstraction, Encapsulation etc. crystal clear in your mind. Hence, I advised you to read the book. As and when you have doubts, you can ask on the forum.


Thanks for your advice mansukhdeep. I have read some books...most use technical languages. I couldn't understand everything. I'm a greenhorn and that's why I'm on a forum for java greenhorns. If I understood what I read in books, I wouldn't be a greenhorn....and I prolly might not be here ( except to help others) right? You referring me to other books will keep me in circles...I appreciate your advice but I would be more grateful if you tried to explain to me what's going on like our friends above are trying to do...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Head First Java explains it this way...

You have different models of TVs. Fancy TVs do fancy things, and basic TVs do basic things. A basic tv may have:

volume up
volume down
channel up
channel down
power

A fancy TV has all the above, plus

pic-in-pic
direct channel access (i.e. "go to channel 87)
SAP

Now, a basic remote can zap a fancy tv, and it will work. You will not be able to access the fancy feature with a basic remote, but you can do the basic stuff. Changing what type of remote you use does not change the TV.


so...

the TVs are like objects. The remotes are like references. So when you have something like


on line 1, you have an Object reference (named 'z'). That is like a basic remote. It is pointing to some kind of TV, but for right now, the only 'buttons' available are the ones for an Object.

When you say


You are telling the compiler: "I know you think z should not be allowed to call the punkty method since 'z' is defined as being an Object reference, and the Object reference (i.e. remote) does not have a punkty method on it. But I know that when we get here, the ACTUAL object 'z' is pointing to is a Zawodnik, so for now, pretend that is true and let me do this.".

The compiler will see if this is even possible. For example, you cannot cast a String reference to an ArrayList, because there is NO POSSIBLE way that could happen. However, since the inheritance tree does allow for Zawodniks and Objects to be in the same tree, the compiler says "OK".

Now, when you get to your runtime, if it indeed turns out that the Object is NOT a Zawodnik, you'll get a runtime error.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Could you paste the whole code Emanuel? Then I would be able to understand the whole picture. As of now, your code says:




Can you also paste the implemented interface "Predicate"? I will be able to give you a better picture of what is going on and why it is done the way it is.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Emanuel Mensa wrote:Thanks for your advice mansukhdeep. I have read some books...most use technical languages. I couldn't understand everything. I'm a greenhorn and that's why I'm on a forum for java greenhorns...

OK, well the best advice I can give you, as a greenhorn, is: DON'T CAST.

The whole point of using an Object-Oriented language is that you have the correct type of object; otherwise, why not just make everything an Object and be done with it?

Admittedly, there are a few situations when you can't always know; but since 1.5 and the introduction of generics, there really is very little reason why you should ever need to cast anything.

So, my advice:
1. Avoid casting at all costs.
2. If you find you absolutely cannot do without a cast (and ask yourself whether there is any other possibility at least 3 times before you code one), follow the good advice you've been given.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Well Winston, people are trying to answer the kid's question on how to hold the bat. And you just come and hit a home run. Not fair.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Mansukhdeep Thind wrote:Well Winston, people are trying to answer the kid's question on how to hold the bat. And you just come and hit a home run. Not fair.

You may be right, but it's simple advice: don't cast.

Just like:
  • don't obsess about memory
  • don't use '==' unless you have to
  • don't use String for anything except strings

  • The problem with all of the above is that explaining why you don't do it takes more knowledge than a lot of beginners have when you give the advice. That doesn't make it bad.

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    You left out instanceof operator as one of the things one must avoid using as far as possible. It goes against the very idea of OOP paradigm which calls for classification of objects in a proper manner
    Emanuel Mensa
    Ranch Hand

    Joined: Feb 22, 2013
    Posts: 31
    @fred rosenberger: you're AMAZING!! thanks for your time and explanation. you made it so simple. i actually bought (hehe, i know i could download it for free) "Head First Java" yesterday and i started from scratch.
    not because i don't know what a variable is or what an Object is or what inheritance is. but wanted to catch all the basic things again . thanks man.

    @Winston Gutkowski: simple advice but what if I HAVE to cast. then i have to know how to do it and i MUST do it right? hehe. but thanks, i'll try not to cast, even if i need to do it i won't )

    @Mansukhdeep Thind: thanks for your help mate. actually i didn't write that program. in my data structures class we're learning iteration now and my prof post that code to explain
    something, i know about casting and autoboxing except that i never had an idea you could cast an object you created yourself (i didn't think hard enough)...but you guys are amazing!!

    thanks...now i'm happy. lets get back to coding.

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 8008
        
      22

    Emanuel Mensa wrote:@Winston Gutkowski: simple advice but what if I HAVE to cast. then i have to know how to do it and i MUST do it right? hehe. but thanks, i'll try not to cast, even if i need to do it i won't )

    As I said, with generics, there is almost no need to cast these days (the only major exception still being, unfortunately, when you're implementing equals()); and furthermore, it's often a sign that your code (or your design) is flawed.

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .

    Asking questions is almost never stupid. Us bods who think we have all the answers on the other hand...

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    PS: i'll have more questions that will do nothing but force you to think i'm stupid .


    I believe it's the opposite. If you have no questions at all, then probably you are stupid because you think that you know everything.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: strange form?