Even simpler Java question

Discussion in 'OT Technology' started by HardTech, Apr 14, 2006.

  1. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    What's the difference between:

    String temp = new String();

    and

    String temp;
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    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?
     
  3. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    My instructor says:

    String temp = new String();

    is just the same as typing

    String temp = "";

    but yeah, your explanation is clear :)
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,409
    Likes Received:
    591
    More specifically,

    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.
     
  5. t b

    t b This is almost as fun as playing with the speech f

    Joined:
    Aug 9, 2004
    Messages:
    998
    Likes Received:
    0
    Location:
    Memphis, TN
    your instructor said that? :ugh2:
     
  6. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    well, here's what he said verbatim

     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    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.
     
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,409
    Likes Received:
    591
    C/C++ allow the same statement.

    char a[25]="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.
     
  9. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Why is the instructor covering syntax anyway :)
     
  10. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    he's not

    I was curious, so I asked him
     
  11. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    C++ isn't a good first language either. No OO language is.

    char a[25] = "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.
     
  12. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,409
    Likes Received:
    591
    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. :o
     
  13. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    java was my first language and i learned fine :dunno:
     
  14. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    :werd:

    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
     
  15. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    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.

    </ivory tower>
     
  16. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    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?
     
  17. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    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).
     
  18. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,409
    Likes Received:
    591
    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.
     

Share This Page