Saturday, 24 September 2011

Java: Equality, Null and This

   Thanks for the get-wells, guys, much appreciated. In my fever dreams last night I (no joke) got told to hurry this shit up, and that focusing too much on the details of Java not only retracted from the fun/interest in learning it, but significantly drew out how long it would be to get there. Or, that's how I'm paraphrasing it, there was a lot more swearing, and naked people in the dream. So basically, I'm going to (try to) be less focused on details that are probably of no use/interest to people and focus on being able to write stuff..


    To paraphrase the above, I'm going to try to teach/learn how to drive a car, not be a mechanic.


Equality
    I mentioned briefly before that there was an actual difference between using the ".equals()" method and the "==".
    The == operator tests for equality in terms of the representation contained in the 2 variables you're testing. This works fine for primitive types since the variables contain the abstract values, but doing this for objects sucks, since the variables in that case hold references to the objects, not objects themselves. So even if 2 objects were exactly the same, == would come up false.
    In the case of .equals(), it tests for the identity of the object, and Java just kind of assumes every object has this method. If not, a default is provided, which implements identity equality.


Null
    There is a special reference value known as null (null reference). This reference points to no object, nothingness.
A reference variable should point to null when it has no value, and can be explicitly assigned to do so:




    Man, I am using the fuck out of the GenericStack class we were working on. This doesn't work so well for setting primitive types, although any uninitialized variable is set to this value. Doesn't make a whole lot of sense, but fuck it. That's how that works.  But if you try to use an object as if it wasn't a null-type, but it is, you'll get a run-time error. So watch it. The error it comes up with is a NullPointerException, so if you get that you know exactly what it is you're looking for.
    Using null is fun for conditionals and stuff, but the drawback is the amount of extra code and testing you need to do. Still, fun times.


This
    Every instance method has this local variable automatically declared. "this" is a reference to the specific object the method has been called for. There are 3 main uses for this:


1. It allows a reference to the current object to be passed as a parameter to another method.
    object.someMethod ( this );


2. It can be used when calling other methods, or accessing instance variables on he current object.
    public void someMethod2 ( ) {
        this.someMethod3 ( );
        this.someMethod4 ( this.someVariable );
    }


3.  The third is to use one constructor to call another but really, this gets confusing fast, and I don't see a lot of use for it, at least for small-time programs, so let's gloss over it.


That's it for tonight.Questions welcome! Comment, follow, subscribe, share etc, and see you tomorrow!




    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

6 comments:

  1. I'm going to start learning programming soon too, I'll follow along with you, maybe at the end of the month you could do a quick summary on some basic things, or a basic program tutorial? Would be really cool to see.

    ReplyDelete
  2. Oh shit, that's a good idea, actually. Thanks, I'll do summaries every now and then.

    ReplyDelete
  3. This looks well too complicated for me! Great post though!

    ReplyDelete
  4. Some good info on this post. Everything coming along great man

    ReplyDelete
  5. Great information, very useful.
    Thanks for sharing.

    ReplyDelete
  6. I will try to slowly learn about this while following your blog. It might work or not, but i will give it a go.

    ReplyDelete