Displaying a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmee
    New Member
    • Mar 2010
    • 39

    Displaying a string

    here is my code i dont understand why it doesnt print me anything.....
    Code:
    class StringDemo
    {
    public static void main(String[] args)
    {
    String s1,s2,s3,output;
    
    s1=new String("hello");
    s2=new String("hello guys");
    s3=new String("happy birthday");
    
    output="s1 = " +s1+ "\ns2 = " +s2+  "\ns3" +s3+;
    
    }
    }
    help me not getting s1,s2,s3
  • justwantananswer
    New Member
    • Feb 2010
    • 3

    #2
    You code just created four String objects and did nothing more. If you want to print something, you need to use System.out.prin tln(). i.e.

    System.out.prin tln(ouput);

    By the way, do not write the following code:

    s1 = new String("hello") ;

    Although it works, it is not a good practice. Just write the following is enough:

    s1 = "hello";

    Try to read articles about "immutable" , "string constant", etc.

    Comment

    • ahmee
      New Member
      • Mar 2010
      • 39

      #3
      i also use system.out.prin tln method but it give error that + operator not recognized

      Comment

      • rethab
        New Member
        • Apr 2010
        • 3

        #4
        the last character on the line where you initialize 'output' is a +. remove it!

        Comment

        Working...