Discussion in 'OT Technology' started by HardTech, Apr 14, 2006.
What's the difference between:
String temp = new String();
String temp = new String(); initializes the variable temp to be a new string, while String temp; only declares the variable.
Initialization is when the constructor is called and memory for the String is allocated. temp is essentially a pointer to a String object, and if you just declare String temp, then the pointer temp is null. If you initialize it, it points to a String.
Is that clear?
My instructor says:
String temp = new String();
is just the same as typing
String temp = "";
but yeah, your explanation is clear
String temp; declares a reference to a string. It's in essence an uninitialized pointer, waiting to be assigned to an object.
temp = new String(); creates a new String object and assigns its address to temp, the reference you created before.
String temp = new String(); does both things in one step.
your instructor said that?
well, here's what he said verbatim
That's true, but only because Java provides String a = "foo"; as a shortcut for String a = new String("foo").
This is why Java is a fucking terrible thing to teach as a first language.
C/C++ allow the same statement.
char a="Hi there"; //both C and C++
String a="This is a test"; //only in C++
String a("This is a test"); //only in C++
are all legal.
Why is the instructor covering syntax anyway
I was curious, so I asked him
C++ isn't a good first language either. No OO language is.
char a = "Hi there"; is very clear in what it does. It creates a statically-allocated array of 25 characters, and assigns the first 9 characters to be "Hi there" plus a null-terminator. This is in no way the same as creating a String object with an initial value of "Hi there". It doesn't create an object, no constructors are called, and very little goes on behind the scenes.
Actually it does create an object and it still allocates an array of 25 characters, etc.
String a("This is a test");
calls one of the several overloaded String class constructors - the one that takes a char* as a single argument.
It does all the same things - it just does some more. Loads some functions onto the heap and allocates some space for data on the stack. The basic mechanics are the same though.
And I hate to disagree with you, but I'm starting to think Java is a pretty good introductory language.
java was my first language and i learned fine
I learned Java before anything else
of course, I failed and switched majors, then had to learn VB, but I'm now primarily a java guy
And therein lies the problem. String a = "foo"; LOOKS like it just creates a char array (or some structure) with foo in it. The reality is, it calls a function (the constructor). This is a massive overhead (well, massive compared to the tiny overhead of just creating a char array), and people who learn Java/C++ as their first/only language may never realize this.
Now, I'm not saying that Java/C++ shouldn't have these features, they're really nice and make life easier when you're writing stuff in them. I guess if all you want to do is write little programs to do little things on your beastly 3GHz computer, then learning Java/C++ and disregarding these inefficiencies is just fine. However, if you want to know what's going on underneath (which I believe is a good thing for programmers to know), a super-high-level OO language like Java or C++ is not a good choice as a teaching language. They obscure too much of what really happens.
But, to each his own. I don't need to convince anyone on here that Java is a bad first language, I'll spend my time and energy convincing my own department of it instead. Just my two cents.
so to somebody who DID learn Java as a first language, what do you recommend for them to do in order to learn what goes on underneath?
To get a better understanding of how memory allocation and things work, C is a good language to learn. A lot of things that are done for you and that you never have to think about in Java (memory allocation, garbage collection, bounds checking, etc) aren't done by default in C, so you have to do them yourself.
To get a real understanding of what goes on, it's interesting to take a course in architecture and learn assembly language. It's not a very practical thing to know for most people, but it really gives you an idea of what happens when you run your code. It also really makes you appreciate that you have a compiler to generate your assembly for you, since compiling by hand is a lot less fun (although very interesting).
Want to know a secret? I've learned a whole lot more about how memory is handled since I started OO programming than I knew back when all I knew was C and Perl.
Granted a large part of it is just because I've learned more than I had before, not BECAUSE it was Java & C++. But my point is that it doesn't at all prevent you from learning what goes on "under the hood". Maybe I just had good instructors.
PS I do recomment taking a class or 2 in any language you want to learn. You can be taught more than you can teach yourself.