By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

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

P: n/a
D E
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.
Jul 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
D E
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.

Jul 17 '05 #2

P: n/a
"D E" <no*****@nothing.com> wrote in message
news:F6*******************@nwrdny01.gnilink.net...
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.

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.
Jul 17 '05 #3

P: n/a
Ryan Stewart <zz********@gspamo.com> scribbled the following:
"D E" <no*****@nothing.com> wrote in message
news:F6*******************@nwrdny01.gnilink.net...
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.
>

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.


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Jul 17 '05 #4

P: n/a
"D E" <no*****@nothing.com> wrote in message
news:F6*******************@nwrdny01.gnilink.net...
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.


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

Jul 17 '05 #5

P: n/a
Tony Morris <di******@optusnet.com.au> scribbled the following:
"D E" <no*****@nothing.com> wrote in message
news:F6*******************@nwrdny01.gnilink.net...
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.

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.
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
The expression evaluates to false ?
Yes, it does. The output is unexpected ?
Well, to me, it's perfectly fine, what were you expecting ?
Expression evaluation and output are not defined for programs that do
not compile.
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>
This is true.
Without more information, speculative answers are all you'll get.


--
/-- Joona Palaste (pa*****@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
Jul 17 '05 #6

P: n/a
D E

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c7**********@oravannahka.helsinki.fi...
Ryan Stewart <zz********@gspamo.com> scribbled the following:
"D E" <no*****@nothing.com> wrote in message
news:F6*******************@nwrdny01.gnilink.net...
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.
> 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.


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


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse

Jul 17 '05 #7

P: n/a
> > 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.


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


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
Jul 17 '05 #8

P: n/a
"D E" <no*****@nothing.com> wrote in message news:<F6*******************@nwrdny01.gnilink.net>. ..
Ok this post was unclear... read below

"D E" <no*****@nothing.com> wrote in message
news:02*******************@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.


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.
Jul 17 '05 #9

P: n/a

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:
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.


Jul 17 '05 #10

P: n/a
D E

"perry" <pe***@unifiedobjects.com> wrote in message
news:qt********************@news20.bellglobal.com. ..

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
IT DOES... to the "T" ... thanks :)

- perry
D E wrote:
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.

Jul 17 '05 #11

P: n/a
perry wrote:
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.


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

Jul 17 '05 #12

P: n/a
perry wrote:

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");


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

The exact same result can be had in one line:

System.out.println("test1".equals("test1"));
Jul 17 '05 #13

P: n/a
> > String strComparer = "test1";

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


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

The exact same result can be had in one line:

System.out.println("test1".equals("test1"));


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

Jul 17 '05 #14

P: n/a
Tony Morris wrote:
The exact same result can be had in one line:

System.out.println("test1".equals("test1"));

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)));


Superfluous. the println() method is overloaded to do that for you.
Jul 17 '05 #15

P: n/a
a wink is as good as a node to a blind man...

- perry

Tony Morris wrote:
String strComparer = "test1";

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


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

The exact same result can be had in one line:

System.out.println("test1".equals("test1"));

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)));


Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.