Connecting Tech Pros Worldwide Help | Site Map

if ("test1".equals(strComparer) ) then

  #1  
Old July 18th, 2005, 12:10 AM
D E
Guest
 
Posts: n/a
Ok the subject line is basically the question. Obviously this won't work.
What is the technical reason?

Is it because here, "test1" is sort of like a static object now?

What exactly is "test1" in this case?

Thanks.


  #2  
Old July 18th, 2005, 12:10 AM
D E
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


Ok this post was unclear... read below

"D E" <nothing@nothing.com> wrote in message
news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=blue]
> Ok the subject line is basically the question. Obviously this[/color]
......
"this" being this line of code:

//begin code segment
String strComparer = "test2";
if ("test1".equals(strComparer) ) then
System.out.println("strComparer should output \"test1\"");
//end code segment

won't work.[color=blue]
> What is the technical reason?
>
> Is it because here, "test1" is sort of like a static object now?
>
> What exactly is "test1" in this case?
>
> Thanks.
>
>[/color]


  #3  
Old July 18th, 2005, 12:10 AM
Ryan Stewart
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


"D E" <nothing@nothing.com> wrote in message
news:F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net...[color=blue]
> Ok this post was unclear... read below
>
> "D E" <nothing@nothing.com> wrote in message
> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=green]
> > Ok the subject line is basically the question. Obviously this[/color]
> .....
> "this" being this line of code:
>
> //begin code segment
> String strComparer = "test2";
> if ("test1".equals(strComparer) ) then
> System.out.println("strComparer should output \"test1\"");
> //end code segment
>
> won't work.[color=green]
> > What is the technical reason?
> >
> > Is it because here, "test1" is sort of like a static object now?
> >
> > What exactly is "test1" in this case?
> >
> > Thanks.
> >[/color][/color]
The "technical reason" that "test1" and "test2" are not equal is because
they're composed of different characters. Both "test1" and "test2" are
instances of java.lang.String. They're called string literals.


  #4  
Old July 18th, 2005, 12:10 AM
Joona I Palaste
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


Ryan Stewart <zzanNOtozz@gspamo.com> scribbled the following:[color=blue]
> "D E" <nothing@nothing.com> wrote in message
> news:F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net...[color=green]
>> Ok this post was unclear... read below
>>
>> "D E" <nothing@nothing.com> wrote in message
>> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=darkred]
>> > Ok the subject line is basically the question. Obviously this[/color]
>> .....
>> "this" being this line of code:
>>
>> //begin code segment
>> String strComparer = "test2";
>> if ("test1".equals(strComparer) ) then
>> System.out.println("strComparer should output \"test1\"");
>> //end code segment
>>
>> won't work.[color=darkred]
>> > What is the technical reason?
>> >
>> > Is it because here, "test1" is sort of like a static object now?
>> >
>> > What exactly is "test1" in this case?
>> >
>> > Thanks.
>> >[/color][/color]
> The "technical reason" that "test1" and "test2" are not equal is because
> they're composed of different characters. Both "test1" and "test2" are
> instances of java.lang.String. They're called string literals.[/color]

How can it "work" or "not work" when it won't even compile? The word
"then" causes a syntax error.

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
  #5  
Old July 18th, 2005, 12:10 AM
Tony Morris
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


"D E" <nothing@nothing.com> wrote in message
news:F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net...[color=blue]
> Ok this post was unclear... read below
>
> "D E" <nothing@nothing.com> wrote in message
> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=green]
> > Ok the subject line is basically the question. Obviously this[/color]
> .....
> "this" being this line of code:
>
> //begin code segment
> String strComparer = "test2";
> if ("test1".equals(strComparer) ) then
> System.out.println("strComparer should output \"test1\"");
> //end code segment
>
> won't work.[color=green]
> > What is the technical reason?
> >
> > Is it because here, "test1" is sort of like a static object now?
> >
> > What exactly is "test1" in this case?
> >
> > Thanks.[/color][/color]

The term "won't work", "doesn't work", etc. etc. has no meaning when it
comes to problem resolution.
So, I will speculate (since it is forced by your lack of contextual
information).

It doesn't compile ?
Incorrect, yes it does.

The expression evaluates to false ?
Yes, it does.

The output is unexpected ?
Well, to me, it's perfectly fine, what were you expecting ?

Java doesn't work ?
<sarcasm>Oh, you found the bug in boolean expressions giving ad hoc
behaviour that all of use millions have spent years looking for</sarcasm>

Without more information, speculative answers are all you'll get.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform



  #6  
Old July 18th, 2005, 12:10 AM
Joona I Palaste
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


