arrays - Strings are not sequence of characters in java? -
in java, why not possible convert string char array , vice versa?
why aren't comparable?
char matrixc[][] = {{'s', 't','a', 'c','k'}, {'o','v','e','r'}, {'f','l','o','w'}}; string [] matrixs = {"stack", "over","flow"}; // matrixs=matrixc //not allowed... if (matrixs.equals(matrixc) || matrixc.equals(matrixs) ) { system.out.print("true"); } else system.out.print("false"); //output
edited:
after research , digging, found answer of first question (why not possible convert string char array , vice versa?) in below link, explained roger_that. hint james. immutable
so, have separate string pool in java heap, stores , creates new string literals each time , refer them string objects accordingly.
still, have doubts comparison (i talking simple string , char sequence here, not 2d arrays)
why not there standard , simple method compare string , char seq? 1 way be, char data[] = {'a', 'b', 'c'}; string str="abc";
if (str.length()==data.length && str.length()>0 ) { (int i=0;i<str.length();i++) { if (str.charat(i)!=data[i]) system.out.println("not equal"); } } else system.out.println("not equal");
is there other simple/direct way this, except,
char data[] = {'a', 'b', 'c'}; string str="abc"; string str1= new string(data); if (str1.equals(str)) system.out.println("equal");
conclusion:
strings not sequence of characters in java.
they should remove line here https://docs.oracle.com/javase/tutorial/java/data/strings.html
strings
strings, used in java programming, sequence of characters. in java programming language, strings objects.
this line confusion started. :|
please correct understanding.
in c++ (which i'm guessing coming from) strings stored character arrays. not case in java. in java strings immutable objects rather char[].
Comments
Post a Comment