(New Question Bottom-Posted ..)

RE: JavaIntro: JavaControl, Lab-1034, Exercise 2: For Loop

[code]
//=========================
        // Search the String array using for loop.
        //  * The "names.length" is the size of the array.
// * This for loop compares the value of each entry of the array with
        //     the value of searchString String type variable.
// * The equals(..) is a method of String class. Think about why you // cannot use "names[i] == searchName" as comparison logic here.

        for (int i=0; i<names.length; i++){
            if (names [i ].equals(searchName)){
                foundName =true;
                break;
            }
        }
// =========================
[/code]


>  -------------------------------------
>  Date: Wed, 29 Aug 2007 09:57:05 +0530
>  Local: Tues, Aug 28 2007 11:27 pm
>  Subject: Re: [java programming] Re: LAB-1034:Controls Homework Solution
>
> By the way, why cant we use == operator to compare strings instead of method "equals"?
>
> I ran the following code in main and it indicates that theres no problem using == operator.


>>  -------------------------------------
>>  Date: Tue, 28 Aug 2007 23:44:50 -0500
>>  Local: Tues, Aug 28 2007 11:44 pm
>>  Subject: Re: [java programming] Re: LAB-1034:Controls Homework Solution
>>
>> You don't want to use '==' for String compares. Here is an article >> that explains why:
>>
>>  http://www.javaworld.com/javaqa/2002-09/01-qa-0906-strings.html
>>
>> Kirby


QUESTION 1:
- So JAVA string comparisions must be done using:
        if (names[i].equals(searchName) )

- IIRC, in C++ you CAN do STRING comparisons using:
        if (names[i] == searchName )

As each will return the VALUE the object-address pointed to, not the object-address as in Java (according to article linked above).

- In C++ to compare the objects it would be something similar to:
        if ( &names[i] == &searchName )

- So in JAVA, could we alternatively do the same STRING comparison using "==" along with some kind of deference operator? ie.
        if (names[i->value] == searchName->value )        

I suppose this is less elegant here, but I'm more interested in grasping how JAVA is accessing memory/objects/etc. compared to C++.

Thanks,
Steven
        

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to