Connecting Tech Pros Worldwide Help | Site Map

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

D E
Guest
 
Posts: n/a
#1: Jul 18 '05
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.


D E
Guest
 
Posts: n/a
#2: Jul 18 '05

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]


Ryan Stewart
Guest
 
Posts: n/a
#3: Jul 18 '05

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.


Joona I Palaste
Guest
 
Posts: n/a
#4: Jul 18 '05

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
Tony Morris
Guest
 
Posts: n/a
#5: Jul 18 '05

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



Joona I Palaste
Guest
 
Posts: n/a
#6: Jul 18 '05

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
D E
Guest
 
Posts: n/a
#7: Jul 18 '05

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]


Tony Morris
Guest
 
Posts: n/a
#8: Jul 18 '05

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


Hylander
Guest
 
Posts: n/a
#9: Jul 18 '05

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.
perry
Guest
 
Posts: n/a
#10: Jul 18 '05

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]

D E
Guest
 
Posts: n/a
#11: Jul 18 '05

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]


Neal Gafter
Guest
 
Posts: n/a
#12: Jul 18 '05

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.

Michael Borgwardt
Guest
 
Posts: n/a
#13: Jul 18 '05

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"));
Tony Morris
Guest
 
Posts: n/a
#14: Jul 18 '05

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



Michael Borgwardt
Guest
 
Posts: n/a
#15: Jul 18 '05

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.
perry
Guest
 
Posts: n/a
#16: Jul 18 '05

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