Thursday, 22 September 2011

Java: Question + Removing Old Objects

    Let's start off today with a question from the comments! (I'm not sure if you guys generally go back to older posts to check them, and wanted to make sure it got answered for you).
Question: Is Java a multipurpose language; ie GUI's, buttons, ect - what are it's main functions besides interactive http - or correct my noobness - maybe give some insight into its major functions, and why Java over other languages? Such as Perl, ect.
     Java has a billion uses, and yes, it has GUI (Graphic user interface) functionality with buttons and the works, separate frames, what have you. This was more or less the last thing I wanted to get into on the subject of Java though, I wanted to focus more on functionality and understanding objects, classes and their relationships to one another, as well as errors and, to a small degree, memory usage. But it will come!
    For interactive http, you're thinking of JavaScript, this is actually a completely separate language to Java, much more simple, much different syntax. I'll also be in the process of learning this. You can actually use some java applets or something in browsers, but I'll just assume you meant JavaScript, 'cause I don't know anything about applets (yet?).
    The main reason to use Java is simply it's power. Object oriented programming is pretty awesome, the use and re-use of certain classes based on context allow for very flexible programs to be made, with few limitations. Compared to some of the other big languages, C and C++ especially, Java holds about the same power with, would you believe it, fewer complexities. The biggest one I can think of right now is pointers. We spoke about these last post, but in C, you actually need to work with the value of the pointer, not just the variable that holds it, and it all gets really messy really quick.
    Another great thing about Java is, since it uses a Virtual Machine (JVM, you might recognize this), it's essentially portable, the program needn't be recompiled to work on each operating system you want it on. AS LONG AS THAT SYSTEM HAS A VIRTUAL MACHINE INSTALLED. This is the primary downside, as far as I can see. Globally usable, but only if you have the (I've been told) bulky JVM on the machine in question. This isn't really a problem in a lot of cases, but still.
    I don't know enough of Perl to comment on why to choose Java over it, unfortunately, but I'm new to everything, so there.

I have adequately answered all your questionsssss

    Onto the actual post! Once an object has outlived its usefulness, there's got to be a way to remove it, to conserve space. If not, then a large program can very easily become overrun with useless information that just bogs it down. An object is considered to be unneeded when there's no variable holding a reference to it, so, when it's unreachable. Luckily, this garbage collection is dealt with by the Java run-time system. So why mention it? Well, garbage collection can happen during the running of the program, but there is no guarantee as to how often this occurs, so an unreachable object could stay in memory for much longer than you might be comfortable with. If your program is small enough, the garbage collection will probably just never occur, but there's not a lot of cases where that'll still be an issue, unless you're running a couple hundred of these small programs at a time.
    A lot f the time, it's possible for an object to be collected without having to worry about it's individual elements/instance variables. If the instance variable references another object, that object will be garbage collected given nothing else refers to it. Yay, chain reactions. But, to allow a programmer some control over object removal, there's a method you can declare in the object's class called "finalize", which is called prior to an objects removal through garbage collection.
    Typically, this method will do all the work required to free resources held by an object that won't be properly managed by the garbage collector. It could be used to close a network connection that would otherwise remain open, or make sure the information in an object is safely written to a file, instead of lost forever. finalize() is an ordinary method in every regard, except for it being automatically called on garbage collection, and as such isn't subject to special rules or regulations like the constructors were.\
Note: use "public void finalize()" as the method signature, or it won't be called!

    Finally, it should be noted that automatic garbage collection does not perform garbage collection or call any finalize methods on termination of the program. While this can't be changed, the ability to ensure everything is properly terminated is important enough to warrant a method to support the behavior. Namely:


....which as you might be able to guess, runs all pending finalize methods. Another useful function you might want is:


...which forces garbage collection to happen right the hell then, instead of having to wait for it. Garbage collection generally makes things a lot easier, since, for the most part, you don't need to worry about details regarding what to delete and when. The penalty we pay for this functionality is a small overhead due to garbage collection taking up system resources, but it's a small price to pay (dun dun duuuun).

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, and feel free to throw loads of criticism at us. 


  1. i heard you can make apps with java is this true?

  2. Well you sure know more than I do! I can't keep up with any kind of script :(

  3. Truth to be told, i have no idea about what you talking about.

  4. @Max Yeah, you can, as far as I know, it uses some Micro Edition of Java, or something.