I can save the choice while I am using the app, but whenever I close the app and restart it, the choices are empty again. Where am I going wrong? It is always loading the default "0" instead of remembering the last selection.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
SharedPreferences choiceSettings = getSharedPreferences("currentChoice", 0);
final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};
final CharSequence[] items = {"AT&T", "Tmobile", "Verizon", "Sprint", "Other"};
// Decide which carrier, so we can apply the correct forwarding code.
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select your carrier");
builder.setIcon(R.drawable.ic_launcher);
builder.setSingleChoiceItems(items, currentChoice[0],
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
// TODO Auto-generated method stub
switch (item) {
case 0:
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
// Your code when first option seletced
currentChoice[0] = 0;
editor.putInt(String.valueOf(currentChoice[0]), 0);
editor.putString("fCode", "*67*");
editor.apply();
break;
case 1:
// Your code when 2nd option seletced
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
currentChoice[0] = 1;
editor.putInt(String.valueOf(currentChoice[0]), 0);
editor.putString("fCode", "*67*");
editor.apply();
break;
case 2:
// Your code when 2nd option seletced
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
currentChoice[0] = 2;
editor.putInt(String.valueOf(currentChoice[0]), 0);
editor.putString("fCode", "*67*");
editor.apply();
break;
}
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
In the beginning (line 4), you try to load a variable named "currentChoice":
final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};
In general you save a variable with editor.putInt(String key, int value). So key is the name used for saving your variable.
When you write
currentChoice[0] = 0;
editor.putInt(String.valueOf(currentChoice[0]), 0);
String.valueOf(currentChoice[0]) becomes "0". You save an int 0 to a variable named "0".
So change the second line to
editor.putInt("currentChoice", currentChoice[0]);
You are using two shared preferences container, the default one and a custom one. Use only the default one.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
final int[] currentChoice = {preferences.getInt("currentChoice", 0)};
Related
I'm using DefaultSharedPrefeneces to save and load the state of the checkboxes in my navigation drawer.
when the navigation drawer is created and the state of the checkboxes are loaded from the DefaultPreferences, if the booleans for each checkbox are true then the checkboxes are set to checked.
when the user clicks on a checkbox the state then get saved to the defaultsharedPrefences.
I have no problem with all this.
the problem is that then i try to put 2 booleans into the defaultsharedprefences before the commit it doesn't work and appears to overwrite the first boolean.
Here is my code
//Navigation Drawer
private void addDrawerItems() {
String[] osArray = {"item1", "item2", "item3"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
if (mIsPremiumUser) {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
} else {
mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
}
mDrawerList.setAdapter(mAdapter);
Boolean isCheckedValue;
// *** THIS IS WHERE I LOAD THE CHECKBOX STATE FROM DEFAULTSHAREDPREFERENCES ***
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
isCheckedValue = preferences.getBoolean("cbox1", false);
mDrawerList.setItemChecked(0, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox2", true);
mDrawerList.setItemChecked(1, isCheckedValue);
isCheckedValue = preferences.getBoolean("cbox3", true);
mDrawerList.setItemChecked(2, isCheckedValue);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (!mIsPremiumUser) {
Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
return;
}
switch (position) {
case 0:
if (ctv.isChecked()) {
requestActivityUpdates();
Toast.makeText(getApplicationContext(), "item1ON", Toast.LENGTH_SHORT).show();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", ctv.isChecked());
editor.commit();
} else {
removeActivityUpdates();
Toast.makeText(getApplicationContext(), "item1OFF", Toast.LENGTH_SHORT).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox1", false);
editor.commit();
}
break;
case 1:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item2 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
// THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked()); // << THIS LINE IS CAUSING ME PROBLEMS
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item2 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox2", ctv.isChecked());
editor.putBoolean("callStatus", ctv.isChecked());
editor.commit();
}
break;
case 2:
if (ctv.isChecked()) {
Toast.makeText(getApplicationContext(), "item3 ON", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
} else {
Toast.makeText(getApplicationContext(), "item3 OFF", Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("cbox3", ctv.isChecked());
editor.putBoolean("smsStatus", ctv.isChecked());
editor.commit();
}
break;
}
}
});
}
I know for sure that it works for one putBoolean on each item clicked, but doesn't when I've two putBoolean within the same if/else before a editor.commit .
any suggestions on whats causing the problem and how to fix it ? would be greatly appreciated it.
I figured it out!
as the boolean were being stored in an external defaultSharedPrefenences file,
outside of the app the values were always being read in from that.
the state would be pulled the last time i clicked on the check boxes.
I was thinking that each time I i force closed the app it would go back to the default selections states that i wanted which was false,true,true. but as the default Sharedpreferences was not destroyed when the app was it would load in the most recent state.
I had to clear the app data in the application manager in order for it set the boolean back to null, and display false,true,true.
I have a few interesting question here about how to return to latest activity if the game was restart/re-open because I have a new game button and continue button.So when continue button clicked it will return to last activity that opened before and the condition is activity is random from activityone to activityfive
I will explain with my code
this is menu.class
public class menu extends Activity {
int level;
Button newgame, continues, continuelocked;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
continuelocked=(Button)findViewById(R.id.buttoncontinuelocked);
continues=(Button)findViewById(R.id.buttoncontinue);
newgame=(Button)findViewById(R.id.buttonnewgame);
newgame.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent i =new Intent(menu.this, intro.class);
startActivity(i);
}
});
}
public void onResume() {
super.onResume();
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
level = pref.getInt("Level", 0);
if(level == 0)
{
continuelocked.setVisibility(View.VISIBLE);
continues.setVisibility(View.GONE);
}
if(level == 1)
{
continuelocked.setVisibility(View.GONE);
continues.setVisibility(View.VISIBLE);
}
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", level);
editor.commit();
continues=(Button)findViewById(R.id.buttoncontinue);
continues.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//How to set this method to return to latest activity that i play before
//if i use random levelactivity?
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and in intro.class I do this method to make activity random,
check my code below here -
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
and in every Activity "One to Five" I put the same random activity code
#Override
public void onClick(View v) {
// Here, we are generating a random number
Random generator = new Random();
int number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
}
So My question is
First. How to open the last Activity with a Continue button if Activity was Random?
Second. if in every Activity had a same Random code to One until Five, How to set Disabled to Activity that already Opened before?
Anyone can explain about this?
UPDATED
I have found a solution with my second answer, but i dont try it yet so i dont know it work or not
so i changed the code like this
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
button5 = (Button)findViewById(R.id.button5);
if(v==button5) {
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 1);
editor.commit();
layout7.setVisibility(View.GONE);
layout7.setVisibility(View.VISIBLE);
// Here, we are generating a random number
Random generator = new Random();
number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
// E.g., if the output is 1, the activity we will open is ActivityOne.class
case 1: if(one == 1){
activity = ActivityOne.class;
}
else if(one == 2){
Random generatorone = new Random();
number = generatorone.nextInt(5) + 1;
}
break;
case 2: if(two== 1){
activity = ActivityTwo.class;
}
else if(two== 2){
Random generatortwo = new Random();
number = generatortwo.nextInt(5) + 1;
}
break;
case 3:if(three== 1){
activity = ActivityThree.class;
}
else if(three== 2){
Random generatorthree = new Random();
number = generatorthree.nextInt(5) + 1;
}
break;
case 4:if(four == 1){
activity = ActivityFour.class;
}
else if(four == 2){
Random generatorFour = new Random();
number = generatorFour.nextInt(5) + 1;
}
break;
default:if(five== 1){
activity = ActivityFive.class;
}
else if(five== 2){
Random generatorfive = new Random();
number = generatorfive.nextInt(5) + 1;
}
break;
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
};
i think, if the int was show ==2 its mean the Activity was already opened before. so it will random again till found activity with ==1
can anyone correct my code above? it is right or not?
and my first question still dont have an answer
First. How to open the last Activity with a Continue button if Activity was Random and the app was re-open/restart?
Thank you in advance, have a good day
For your first question, once you open your random Activity you can use SharedPreferences to store an ID for that Activity, and once you press your continue button, you can read this preferences, get this ID, and open the respective Activity.
For example, create an utility class to handle the SharedPreferences.
public class ActivityManager {
private static ActivityManager instance = null;
private SharedPreferences sharedPref;
private Context context_;
private final String prefName = "preferencesHandler";
public static ActivityManager getInstance(Context context) {
if (instance == null) {
instance = new ActivityManager(context);
}
return instance;
}
private ActivityManager(Context context) {
context_ = context;
sharedPref = context_.getSharedPreferences(prefName,Context.MODE_PRIVATE);
}
public void saveActivity(int ID) {
editor = sharedPref.edit();
// Key,Value
editor.putInt("activity_id",ID);
editor.commit();
}
public int getActivityID() {
// Key, Default Value
return sharedPref.getInt("activity_id",0);
}
}
And once you open each one of your random Activity, set some ID to them and save it using the class above. Then when needed, retrieve that value with getActivityID(). That should do the trick.
For more info on how to use SharedPreferences, please read this link.
EDIT: On your specific case, there's this part of your code:
// Here, we are checking to see what the output of the random was
switch(number) {
case 1:
// E.g., if the output is 1, the activity we will open is ActivityOne.class
activity = ActivityOne.class;
break;
case 2:
activity = ActivityTwo.class;
break;
case 3:
activity = ActivityThree.class;
break;
case 4:
activity = ActivityFour.class;
break;
default:
activity = ActivityFive.class;
break;
}
You can easily use that number variable as an ID. So, once you randomly get that number, you can also save it using the SharedPreferences. And when you press the continue Button, simply read that value and use a switch-case again to get the correct Activity.
In my application when user click back , I make an alert dialog that included 2 buttons. first button is Exit that allow user to exit the application.
the second button is 5 star that allow user to rate me in the market.
it works correctly.
but the problem is that when I kill the application and I run it again, this process repeat. and I want if the user rate me before , I don't show the 5 star button to user again.
how can I save my state button in it?
I know that I must share preferences , but how?
int star = 0;
public void onClick(View v) {
int id = v.getId();
if(id == R.id.button1 && stringListCounter <lines.size() - 1) {
stringListCounter++;
} else if (id == R.id.button2 && stringListCounter > 0) {
stringListCounter--;
}
txtQuestion.setText(lines.get(stringListCounter));
}
public void onBackPressed() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(QuizActivity.this);
alertDialog.setTitle("please rate us");
alertDialog.setPositiveButton("5star", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(
Intent.ACTION_EDIT,
Uri.parse("http://cafebazaar.ir/app/my package name/?l=fa"));
startActivity(browserIntent);
star ++;
}
});
alertDialog.setNegativeButton("exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addCategory(Intent.CATEGORY_HOME);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
});
alertDialog.show();
}
here my star value is 0. when user rate me , the value of star become 1 . I
want save the my star value to 1 that this process don't repeat again.
please help
First Of Save Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("KeyValue", newHighScore);
editor.commit();
Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger("KeyValue");
after get preference easily check condition
you can create one method in shared preferences that will give your app state means when user is clicking on 5 start on that time you have to set the app state 1. and next time before calling this
alertDialog.setPositiveButton("5star", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(
Intent.ACTION_EDIT,
Uri.parse("http://cafebazaar.ir/app/my package name/?l=fa"));
startActivity(browserIntent);
star ++;
}
});
apply if condition and check the state of the app in shared preference means if state is 1 then don't allow to execute this functionality else allow.
I'm using Shared Preferences to save data from an AutoCompleteTextView.
When the user writes something in this AutoCompleteTextView, he can click a button in order to save what he just wrote (so that he doesn't have to write it every time).
Here's what my code looks like:
private AutoCompleteTextView autoComplete = null;
String nameFile = "Web service data";
String myData = "";
SharedPreferences pref;
Editor editor;
String channel = "";
String[] valuesArray = {channel};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
editor = pref.edit();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, valuesArray);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autocompletion);
autoComplete.setAdapter(adapter);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(sendForm2);
Button remove = (Button) findViewById(R.id.remove);
remove.setOnClickListener(sendForm2);
channel = pref.getString(nameFile, null);
}
OnClickListener sendForm2 = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
myData = autoComplete.getText().toString();
editor.putString(nameFile, myData);
editor.commit();
break;
case R.id.remove:
editor.remove(nameFile);
editor.commit();
break;
}
}
};
The problem is, the Shared Preferences doesn't save any data in channel at all. Even when I close the application and restart it.
Any clue or idea how to resolve this problem?
First thing I would try would be to add a
Log.d("OnCreate", "channel : "+ channel);
in the onCreate just after
channel = pref.getString(nameFile, null);
to see if you have something inside.
If you don't, this really means that sharedpref are not saved.
In this case I would try to bring back the :
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
editor = pref.edit();
just before the
switch (v.getId()) {
I remember reading that sometimes depending on what you are doing with your activities, the sharedpref editor you create can be "lost" and not be related to anything later in the code.
Use the following method to save and retrive String prefrence.
//save the prefrence by pasing key and data
public static void SavePrefrence(Context ctx, String Key, String value) {
ctx.getSharedPreferences("mypref", ctx.MODE_PRIVATE)
.edit().putString(Key, value).commit();
}
//get the prefrence by passing your key
public static String getPrefrence(Context ctx, String key) {
SharedPreferences pref = ctx.getSharedPreferences(
"mypref", ctx.MODE_PRIVATE);
String result = pref.getString(key, null);
return result;
}
I have a boolean method returning true or false to check whether or not data exists inside of strings. Everything works ok if the user enters all data or does not run through the dialogs.....BUT....if the user DOES NOT enter data in the "getItemsEditText" dialog popup AND still clicks "OK", this boolean is resolving to true, even though "pricePerItemText" still has nothing stored. This is the boolean method:
public Boolean doesAllDataExistCheckBool ()
{
if (pricePerItemText != "" && itemsPerDayText != "" && sleepTimeText != "" &&
wakeTimeText != "")
{
SharedPreferences.Editor editor = mySharedPreferences.edit
(); //opens shared preference editor
editor.putBoolean("storedDoesAllDataExist", true);
editor.commit(); //commit changes to mySharedPreferences
//End storing shared preferences
return true;
}
else
{
SharedPreferences.Editor editor = mySharedPreferences.edit
(); //opens shared preference editor
editor.putBoolean("storedDoesAllDataExist", false);
editor.commit(); //commit changes to mySharedPreferences
//End storing shared preferences
return false;
}
}
Here is where the boolean is being tested to see if true or false:
if (position == 4)
{
allDataExists = doesAllDataExistCheckBool (); //checks if true or false
if (serviceStarted == true)
{
Context context = getApplicationContext();
String text = "Schedule is already running";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
if (serviceStarted == false && doesAllDataExistCheckBool () == true)
{
startScheduleService();
}
if (serviceStarted == false && doesAllDataExistCheckBool () == false)
{
Context context = getApplicationContext();
String text = "Please enter all data before starting!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
Here is how the dialog with EditText and OK/Cancel buttons is written:
case ITEMS_PER_DAY :
LayoutInflater li = LayoutInflater.from(this);
final View itemsEntryView = li.inflate(R.layout.settings_dialog_input, (ViewGroup)
findViewById(R.id.layout_root));
final EditText getItemsEditText = (EditText)itemsEntryView.findViewById
(R.id.DialogEditText);
return new AlertDialog.Builder(SettingsActivity.this)
.setTitle("This is the title")
.setView(itemsEntryView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
itemsPerDayText = getItemsEditText.getText().toString(); //gets input from
edittext and saves it to a string itemsPerDayText
//Initialize shared preferences
SharedPreferences.Editor editor = mySharedPreferences.edit(); //opens editor
editor.putString("storedItemsPerDayText", itemsPerDayText);
editor.commit(); //commit changes to mySharedPreferences
//End storing shared preferences
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//user click cancel
}
}).create();
Is there another way to do this? Why can the user still click "OK" if they did not enter anything at all? Any ideas? Thanks guys!
You posted way too much code. But right away I noticed this
pricePerItemText != ""
Assuming pricePerItemText is a string, which we really have no idea since you didn't include that, that's not how you compare strings in java. It needs to be
!pricePerItemText.equals("");
Edit:
In java, the == operator compares objects references, not values. So
String mytext = "text";
if (mytext == "text"){ print "True"}
will never print true because the mytext variable is pointing to some memory location, which is most definitely not the same as where "text" points to.
The fact that
"text == "text"
is true is a an artifact of Java keeping a string pool so it doesn't have to reallocate new strings. This is a major cause of confusion.
Here's a random link which describes it probably better
http://leepoint.net/notes-java/data/expressions/22compareobjects.html