Today we covered many topics while covering the JFrameDemo class (go look at it again).
Topics for Discussion (reply for participation credit):
The new keyword was mentioned today. What is its purpose?
Another word that came up was null. What is null? What does it mean for something to be null? When might you get a NullPointerException?
We mentioned the 8 primitive types in Java. What are they?
Through out today's class the word reference popped up quite a bit. What is a reference? Why can't primitive types contain references?
In Java, we don't have to worry about managing our own memory. There is a special feature that walks around in the background like your mother cleaning up your messes. What is the name of this thing? When does it clean up your memory? How do you know an Object is ready to be cleaned up?
Pass by value and Pass by reference were also mentioned. What does it mean to be pass by value? What does it mean to be pass by reference?
Look at the following code Incrementor.java: http://pastebin.com/TPN5XDbF
Don't compile and run it. First write down what you think it will print out. Explain your answer.
Now compile and run it. Did it run the way you thought? Explain what is going on here.
Preparing for Monday
Review JFrameDemo.java and understand what it is doing. Read through some GUI tutorials in Java. A good place to start is here: http://docs.oracle.com/javase/tutorial/ui/index.html
Have a great weekend!
Yay, first comment!
ReplyDeleteAnyways...
"Null" means that something is empty, for example, a "null" string. A Null Pointer Exception, however, is a little different. The exception is caused by trying to do something that requires looking up a piece of data, but the data does not exist, which then causes the program to crash. Overall, null mean that something is there, but it is empty or missing physical data.
Jennifer, this is a good start. However, you are wrong when you state, "null mean that something is there, but it is empty or missing physical data". Null actually is the "lack" of data.
DeleteNull means nothing is there. A null pointer exception means you are trying to access something or "point" to something that doesn't exist.
Deletenull is the absence of memory from the java virtual machine but i really dont know what jvm is or what it does.
Deletenull is the value of a reference variable that isn't currently storing a reference to a class instance (a.k.a. an object). This may or may not be a normal occurrence, depending on how your code is designed.
DeleteHistorically, memory addresses were used as "references" to data, and the invalid memory location 0 was used to indicate that no data was being referred to. Hence "null", the synonym for 0. However, in Java, 0 and "null" are not interchangeable in any way.
eight primitive types are:
ReplyDeleteshort
long
char
int
boolean
float
double
byte
This is correct. Now, can you give additional details on some or all of them?
Deletechar is used for single characters like a or L.
Deleteint is a 16 bit number + or -
double is a 32 bit floating point number(meaning 10.2). + or -
boolean is a true or false.
short is a two bit number + or -
You are incorrect on int, short, and double.
DeleteAlso, your explanation of char is a little vague. Could you expand on your answer?
You should take a few minutes to do a little more research on the primitive types.
int: signed 32-bit integer
Deleteshort: signed 16-bit integer
double: signed 64-bit floating point number
the code will run through the for loop 10 times increasing the value of "value" by one each time the for loop is gone through. so the output should read 0 1 2 3 4 5 6 7 8 9
ReplyDeleteAre you sure? Compile and run the code. Are the results as you expected? What is going on with this code?
DeleteThe incrementor demo code iterates through a for loop 10 times. During each iteration, the member variable named value (of type int) is passed to the static void method increment(), which takes an integer as it's only parameter. Increment() adds 1 to the value of its int parameter.
DeleteIn this demo, the int parameter of increment() gets set to the value of the int member variable passed to it. Primitive types cannot be passed by reference (in Java), rather, they are passed by value.
It follows that, each call to increment (in this case) does not modify the member variable named value. So every iteration, increment() is called, passed the value 0 (which is incremented by 1), and then the value of int value is printed (it is still set to 0 and is never changed).
See: http://pastebin.com/AWLeNQZB for a modified version. Increment() returns the incremented value of its parameter, which is assigned to the member variable value, therefore actually incrementing its value.
This is a good explanation. You should be careful when you say "Primitive types cannot be passed by reference", this is true. However, nothing is passed by reference in Java. Everything is pass by value.
DeleteWhat does it mean to be pass by value? What does it mean to be pass by reference?
I did some reading, here's what I learned:
DeleteJava method parameters are always passed by value, regardless of type. However, Java does use references. Accessing an object's methods or members is done by reference.
Pass by value: pass only the value/data of an object for manipulation and storage in a separate object of the same/compatible type.
Pass by reference: pass a reference to the memory location of the object for direct manipulation. Also, a single instance of an object can be referred to by multiple variables, multiple instances of an object can be referenced by using an array.
Wait a second, aren't arrays passed by reference rather than value? From what I can tell, in this example, the output should just be 1s all along because value is never getting changed, only i, because only the value of value has been passed, not the reference.
DeleteBasically, as I understand it, passed by value means when you pass in something like a primitive type you just take the data it holds. But when you pass by reference, you access where it is stored in memory.
Java only will pass by value no matter what your are attempting to pass into a method. However, you may be confused as to why you can change an array value when it is passed in as a parameter to some method. It is a subtly in Java that is often the frustration of many people. Kudos from me if someone can tell me why they are still passed by value instead of reference. (Hint: this is a rather confusing aspect of figuring out what exactly is passed by value)
Delete"In Java, we don't have to worry about managing our own memory. There is a special feature that walks around in the background like your mother cleaning up your messes. What is the name of this thing? When does it clean up your memory? How do you know an Object is ready to be cleaned up?"
ReplyDeleteThe feature is called the Garbage Collector (GC). The GC monitors references to objects in your program, and when it detects that there are no more live references to a particular object, it marks it for deletion. References are pointers to the object's space in memory.
So, if you create an object using the new keyword with the name myObject (the pointer), then later set myObject to point at another new object without ever making some other connection to the original data, it becomes effectively unreachable. The original data has no more pointers/references to it and thus no way for the programmer to use it, so the GC knows it's time to clean that memory space up.
Also, if an object lives inside of a scope that disappears (like inside of an if-then loop after it completes) then the object is similarly marked.
I read up some on the GC, and there is mention that the GC knows when to free memory by monitoring "active threads" but I don't know enough about threads to comment on how that part of the GC works.
This is a good description of the Garbage Collector.
DeletePass by value and Pass by reference were also mentioned. What does it mean to be pass by value? What does it mean to be pass by reference?
ReplyDeleteIn general "pass by value" means to pass an actual value (45 for an int) whereas "pass by reference" means to pass the location (or address or reference) of the item being passed. So instead of getting a 45 passed over, you'll get a reference to the original item itself. This can save lots of overhead for large items.
The discussion of call-by-value vs call-by-reference puts me in mind of this conversation between Alice and the White Knight (from "Through the Looking-Glass"):
ReplyDelete"Is it very long?" Alice asked, for she had heard a good deal of poetry that day.
"It's long," said the Knight, "but it's very, very beautiful. Everybody that hears me sing it--either it brings the tears into their eyes, or else--"
"Or else what?" said Alice, for the Knight had made a sudden pause.
"Or else it doesn't, you know. The name of the song is called 'Haddock's Eyes'."
"Oh, that's the name of the song, is it?" Alice said, trying to feel interested.
"No, you don't understand," the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Aged Man'."
"Then I ought to have said 'That's what the song is called?'" Alice corrected herself.
"No, you oughtn't: that's quite another thing! The song is called 'Ways and Means': but that's only what it's called, you know!"
"Well, what is the song, then?" said Alice, who was by this time completely bewildered.
"I was coming to that," the Knight said. "The song really is 'A-sitting on a Gate': and the tune's my own invention."
----
If Java actually featured "call by reference", then Joe's incrementor method would actually work properly, since the arguments passed in would actually refer to the variable i in the calling frame. Since they are passed by value instead, the value of i is instead copied into a new local variable. (Rob H already explained this well).
For an example where a reference variable is passed by value, think about the following:
public void blowUp ( CelestialBody planet ) {
if (planet != null) {
System.out.println( "Bye bye, " + planet.getName( ) );
planet = null;
}
}
when we invoke this method in the following code snippet:
moon.blowUp( );
System.out.println("Now, moon == " + moon );
what will happen?
Sorry for the bad indenting there. It looked better before blogger chewed it up.
DeleteAlso, I should have typed
blowUp( moon );
rather than
moon.blowUp ( );
No confusion intended.