Tony Morris <dibblego@optusnet.com.au> scribbled the following:[color=blue]
> "D E" <nothing@nothing.com> wrote in message
> news:F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net...[color=green]
>> Ok this post was unclear... read below
>>
>> "D E" <nothing@nothing.com> wrote in message
>> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=darkred]
>> > Ok the subject line is basically the question. Obviously this[/color]
>> .....
>> "this" being this line of code:
>>
>> //begin code segment
>> String strComparer = "test2";
>> if ("test1".equals(strComparer) ) then
>> System.out.println("strComparer should output \"test1\"");
>> //end code segment
>>
>> won't work.[color=darkred]
>> > What is the technical reason?
>> >
>> > Is it because here, "test1" is sort of like a static object now?
>> >
>> > What exactly is "test1" in this case?
>> >
>> > Thanks.[/color][/color][/color]
[color=blue]
> The term "won't work", "doesn't work", etc. etc. has no meaning when it
> comes to problem resolution.
> So, I will speculate (since it is forced by your lack of contextual
> information).[/color]
[color=blue]
> It doesn't compile ?
> Incorrect, yes it does.[/color]

No it won't. Have you tried to compile it? I have.

[joona@teletran-1 joona]$ cat test.java
public class test {
public void method() {
//begin code segment
String strComparer = "test2";
if ("test1".equals(strComparer) ) then
System.out.println("strComparer should output \"test1\"");
//end code segment
}
}
[joona@teletran-1 joona]$ javac test.java
test.java:5: not a statement
if ("test1".equals(strComparer) ) then
^
test.java:6: ';' expected
System.out.println("strComparer should output \"test1\"");
^
2 errors
[color=blue]
> The expression evaluates to false ?
> Yes, it does.[/color]
[color=blue]
> The output is unexpected ?
> Well, to me, it's perfectly fine, what were you expecting ?[/color]

Expression evaluation and output are not defined for programs that do
not compile.
[color=blue]
> Java doesn't work ?
> <sarcasm>Oh, you found the bug in boolean expressions giving ad hoc
> behaviour that all of use millions have spent years looking for</sarcasm>[/color]

This is true.
[color=blue]
> Without more information, speculative answers are all you'll get.[/color]

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
  #7  
Old July 18th, 2005, 12:10 AM
D E
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then



"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:c7phk9$t6a$1@oravannahka.helsinki.fi...[color=blue]
> Ryan Stewart <zzanNOtozz@gspamo.com> scribbled the following:[color=green]
> > "D E" <nothing@nothing.com> wrote in message
> > news:F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net...[color=darkred]
> >> Ok this post was unclear... read below
> >>
> >> "D E" <nothing@nothing.com> wrote in message
> >> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...
> >> > Ok the subject line is basically the question. Obviously this
> >> .....
> >> "this" being this line of code:
> >>
> >> //begin code segment
> >> String strComparer = "test2";
> >> if ("test1".equals(strComparer) ) then
> >> System.out.println("strComparer should output \"test1\"");
> >> //end code segment
> >>
> >> won't work.
> >> > What is the technical reason?
> >> >
> >> > Is it because here, "test1" is sort of like a static object now?
> >> >
> >> > What exactly is "test1" in this case?
> >> >
> >> > Thanks.
> >> >[/color]
> > The "technical reason" that "test1" and "test2" are not equal is because
> > they're composed of different characters. Both "test1" and "test2" are
> > instances of java.lang.String. They're called string literals.[/color]
>
> How can it "work" or "not work" when it won't even compile? The word
> "then" causes a syntax error.
>[/color]

OOPS... vb stuff there... yeah.. omit "then"

[color=blue]
> --
> /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
> \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
> "B-but Angus! You're a dragon!"
> - Mickey Mouse[/color]


  #8  
Old July 18th, 2005, 12:10 AM
Tony Morris
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


> > The term "won't work", "doesn't work", etc. etc. has no meaning when it[color=blue][color=green]
> > comes to problem resolution.
> > So, I will speculate (since it is forced by your lack of contextual
> > information).[/color]
>[color=green]
> > It doesn't compile ?
> > Incorrect, yes it does.[/color]
>
> No it won't. Have you tried to compile it? I have.[/color]

doh!

Remove 'then'

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


  #9  
Old July 18th, 2005, 12:10 AM
Hylander
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


"D E" <nothing@nothing.com> wrote in message news:<F6Xnc.25872$vz5.23303@nwrdny01.gnilink.net>. ..[color=blue]
> Ok this post was unclear... read below
>
> "D E" <nothing@nothing.com> wrote in message
> news:02Xnc.25871$vz5.17045@nwrdny01.gnilink.net...[color=green]
> > Ok the subject line is basically the question. Obviously this[/color]
> .....
> "this" being this line of code:
>
> //begin code segment
> String strComparer = "test2";
> if ("test1".equals(strComparer) ) then
> System.out.println("strComparer should output \"test1\"");
> //end code segment
>
> won't work.[color=green]
> > What is the technical reason?
> >
> > Is it because here, "test1" is sort of like a static object now?
> >
> > What exactly is "test1" in this case?
> >
> > Thanks.
> >
> >[/color][/color]

Perhaps he is also getting a syntax error on the word "then".
Also, there is a metaphorical issue with strComparer. Typically you
have a Comparator. I'm assuming this is yet another VB programmer
getting paid to do java.
  #10  
