We talked about the new and improved GuessingGame.java and GGFrame.java. Make sure to look over these in the repository. There are some tricky bits in these so it is okay if you don't understand *everything* but do your best and post questions.
Topics for Discussion:
In class, we decided it was better to separate the GUI code from the GuessingGame code. Why is this a good idea? Name a few reasons and explain them in a one or two sentences.
You will notice that GuessingGame extends Object. What does this mean? What would change if we removed the extends Object portion of this declaration?
We saw a StringBuilder on line 65 of GuessingGame.java. We used this object to create a String. Why do we use this instead of concatenating Strings together using +=?
The term immutable came up in the discussion today. What does this term mean? Why is the String object immutable in Java? How do we get around String being immutable?
Look at the code here: http://pastebin.com/42arBK9j
It prints two booleans. Can you guess what they are? After guessing, compile and run. Were you correct? What is going on here?
Look at the GuessingGame files in the lecture10 package. What additional improvements can we make?
See you Friday!
Joe
We use StringBuilder because the data inside the object can be change and each time 'add' strings together the compiler makes a object for it. So if you had a string called s and it referred to "Alex" and you want to add " Moon" to it there would be two objects that has a memory contianing "Alex" and another contianing "Alex Moon". A StringBuilder will allow you to change the data inside the object.
ReplyDeletethe stringbuilder class is more suitalbe to modifing strings than the string class itself. string are inmutealbe making it more time consuming to modify a string. whereas string builder is mutealbe and thus taking less time and memory to manipulate strings.
ReplyDeleteBurton, I think you have the right idea but your wording is very unclear. First you say a String is immutable but then you say it is time consuming to modify it. However, those two ideas contradict themselves. Try again, this time really try to be clear with your answer.
DeleteExtends means that a class that extends another class inherits (not sure if I am using that term correctly) the extended classes methods and other features. If we removed the extended from the declaration we wouldn't be able to use the methods and constructors from the the extended class.
ReplyDeleteWe use StringBuilder so we can change the actual string stored in memory instead of creating a new string in memory every time we change the string.
Immutable means an object cannot be changed or modified after it is created. When you concatenate a string in java you just create a new string in memory, the original string is still there.
You're right that subclasses inherit the (fields and) methods and constructors from the parent class. The other big feature of a subclass is that objects from the subclass can be used in any piece of code that will work with an object from the parent class.
DeleteSo if, for example, class RaceCar extends Car, then a method such as:
Garage.tuneUp( Car c )
can be used on any RaceCar too.
Look at the code here: http://pastebin.com/42arBK9j
ReplyDeleteIt prints two booleans. Can you guess what they are? After guessing, compile and run. Were you correct? What is going on here?
The first boolean it will print is true, followed by false. What's happening is that "==" compares the memory location of the two variables, not their value. In the first line a variable is created with a value of "Hello World". In the second line a second variable is created and assigned the same value. Instead of creating a completely separate variable, java simply assigns the memory location of the first variable to the second. They point to the same thing in memory, therefore have the same memory location assigned to them and "==" returns true. In line 9 the String constructor is called and creates a completely new and separate variable. Now, even though they have the same characters, the variables do not point to the same memory location and the "==" will return a false.
This is correct.
DeleteHow do we get the desired result on line 10?
Use the string method equals() to evaluate what characters are contained in the string. On line 10 if you use hello.equals(world) would return true.
DeleteThis comment has been removed by the author.
ReplyDeleteThe string builder lets you take up less memory than just concatenating many strings on top of each other. For small programs I would think that it doesn't make too much a difference, but if you have several large programs running and space is at a premium then the String Builder would let you free up memory for other uses.
ReplyDeleteYour first sentence is correct. However, I don't quite believe your justification. You should generally use this anytime you want to concatenate any substantial amount of text together.
DeleteJust found this article explaining why String is immutable:
ReplyDeletehttp://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html