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: Constructing Strings

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructing Strings

    Hello,

    This probably seems simple, well I'm sure all of mine seem simple, so here it goes. I am having trouble constructing strings. str1, str3, and str4 give pre-compile errors and show up yellow in my IDE. The pre-compile errors are commented in the code below. str2 gives a pre-compile error (commented in code below) and also a runtime error. The runtime error was:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at hsstringdemo.HSStringDemo.main(HSStringDemo.java:1 4)

    Line 14 is where the string is declared and initialized, which I think is also called the string constructor.

    I imported java.lang.Object and java.lang.String in hopes that something was not defined, but to no avail. I tried using the code tags this time, seems simple, but tell me how I am doing.

    package hsstringdemo;
    import java.lang.Object;
    import java.lang.String;
     
    public class HSStringDemo {
        public static void main(String[] args) {
            // Declare strings in various ways.
            String str1 = new String("Java strings are objects."); //Pre-Compile Error: String constructor invocation
            String str2 = new "They are constructed in various ways."; //Pre-Compile Error: <identifier> expected
            String str3 = new String(str1); //Pre-Compile Error: String constructor invocation
            String str4 = new String(str1 + str3); //Pre-Compile Error: String constructor invocation
     
            System.out.println(str1);
            System.out.println(str2);
            System.out.println(str3);
            System.out.println(str4);
     
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Constructing Strings

    The only error in your code is for str2. If you remove the "new" keyword the code will compile and run.
    Improving the world one idiot at a time!

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

    Default Re: Constructing Strings

    You don't need to construct String objects, Java has built-in support for some common String operations - "Hello World" is already a String object. You can for example do "Hello World".indexOf("W") to get the int return value 6. 'String hw = "Hello World";' (probably) instantiates a String object and assigns it to the String reference hw. The reason your IDE is warning you may have something to do with String interning - an obscure feature of Java which is almost always a VeryGoodThing™ - just don't use the String constructor (I never have, other than on byte arrays) and you'll be fine.

    The class files from java.lang are imported by default, so there's no value in doing it explicitly. Java can also concatenate Strings with '+' to create new Strings - there's no need to use a constructor there either.
    Last edited by Sean4u; August 5th, 2011 at 06:13 AM. Reason: not much

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructing Strings

    Thank you everyone for the information.

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

    Default Re: Constructing Strings

    You can check here for all String related Details Blog Archive Java String Basic

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Constructing Strings

    Your a little late with your answer. The question was asked a month ago.

Similar Threads

  1. separate strings
    By ridg18 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 21st, 2011, 05:22 AM
  2. I need help! Re: strings, nextLine, while and more..
    By rockerade in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 5th, 2010, 03:51 PM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 09:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM