This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Well the String itself is implemented at a char array, so just count the chars. Then there is a little overhead elsewhere to track it's location. A file could be more complex. You could just read it out byte by byte and find out.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Jun 16, 2002
hi cindy, I really couldnt get your answer. i am able to count the charecters of the string. but i couldnt find any method to find the memeory it would occupy. please help me with a little more detailed answer Thanks Chandhrasekar Saravanan
I really couldnt get your answer. i am able to count the charecters of the string. but i couldnt find any method to find the memeory it would occupy. Do you mean the physical address of the String object in memory?
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
If you are referring to the AMOUNT of memory, rather than the LOCATION in memory, each char is 2 bytes long in Java. So if a String has 10 characters, it occupies 20 bytes, plus whatever overhead is needed for the String class itself. HTH Layne