[java] Hi, I have another newbie sort of question... (weird one... :)

Started by
8 comments, last by LinkOfTime 18 years, 8 months ago
Hi I wrote a small testing program (please disregard the names of the methods in it, I was going to make it something else...). Here is the code: (Sorry, didn't know how to put it in a code box...)

import java.util.*;

class Bool
   {
        private int TF = 0; //false
        void ChanBool(String Condition)
           {
                System.out.println("Condition= " + Condition);
                if (Condition == "True")
                   {
                        TF = 1; //true
                        System.out.println("Changing to TRUE!");
                   }
                else
                   {
                        TF = 0; //false
                        System.out.println("Changing to FALSE!");
                   }
           }
        int GetBool()
           {
                return TF;
           }
   }

public class TicTacToe
   {
        public static void main(String[] args)
           {
                System.out.println("TicTacToe started.");
                Bool B = new Bool();
                if (args.length != 0)
                   {
                        System.out.println(args[0]);
                        B.ChanBool(args[0]);
                        System.out.println(B.GetBool());
                   }
           }
   }



and here is the output: " ?-java TicTacToe True TicTacToe started. True Condition= True Changing to FALSE! 0 " (* "?-" = The command line I wrote in DOS) Can someone please explain to me how is it possible to get that output for my program with the command I gave? I really thank all of you for trying to help me :) [edit: added source tags -SiCrane] [edit: Thanks, now I will know how to use source boxes for next time :) -LinkOfTime] [edit: Corrected grammer mistakes in a word :) - LinkOfTime] [Edited by - LinkOfTime on August 24, 2005 7:07:39 PM]
Don''t have one, sorry.
Advertisement
One more thing...
When changing the line "B.ChanBool(args[0]);" to "B.ChanBool("True");", the program ran correctly and gave the correct output:

"
?-java TicTacToe True
TicTacToe started.
True
Condition= True
Changing to TRUE!
1
"
(* "?-" = The command line I wrote in DOS)

Hope it helps you understand what I mean by weird problem... :)
Thanks again for the help :)
Don''t have one, sorry.
Look at the following code:

String a1 = "a";String a2 = "a";String a3 = new String("a");String a4 = new String("a");String a5 = a4;if("a" == "a")	System.out.println("Test 1 passes.");if(a1 == a2)	System.out.println("Test 2 passes.");if(a4 == a5)	System.out.println("Test 3 passes.");if("a" == new String("a"))	System.out.println("Test 4 passes.");if(new String("a") == new String("a"))	System.out.println("Test 5 passes.");if(a3 == a4)	System.out.println("Test 6 passes.");if(a3 == "a")	System.out.println("Test 7 passes.");


Only tests 1, 2, and 3 pass. Why? Because String literals are treated differently than String objects allocated with new. When the == operator is used between objects, it is NOT a test for equality. It is a test to determine whether those two identifiers point to the same object in memory. (In other words; a test for aliasing). I can't stress this enough. However, between String literals, it is a test for equality, because they are treated like primitives by the == operator.

Since the String contained in the array args is a String object allocated with the new keyword, it is treated as an object by the == operator, making the test args[0] == "True" false.

To test for equality among objects, use the objects.equals() method.
Thanks Kevinator, it works now :)
My problem was that I didn't relize that I was receiving a memory reference to a string object by calling args[0] and not the actuall string... :)
Thanks again :)
Don''t have one, sorry.
You'd think that since Sun went out of their way to overload operators for String creation, that you'd be able to at least compare them with == . Another reason to add operator overloading to the language.
Totally agree... It would have been much easier to use if it was that way. :)
Don''t have one, sorry.
Quote:Original post by Kevinator
When the == operator is used between objects, it is NOT a test for equality. It is a test to determine whether those two identifiers point to the same object in memory.


Yes.

Quote:However, between String literals, it is a test for equality, because they are treated like primitives by the == operator.


No. When a string literal is first encountered, a global String object is created behind the scenes. Any identical string literals found in the code will refer to the same object rather than creating a new one. Since Strings are immutable, this causes no problems and is more efficient than creating a new instance for each literal. The result is that == on string literals functions exactly as it does with other objects and does not test for equality.

That's why this code is redundant:

String s = new String("a");

Every time that code is called a new String is created that is a copy of the global object created for "a". This is pointless because Strings are immutable anyway. By doing this:

String s = "a";

Only one object is actually created, and each invocation of that code creates a new reference to that global string object. These global Strings are shared across all code running in the same VM. So if a user is running 3 Java apps om the same VM, and all 3 apps have a String literal "a", then only one instance of that String object is shared between each app. You can read about this in the Java specification.

Oh... Now, I understand it... Thanks :)

Anyway, I have another problem now:
import java.util.*;public class TicTacToe   {        public static void main(String[] args)           {                for (int i=0;i==2;i++)                                  for (int j=0;j==2;j++)                           {                                System.out.println("Testing...");                           }           }   }


How come that when I write:
"
?- java TicTacToe
"
I get no result at all, nothing is written back on the screen :(

Can someone please tell me what I am doing wrong this time... Thanks again :)

Edit: I don't get any error message during compilation or run-time as well...
Don''t have one, sorry.
The first for loop gets skipped because i is not 2 for the first check. Remember, the condition used in for loops is "while this condition is true", not "until this condition is true". So what you want to do is say:

import java.util.*;public class TicTacToe {    public static void main(String[] args) {        for (int i=0;i<=2;i++)                      for (int j=0;j<=2;j++)                System.out.println("Testing...");    }}


When the first for loop executes, it initializes i to 0, then checks the condition (i<=2) to see if it's true. If it is (which it will be), then the body of the for loop executes and i will be incremented by one. If i is 0 and the condition is i==2, the condition will be false and the loop won't execute.
Ah... Ok... I was sure it was an until kind of loop...
Thanks again :) Sorry for all the newbie questions... :)
Don''t have one, sorry.

This topic is closed to new replies.

Advertisement