Jump to content

Java -- Char array to string problem

Go to solution Solved by Ciccioo,

in java you can't use the == operator to compare objects

what you're actually comparing is not the strings, but the strings references

to do the string comparison you have to use the equals() method

if( s.equals(cs) )

Heyo guys,

There is a slight problem I encountered a few hours ago.

I have a char array and a string. If you put together the values of the different indexes of the array you'll get the exact same as the string.

There is a problem though, whenever I do that and compare those two , they apparently are NOT the same. I don't understand it.

If someone could explain this to me I would be very grateful.

Here is the sample code I setup, I don't want to bother you with my main program I'm writing, this snippet of code just highlights the problem:

public class Start {	public static void main(String[] args){				char[] c = {'H', 'E', 'Y'};		String s = "HEY";				String cs = new String(c);				if(s == cs){			System.out.println("true");		}			}	}

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

in java you can't use the == operator to compare objects

what you're actually comparing is not the strings, but the strings references

to do the string comparison you have to use the equals() method

if( s.equals(cs) )
Link to comment
Share on other sites

Link to post
Share on other sites

 

in java you can't use the == operator to compare objects

what you're actually comparing is not the strings, but the strings references

to do the string comparison you have to use the equals() method

if( s.equals(cs) )

 

Very good! It worked well. Thank you very much.

Are you able to explain how the equals() method works?

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

If what @Ciccioo said does not work try this instead. String© may return just one of the letters. Not sure.

String is a class that handles strings, so that's exactly how you're supposed to look at it: as a string

for this reason it wouldn't be acceptable that String returns a character when you access it

 

the "problem" here is just how java works: object variables only store the reference to the actual object, so the == operator loses its meaning when you use it to compare object which are instances of the same class

the right way to do it is to use the compairing behaviour implemented as the equals() method (i think there is also compare() or something), that is inherited from Object and hopefully overridden by the class, String in this case

 

 

Very good! It worked well. Thank you very much.

Are you able to explain how the equals() method works?

link to java doc

it accepts an object as a parameter, it returns true if the object is a string with the same thing written into it

Link to comment
Share on other sites

Link to post
Share on other sites

Expanding on what @Chiccioo said,

You have your string s which is in memory location "A", then you create a new string cs from "H" "E" & "Y" which is now in memory location B.

s points to a string at A, and cs points to a string at B.

s == cs will return false because the memory location A isn't the same as the memory location B (even though the contents are the same)

In the .equals() method, the values of each of them are obtained and checked against each other to return if they are strings of the same characters or not.

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

I wish I had the time or patients to learn to code :( .

 

If you're motivated, you can do everything.

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

Someone really needs to pin a topic about some of the common Java issues....that == happens so often it seems.

 

On a side note I just figured out yesterday that Java reads as Big Endian (even if your machine is running as Little Endian and despite that most systems seem to write as little endian)....took me 30 min to figure out why my c generated output file wasn't being read in by the jar properly (It sucks not being able to debug).

 

So as a note, if you ever write a java program that interacts with a c program, watch out for Endianness

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Someone really needs to pin a topic about some of the common Java issues....that == happens so often it seems.

 

On a side note I just figured out yesterday that Java reads as Big Endian (even if your machine is running as Little Endian and despite that most systems seem to write as little endian)....took me 30 min to figure out why my c generated output file wasn't being read in by the jar properly (It sucks not being able to debug).

 

So as a note, if you ever write a java program that interacts with a c program, watch out for Endianness

 

I don't think I'm the right person to start a "common java problems" thread, but it definitely seems like a good idea!

What is the purpose of writing to memory in different ways? 

When do you need to write a java program that interacts with a c program?

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think I'm the right person to start a "common java problems" thread, but it definitely seems like a good idea!

What is the purpose of writing to memory in different ways? 

When do you need to write a java program that interacts with a c program?

 

Actually java uses big endian to write files.

 

There is really never a "need" to write a java program that interacts with a c program, but there are some cases where it can be handy.  Personally I used c to write a file, instead of java because the libraries I needed are easier to work with in C (in my opinion)...the only downside is that C on current processors will save things as little endian.

 

In terms of Big Endian vs Little Endian, the main concept (from what I remember in classes), is when data is being streamed...if you need to start doing calculations it is important to get the most significant numbers first...there are probably better cases where big endian vs little endian is compared...but I can't think of any right now

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×