Old July 18th, 2005, 12:11 AM
perry
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then



well, i'm not sure what your asking, but the java compiler being the
highly efficient animal that it is accepts:

String strComparer = "test1";

if ("test1".equals(strComparer))
System.out.println("true");
else
System.out.println("false");

just as it is (and therefore WILL work).... it looks at "test1" as a
string constant and therefore entitled to whatever methods are available
for that class.

in java, string constants can be considered static given that the
objects of the String class are "immutable" as in, they cannot be
changed directly. to have mutable string buffers that you are more
familiar with in C++ you can use the StringBuffer class.

i hope this addresses your question

- perry


D E wrote:[color=blue]
> Ok the subject line is basically the question. Obviously this won't work.
> What is the technical reason?
>
> Is it because here, "test1" is sort of like a static object now?
>
> What exactly is "test1" in this case?
>
> Thanks.
>
>[/color]

  #11  
Old July 18th, 2005, 12:11 AM
D E
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then



"perry" <perry@unifiedobjects.com> wrote in message
news:qtxoc.24981$dr1.645891@news20.bellglobal.com. ..[color=blue]
>
> well, i'm not sure what your asking, but the java compiler being the
> highly efficient animal that it is accepts:
>
> String strComparer = "test1";
>
> if ("test1".equals(strComparer))
> System.out.println("true");
> else
> System.out.println("false");
>
> just as it is (and therefore WILL work).... it looks at "test1" as a
> string constant and therefore entitled to whatever methods are available
> for that class.
>
> in java, string constants can be considered static given that the
> objects of the String class are "immutable" as in, they cannot be
> changed directly. to have mutable string buffers that you are more
> familiar with in C++ you can use the StringBuffer class.
>
> i hope this addresses your question[/color]

IT DOES... to the "T" ... thanks :)
[color=blue]
>
> - perry
>
>
> D E wrote:[color=green]
> > Ok the subject line is basically the question. Obviously this won't[/color][/color]
work.[color=blue][color=green]
> > What is the technical reason?
> >
> > Is it because here, "test1" is sort of like a static object now?
> >
> > What exactly is "test1" in this case?
> >
> > Thanks.
> >
> >[/color]
>[/color]


  #12  
Old July 18th, 2005, 12:11 AM
Neal Gafter
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


perry wrote:[color=blue]
> in java, string constants can be considered static given that the
> objects of the String class are "immutable" as in, they cannot be
> changed directly. to have mutable string buffers that you are more
> familiar with in C++ you can use the StringBuffer class.[/color]

Better yet, if you don't need the synchronization built into StringBuffer, use
StringBuilder from JDK 1.5.

  #13  
Old July 18th, 2005, 12:11 AM
Michael Borgwardt
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


perry wrote:[color=blue]
>
> well, i'm not sure what your asking, but the java compiler being the
> highly efficient animal that it is accepts:
>
> String strComparer = "test1";
>
> if ("test1".equals(strComparer))
> System.out.println("true");
> else
> System.out.println("false");[/color]

A nit! A nit! let's pick it!!

The exact same result can be had in one line:

System.out.println("test1".equals("test1"));
  #14  
Old July 18th, 2005, 12:11 AM
Tony Morris
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


> > String strComparer = "test1";[color=blue][color=green]
> >
> > if ("test1".equals(strComparer))
> > System.out.println("true");
> > else
> > System.out.println("false");[/color]
>
> A nit! A nit! let's pick it!!
>
> The exact same result can be had in one line:
>
> System.out.println("test1".equals("test1"));[/color]

I see a nit!
"true" is not the same as true.

EXACT same result can be had with:

System.out.println(String.valueOf("test1".equals(s trComparer)));

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform



  #15  
Old July 18th, 2005, 12:11 AM
Michael Borgwardt
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


Tony Morris wrote:
[color=blue][color=green]
>>The exact same result can be had in one line:
>>
>>System.out.println("test1".equals("test1"));[/color]
>
>
> I see a nit!
> "true" is not the same as true.
>
> EXACT same result can be had with:
>
> System.out.println(String.valueOf("test1".equals(s trComparer)));[/color]

Superfluous. the println() method is overloaded to do that for you.
  #16  
Old July 18th, 2005, 12:11 AM
perry
Guest
 
Posts: n/a

re: if ("test1".equals(strComparer) ) then


a wink is as good as a node to a blind man...

- perry

Tony Morris wrote:[color=blue][color=green][color=darkred]
>>> String strComparer = "test1";
>>>
>>> if ("test1".equals(strComparer))
>>> System.out.println("true");
>>> else
>>> System.out.println("false");[/color]
>>
>>A nit! A nit! let's pick it!!
>>
>>The exact same result can be had in one line:
>>
>>System.out.println("test1".equals("test1"));[/color]
>
>
> I see a nit!
> "true" is not the same as true.
>
> EXACT same result can be had with:
>
> System.out.println(String.valueOf("test1".equals(s trComparer)));
>[/color]

Closed Thread