views:

75

answers:

4
class MyStringBuffer {
    //TODO explain: why you would need these data members.
    private char[] chars; //character storage.
    private int length;   //number of characters used    

public String toString(){
        //TODO

        //Hint: just construct a new String from the ‘chars’ data member
        //and return this new String – See API online for how create
        //new String from char[]

This is only part of the code, I didn't want to post the whole thing. I just to focus on this and then then move on when I fully understand it.

1) What does he mean when he said construct a new String from the 'chars' data member? I'm confused as to what he wants me to do. Am I suppose to do something with char charAt(int index)? or something like this: StringBuffer sb = new StringBuffer("test");?

2) Does construct and create mean the same thing in Java?

A: 

As he says, have a look at the API, where you will find a String constructor that takes an array of characters: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#String%28char[]%29 .

Zarkonnen
@Zarkonnen Hi, I think I am suppose to use this. public String(char[] value), but it doesn't seem to explain how to use it. When I just copy and paste it in there, it says illegal start of expression.Sorry if it is a dumb question, I am just trying to learn.
CuriousStudent
`public String(char[] value)` is a String constructor, so you'd call it like `new String(value)`.
cHao
+1  A: 

You need an array of chars because, well, you have to store the characters somewhere. You could store them in a String but then you would have little need of a StringBuffer, whose raison d'etre is to be more efficient than a String (you generaly use the more efficient StringBuffer to construct a string, then use .toString() to get a real String object).

The length would be used to specify the length of the StringBuffer. You may thing you could just use the length of the character array but it's actually more efficient, when reducing the size of the StringBuffer, to not re-allocate a smaller array, especially if you may just end up needing more anyway.

The String constructor you would need for this scheme would be String (char[] value, int offset, int count) using an offset of zero and a count of length. This would allow you to use only the desired subset of the char array.

paxdiablo
I see I see, that makes sense. Thanks!
CuriousStudent
+1  A: 

1) The easy way to create a string from chars:

String result = new String(chars, 0, length);

Note, you probably can't just use the String(char[]) constructor, because the existence of the length member implies that some elements of chars may not be used. If you don't specify what elements to use, you could end up with extra chars (maybe NULs, maybe garbage) at the end of the string.

2) Not exactly. While the object is created (the memory for the object itself allocated) at the time the constructor's called, at that time it's just a blob of memory. The object is not fully constructed (read: any class invariants may not necessarily hold) until the constructor returns. This means the difference between the two only applies in the constructor, though, so it's common for people to use "create" and "construct" almost interchangeably.

cHao
@cHao Hi, to return it, is simply this? return result.toString();
CuriousStudent
@CuriousStudent: Even simpler. Once you have the string, you can just `return result;`.
cHao
@cHao oh okay, just curious but does it matter if i went with return result.toString();? im going to change it to return result; but im just wondering
CuriousStudent
@CuriousStudent: It shouldn't matter. `result.toString()` would be equal to `result` (and, in any decent class library, would even be the same object). However, `someString.toString()` obviously comes from someone who's just learning about strings and such. It involves a needless method call.
cHao
@cHao My next part is --public MyStringBuffer append (char c){-- so he wants me to add C to my string?
CuriousStudent
He wants you to write the code that appends a char to the end of the string buffer, yes.
cHao
+2  A: 

What does he mean when he said construct a new String from the 'chars' data member? I'm confused as to what he wants me to do. Am I suppose to do something with char charAt(int index)? or something like this: StringBuffer sb = new StringBuffer("test");?

Do either of those things create a String? (Hint: no)

How do you create an instance of a particular class in Java? (Hint: review your notes on new. Hint 2: look at the javadocs for the java.lang.String class.).

Does construct and create mean the same thing in Java?

Java objects are created by a constructor, so it is reasonable to use "construct" and "create" interchangeably.

(Technically they don't mean exactly the same thing. An object is created by allocating its memory and calling a class constructor. So if you are being pedantic, constructing is part of the process of creating an object. But there's no need to be pedantic.)

Stephen C