![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Sexy Programmer
|
I didn't know I could have used the "==" equality operator, I thought I had to use a pre-defined method in the Character class. You have to use ".equals()" for Strings and Strings are nothing but character arrays acording to the Java books I've been reading/read.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#12 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Strings are objects (they even extend Object). While the underlying data structure is most likely a character array, Strings (obviously) have properties and methods. The purpose of a String is to hold a sequence of characters in a data type and perform whatever operations on that sequence. Because Strings are objects, using == compares the memory address of the object, rather than the value it holds. Because this hold for any object, the Object class defines a .equals() method for comparing the values of two objects. Primitive (non-object) types are simply data, without methods (global operators excluded). With primitives, using == compares the data in memory, not the address where the data is stored. Variables of type char are primitive. Hence, when you use:
String s = "some string";
if(s.charAt(5) == 's')
System.out.println("It\'s an \'s\'.");[edit:] Quote:
|
|
|
|
|
|
|
#13 |
|
Sexy Programmer
|
The beauty of Object Orientated Programming. So simple yet so complex!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|