Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Basic java question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic java question

    Hey guys
    I just started learning java, I am using the book Java software solutions and have a question.
    Dont know if any one has this book but might help, in chapter 3 creating objects it talks about initialization of strings page 115. I guess i dont get this because if you are useing the print method you dont use the new operator IE System.out.println (" Hi");
    but I think it is saying if I wanted to do String
    bruce = ("Hi bruce");
    System.out.println (bruce);
    I would have to initialize bruse with
    String bruse = new String ("Hi Bruse")

    I guess what im asking is to explain the new operator better, because with something like an INT you dont have to use new IE
    int bank = 43
    not
    int bank = new int (43)
    I hope all this makes sense but let me know if can figure out what the heck im talking about lol


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Basic java question

    It's a confusing feature of Java that people will argue over forever. The Java compiler has extra built-in support for String that it doesn't have for any other Object. "Hi Bruce" is a 'String literal' which Java treats as though you typed 'new String("Hi Bruce")'. String is probably the most used Object in any Java application - can you imagine if we had to write
    String sBruce = new String(new char[]{'H', 'i', ' ', 'B', 'r', 'u', 'c', 'e'});
    - how hard it would be to write even trivial programs?
    int, char, float and boolean are built-in types. Object references are also a built-in type, but to assign one you must first create an Object with the new operator. Except for String - because it's special. You can write 'new String("hi Bruce")' if you want, but nobody does because javac implicitly created the String object for you when it saw the string literal in your code.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic java question

    Oh ok, I get that thanks for the help

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic java question

    Or you could do this:

    String bruse = "Hi Bruse";

    instead of using new.

    Later edit: Actually I see that this was clarified in an earlier reply.. pls disregard

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Basic java question

    Java has a String Literal Pool. Whenever a String literal is in your code it will be placed into the pool (if it is not already there). If it is already there then your code will refer to that String rather than create a new one and waste space. Since Strings are immutable then it is ok and will not cause problems.
    String s1 = "Hello";
    String s2 = new String("Hello");
    On the first line the String "Hello" is placed in the literal pool and the variable s1 references it. For the secong line the String "Hello" is already in the literal pool so nothing happens but then a new Object is created and placed onto the heap. This new Object references the String "Hello" in the pool and then the variable s2 references the new Object. Therefore using the new operator will always create a new Object but just using a String Literal will only create a new Object if the String is not already in the pool.
    String s1 = "Hello";
    String s2 = new String("Hello");
    String s3 = "Hello";
    System.out.println(s1 == s3);
    System.out.println(s1 == s2);
    Hopefully you have been informed that using == to compare Strings can produce incorrect results and you should use the equals method instead. Well using == can be helpful in some instances. If you run the above code the output will be true and false. The first line will give true because s1 and s3 both reference the "Hello" String in the literal pool. Where as s2 references a completely different object.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Basic java question

    Just want to add to Junky explanation. As for as String object is concerned "equals" and "==" both are for different purpose. "equals" are used to compare the value in String object while "==" is used to compare the references.

Similar Threads

  1. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 07:21 PM
  2. please answer some basic question
    By togaurav in forum Java Theory & Questions
    Replies: 5
    Last Post: April 16th, 2011, 08:58 AM
  3. Basic Operational Question on Tomcat/Servlets
    By dottore11 in forum Java Servlet
    Replies: 1
    Last Post: December 24th, 2010, 04:11 AM
  4. [SOLVED] Asking what I suspect to be a very basic question
    By Noobert in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2010, 08:42 AM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 07:52 AM