The Artima Developer Community
Sponsored Link

Java Answers Forum
not logical to me =/

2 replies on 1 page. Most recent reply: Mar 31, 2003 12:43 AM by Roger

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Roger

Posts: 6
Nickname: lionheart
Registered: Mar, 2003

not logical to me =/ Posted: Mar 28, 2003 6:11 AM
Reply to this message Reply
Advertisement
hello everyone
i had been doing a program using java and hit this error that i don't know how to solve .. it seem logical to me but it just dun work .. i wrote some codes to show u all what i mean..


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Strange extends JFrame implements ActionListener
{
JTextField aTextField;
JButton aButton;

public Strange()
{
setVisible(true);
JPanel myPanel = new JPanel();
aTextField = new JTextField(20);
aButton = new JButton("enter");
aButton.addActionListener(this);
myPanel.add(aTextField);
myPanel.add(aButton);
pack();

getContentPane().add(myPanel);
pack();
}
public void actionPerformed(ActionEvent evt)
{
A a = new A();
if (evt.getSource() == aButton)
{
String textFieldValue = aTextField.getText();

if (textFieldValue == a.getx())
{
JOptionPane.showMessageDialog(null, "true!");
}
else
{
JOptionPane.showMessageDialog(null, "false?");
}


}
}



public static void main(String args[])
{
Strange strange = new Strange();

}
}

class A
{
String x = "abc";
String getx()
{
return x;
}
}


when i key in to the textfield abc, the dialog shows false
i really don't understand why, can anyone enlighten me
thanks!!


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: not logical to me =/ Posted: Mar 30, 2003 11:54 AM
Reply to this message Reply
If you replace
textFieldValue == a.getx()

by
textFieldValue.equals( a.getx() )
then you will true in your message box.== operator compares the references of two opererands. textFieldValue always contains reference to a new string object. I think you have read about string pool and you are thinking that since you are using "abc" literal inside class A, == should return true. You are getting false because of getText() method on text field is always returning new reference. To compare the content, as you intend, use equals() method instead of == operator.

Thanks
Kishori

Roger

Posts: 6
Nickname: lionheart
Registered: Mar, 2003

Re: not logical to me =/ Posted: Mar 31, 2003 12:43 AM
Reply to this message Reply
Ohhh ya it works .. thanks for the tutorial =)

Flat View: This topic has 2 replies on 1 page
Topic: How to call classes Previous Topic   Next Topic Topic: default provider of jtapi not found?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use