Skip to main content

Final-Reference Variables-Constants in Java. What does it all mean?

So far, my programming journey has been easy riding. Everything in computer science I've come across so far seems so logical and (really) obvious after a moment's thought.
(n.b. The following will contain mistakes, don't copy and paste this for your Comp.Sci.101 Assignment. If any readers can help clarify my thinking, it would be much appreciated.)

Well, except for constants in Java.

In Java, you can define a variable and give it a value:
 (e.g. Year current =1932; creates an variable of type Year and initializes it with the value 1932).

Speaking correctly, the variable current is NOT equal to 1932. Rather, current is a reference to a location in the computer's memory where the data 1932 is stored. A variable name like current can be thought of as a listing in a telephone directory: "To find the current year, go to this address in the memory."

Variable are so called because their values can change during the running of a program. For example, we could change current=1932 to current=2014. The variable current would now refer to a different location in the memory where 2014 is stored. Since current is really a reference to a memory location,  the value of current has now changed  to a different memory location.
The value 1932 would be disposed of by Java's garbage collector because there is no longer any reference to it in the program. (i.e. 1932 was being referred to by current. Now, current  has been re-assigned to 2014, current's link to 1932 has been severed.)

In addition to variables (whose value can change), Java allows constants. Constants are designed to be unchanging.

For example, if you write a program that defines the class zebra, you might include variables like: no_of_legs, no_of_stripes, sex, among others.  Now, sex and no_of_stripes  will be different for different zebras, so we'll define those as variables. However, barring any accidents, zebras will have 4 legs.
Therefore, we'll make no_of_legs an integer constant, like this:

final Leg NO_OF_LEGS =4;
(This creates the constant NO_OF_LEGS of type Leg.  Assume the type Leg has been defined elsewhere in the program)

This tells the program that no matter what happens, all the zebras it ever sees will have 4 legs. Or does it?

NO_OF_LEGS is a reference to a location in the memory. Because it has been declared final, NO_OF_LEGS never refers to a different location. Programmers say it is referring to a specific object in the memory, and that object  cannot change. Seems reasonable.

BUT, the value stored in the object CAN change. In other words, we could re-purpose the program  zebra to deal with both 4-legged and the new breed of 6-legged zebras. We could add code to the program that said:
    NO_OF_LEGS = 6;

How is this different from the current year example above? There, when we re-assigned current the value 2014, a new object was created in the memory with a different address. current  was updated with a reference to the new address.

With the constant NO_OF_LEGS,  we have not created a new object in the memory. So we have not changed the location NO_OF_LEGS is referring to. Instead, we have replaced the 4  in that memory location with a 6.


There's one more question: why, wherefore, what's the reason, and ??




Comments

Popular posts from this blog

CodeSchool vs Codecademy(or 'How socket inherits event listening methods and implements asynchronicity')

In this review I'm going to focus on the pedagogy that I see evident in some CodeSchool courses and compare them to  Codecademy. By pedagogy, I mean: 'How does CodeSchool teach?' and ' Does it do a good job of teaching?'. I'm going to argue that despite high quality videos, colourful web pages, and often ssspppeeeeeakkkiiiing...rrrreeally...slowly..., CodeSchool's pedagogy is inferior to that of Codecademy. There are many fantastic resources for learning to code on the web, and CodeSchool is one of them. So far I have completed courses in Ruby, Rails, Javascript, HTML/CSS, Jquery and Git on CodeSchool. The courses have all included high quality videos and colourful, interactive exercises- as well as  massive  pdf files of the slides ( the files take more than a minute to load on my machine .) The question is: does the higher production value mean better educational quality? The 'Try' courses on CodeSchool(such as Try Ruby and Try jQuery) are f...

Algorithm Analysis - 1

Currently, I'm learning to perform algorithm analysis using Big-O notation. In one resource I found the following problem: You just dropped a box of glass toys and  n  toys in the box broke in half. You'd like to match the halves of the toys so that you could glue them together, but the only way to tell whether two halves belonged to one toy is to physically pick up the two pieces and try to fit them together. Express how long this matching process will take in terms of  n . The answer given is n^2 (n squared) with the following explanation:  You have to compare every piece with every other piece. If you have 1 toy and it breaks in half, you have 1 comparison to make. If you have 2 toys and they both break in half there are 4 pieces and you have to do 6 comparisons. If you have 3 toys, there are 6 pieces and you have to do 15 comparisons. If you have  N/2  toys, you have  N  pieces and you have to do N-1 + N-2 + N-3 + ... + 1 =(N)(N-1)/2...

Ruby, RSpec, Linux and Relational Algebra (or "What I'm doing this Christmas")

Last week was challenging- I'm beginning to appreciate the need for a programming buddy.  So far, I haven't found a Ruby IDE that helps with missing brackets or illegal syntax. This means hours spent looking for errors which more experience (or an IDE) would have solved in an instant. I've spent a lot of time on Stack Exchange looking for solutions to problems that many have already had- so far I haven't had to post a single new question (though it's evident that many people post duplicate questions, not having looked for a solution first). I spent longer on the Ruby project than anticipated, BUT I really enjoyed learning about Behaviour Driven Development. Thinking of tests in RSpec and then watching my Ruby script pass (eventually) was like passing an exam (though  one I had written myself). This week I'll be starting Rails-after I complete the CodeSchool Rails courses I'll go on to their RSpec course. Partly for the geek cred, partly out of curiosit...