the question about output format

Discussion in 'Java' started by David, Jul 27, 2003.

  1. David

    David Guest

    class CompStr {
    public static void main(String[] args) {
    compare("first", "first");
    compare("first", "second");
    }
    static void compare(String a,String b) {
    System.out.println(a+"=="+b+": "+ a==b);
    System.out.println(a+"!="+b+": "+ a!=b);
    System.out.println(a+"="+b+": "+ a.equals(b));
    }
    }

    i write this code for testing string comparision, however, when i execute
    it,
    System.out.println(a+"!="+b+": "+ a!=b);
    System.out.println(a+"="+b+": "+ a.equals(b));
    just output its boolean value, why.

    the most interesting is when i change System.out.println(a+"=="+b+": "+
    a==b)//--output value is false--// into System.out.println(a==b),the output
    is true,

    why is that???


    --
    Bi Hongliang / David
    David, Jul 27, 2003
    #1
    1. Advertising

  2. "David" <> writes:

    > System.out.println(a+"!="+b+": "+ a!=b);
    > System.out.println(a+"="+b+": "+ a.equals(b));
    > just output its boolean value, why.


    Operator left-associativity. You are in effect comparing the string

    a+"!="+b+": "+a

    against the string

    b

    You need to put a parenthesis around the last expression, e.g.

    System.out.println(a+"!="+b+": "+ (a!=b));
    ^ ^
    Tor Iver Wilhelmsen, Jul 27, 2003
    #2
    1. Advertising

  3. David

    N.V.Dev Guest

    "David" <> wrote in message news:<>...
    > class CompStr {
    > public static void main(String[] args) {
    > compare("first", "first");
    > compare("first", "second");
    > }
    > static void compare(String a,String b) {
    > System.out.println(a+"=="+b+": "+ a==b);
    > System.out.println(a+"!="+b+": "+ a!=b);
    > System.out.println(a+"="+b+": "+ a.equals(b));
    > }
    > }
    >
    > i write this code for testing string comparision, however, when i execute
    > it,
    > System.out.println(a+"!="+b+": "+ a!=b);
    > System.out.println(a+"="+b+": "+ a.equals(b));
    > just output its boolean value, why.
    >
    > the most interesting is when i change System.out.println(a+"=="+b+": "+
    > a==b)//--output value is false--// into System.out.println(a==b),the output
    > is true,
    >
    > why is that???



    Well, as far as my understanding goes, below are the details.

    a) when object is compared with another object then it is compared not
    by value but by reference. Since, reference/address cannot be same, it
    returns 'false'

    which is a=b

    when this done S.o.p() actually works on the basis of rule of
    precedence.
    remember right to left..... so, when compared, it is false and only
    *boolean* is returned.

    b) the same when changed from

    System.out.println(a+"=="+b+": "+ a==b); to

    System.out.println(a+"=="+b+": "+ a.equals(b) );

    would print

    first==first: true

    NOTE: even if values of a = "first" and b = "fIrst" it would
    display
    first==fIrst: false


    Any comments is appreciated.

    Thanks in advance!
    N.V.Dev, Jul 27, 2003
    #3
  4. David

    Roedy Green Guest

    Roedy Green, Jul 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,137
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,236
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,575
  4. shannon
    Replies:
    0
    Views:
    554
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    450
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page