Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Size of Char Array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Size of Char Array." Watch "Size of Char Array." New topic

Size of Char Array.

Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 141
String str = new String("abc");

What is the size of the character array that got created for the same.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

So what do you think?
Did you check the API docs for any methods which will help you solve this?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825


There is a method provided by String class that you can use to convert string to a character array. Following that you can retrieve the length of the array. Check API to find it.

The quieter you are, the more you are able to hear.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15099

Santosh Kumar Nayak wrote:String str = new String("abc");

Note that you should never write code like this. Just write:

String str = "abc";

It is never necessary to explicitly create a new String object if you're initializing it with a literal, such as "abc".

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 970
The API description (a valuable resource worth studying for every developer) mentions this method of String:

I agree. Here's the link:
subject: Size of Char Array.
It's not a secret anymore!