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

D

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

D E

Ok this post was unclear... read below

D E said:
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.
 
R

Ryan Stewart

D E said:
Ok this post was unclear... read below


.....
"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.
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.
 
J

Joona I Palaste

Ryan Stewart said:
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.
 
T

Tony Morris

D E said:
Ok this post was unclear... read below


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

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
 
J

Joona I Palaste

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

Tony Morris

The term "won't work", "doesn't work", etc. etc. has no meaning when it
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
 
D

D E

Joona I Palaste said:
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"
 
H

Hylander

D E said:
Ok this post was unclear... read below


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

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

perry

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

D E

perry said:
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 :)
 
N

Neal Gafter

perry said:
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.
 
M

Michael Borgwardt

perry said:
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"));
 
T

Tony Morris

String strComparer = "test1";
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(strComparer)));

--
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
 
M

Michael Borgwardt

Tony said:
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(strComparer)));

Superfluous. the println() method is overloaded to do that for you.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top