I want to know how to access particular character of a String without using any pre defined functions(like charAt etc)? Since there are no pointers in java and the concept of character array being used as a String...So is there any way to do this?
How to access particular character of a string without using pre defined functions?
Collapse
X
-
Tags: None
-
No. If you can't use any of the methods of the String (or other) class then you can't determine what any character is.
Then again without using some method or other, it's hard to see that you can do anything at all. -
Strings are immutable so it won't let you fiddle with its internal data buffer at all, you have to use the methods it supplies for that purpose (such as charAt(...)). It doesn't supply any methods for changing that buffer at all, you can only read from the buffer.
kind regards,
JosComment
-
And why would you want to avoid the provided method? I thought that was the whole point of having the class.
Requirements never cease to amaze.
Regards,
Alex.Comment
-
Anyways thanks everyone for replying......S o how was charAt implemented ...I kw it must have used some other function for its task so on then how that function implemented?Comment
-
Wanting to know how a method is implemented is a totally different matter I think. Maybe I misunderstood what you wanted to do.
If you simply want to know how charAt is implemented, I believe the JDK install comes with the source code for the String class. Mostly you will find the methods that do not require native implementation. Additionally there is the OpenJDK (Open source java) project which will have the entire sources.
Regards,
Alex.Comment
-
Have you never implemented a String class before?
I think I must have implemented a String class a thousand times as school assignments or during exams or quizzes at school.
Anyways, think about what a String really is...it's just an array of chars.
The String class provides you with methods that add nice functionality which lets you interact with this underlying array of characters.
So the charAt() method will return you the character at the index in the array of characters that make up the String.Comment
-
I too have implemented String class in c++ but with the aid of pointers there. In Java Character array is different from String......Act ually what I was thinking how these functions were implemented without existence of pointers.Comment
-
The same can be said of the other languages on which *there are no pointers*.
These languages just makes the programmer's work easier not to have to deal with pointers directly.
Regards,
Alex.Comment
-
There seem to be three questions so far:
How do I access characters in a String without using String (or other?) methods? - answer: you can't.
How do Java implementations provide String's functionality? - answer: read the code. (It seems to me that this is subtle and difficult as the Java language treats Strings somewhat specially and what you end up with is a complex solution to a complex problem involving efficiency and security).
How would I implement a string class?
The last is quite interesting. It might be as simple as wrapping a char array and providing the methods you want. Or it might be more meaningful. Why do you want a string class? What will it do? A string that's designed for efficient searches (of some particular type) might involve a more elaborate data structure than a simple array. A string that's going to participate in other data structures might have special (eg hashing) requirements. Should the string be mutable?Comment
-
But what is really a string? What is the abstraction? It is in my opinion there is no more a best way than an array of characters. That's what the machine understands. If we want a more specialized cousin of the string, we do it another way. An example is the StringBuffer class in Java or the more specific StringBuilder.
But the common denominator is that every programmer will need the String class without all the bells and whistles. One that he can use *quick and dirty* without worrying about efficient searches, hashing and all. So it's okay to implement a specialized String, but that doesn't make your usual kind of String redundant.
Regards,
Alex.Comment
Comment