• Post Reply Bookmark Topic Watch Topic
  • New Topic

String and char help needed  RSS feed

 
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I got stuck with this problem that I have know idea as to how to go about solving. Please don't give me the outright answer, but if anyone could explain these objects to me and how to find the answer, it would be greatly appreciated!

8. Consider the following statements:



a. What value is stored in ch by the following statement?
ch = str.charAt(0);

b. What value is stored in ch by the following statement?
ch = str.charAt(10);

c. What value is stored in len by the following statement?
len = str.length();

d. What value is stored in position by the following statement?
position = str.indexOf('t');

e. What value is stored in position by the following statement?
position = str.indexOf("park");

From Java Programming: From Problem Analysis To Program Design
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if you see string class in source code then you can see it uses character array for its implementation. So each character in a string will be stored as a single character in a array.

if you want to access first element then use str.charAt(0) and like wise others. indexOf will tell the first occurring position of the character in the string.

So, say string is "Hello" so "Hello".indexOf('l') will give 2 index position or you can say l is present at (2+1) position. and str.charAt(0) will give 'H'.

length() will tell the length of a string internally it calculate length of a character array which string uses.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to study String class in detail to get the answers.

Class String
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:
if you see string class in source code then you can see it uses character array for its implementation.
That doesn't help, I am afraid. You use a class without knowing anything about its internal implementation
So each character in a string will be stored as a single character . . .
That was true once, but ten years ago Java implemented the extensions to Unicode, so some characters are stored as two chars.
Please write the correct data type: char. Java does not store characters (small c) at all, but chars, which are not characters. They are numbers which are usually translated back to characters when printed out.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't help, I am afraid. You use a class without knowing anything about its internal implementation


Campbell, i have checked String class again and found below implementation. That's why i told it implemented char array.



That was true once, but ten years ago Java implemented the extensions to Unicode, so some characters are stored as two chars.


I was not talking in regards to memory but with the index term. like "Hello" store as {'H','e','l','l','o'}. each character takes one position in array.

Please correct me i am wrong.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote: . . . Campbell, i have checked String class again and found below implementation. . . .
I didn't mean that the implementation was incorrect; I meant that it is unnecessary to know that.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how UTF-16 is implemented; I have spent the best part of an hour failing to work it out , but you can get Unicode characters whose value is > 0xffff, so they require two places in the array. But they still seem to appear with charAt(i) whether they occupy 16 or 32 bits, so you appear to be correct on that point .
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first comment is simply a definition issue. You ask about trying to explain the objects however the only object which you have is str. Variables of type char and int are primitives and not objects.

Next, The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Please remember that the first character in a string is position 0, so that point will help you in (a) and (b).

The length is the actual length of a string but remember that "a" has a length of 1 and NOT 0.

For indexOf(), when only one parameter is given, it is the starting point of the first occurrence of the string or character. Again, if it is the first character, it is 0. If the character or string is not present, the result is -1.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can get Unicode characters whose value is > 0xffff, so they require two places in the array.


Thanks Campbell.. I was not aware of this..
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure all of these questions could be answered by writing some quick test code and printing out the results. I would strongly suggest the original poster do that, and then if there are specific questions about WHY the results are what they are, come back and ask those.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Thanks Campbell.. I was not aware of this..

And TBH, for charAt() you really don't have to worry about it, because it returns a char - and chars will NOT have a value above '\uFFFF' (or 64K).
I can't be absolutely certain, but Strings internally hold their characters in a char array, so I'd care to bet that charAt(x) is simply implemented as:
return internalArray[x];

It's codePointAt(x) where things get interesting , but again, the definition seems to be fairly straightforward.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes are supposed to get a code point count less than length() but I couldn't seem to get that to occur when I tried yesterday.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!