1

When I write my code as

String s = "T";

Java is internally creating the value as char[]. That means [T].

But I was not able to find, how and when a String is initialized, it is automatically converted into a char array.

Could anybody please direct me, on what's happening here?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
user2082928
  • 11
  • 1
  • 5
  • 2
    I am a bit confused of your question, what is exactly your problem is? and I am not sure what you mean by Java is internally creating the value as char? – Maytham Fahmi Mar 15 '17 at 08:13

2 Answers2

1

Java treats Strings as constant objects. To hide this and for some performance improvements, there's an internal representation of the string value. (If you create e.g. two Strings with the same value, they might use the same memory location for the value.) If you want to dig deeper, you can find more details in the Java Language Specification, JLS $4.3.3 and the Java Virtual Machine Specification, e.g JVM $5.1

Unless the JLS or JVMS specifies it, these details are JVM / implementation specific. It looks like the current Oracle JVM uses an array of UTF-16 characters to represent the value of strings in memory. JEP 254: Compact Strings documents this (and a change in Java 9). If you look at it on a byte level (and we're talking about internals) an array of char is very similar to a string, so this distinction is somewhat academic and the char array just another way to look at a string.

There are multiple questions dealing with the concrete internal String representation (What is the Java's internal represention for String? Modified UTF-8? UTF-16?, Java String internal representation).

BUT: Unless you're working on JVM-level (JNI, hard core performance optimization, etc.) the internal representation shouldn't concern you! In your Java-Code, you still have a String object to program with. Your String s is not a char array or even assignable to one; char[] ch = s; gives you a type mismatch error (that's why you have a special method for that conversion ch = s.toCharArray();).

It looks like the confusion comes from the variable inspector in Eclipse that shows String object also as a char array. A debugger in a Java IDE might not show you the "true" representation of an object, but merely a user-friendly view. (AFAIK you can even extend the Eclipse Inspector to show "more meaningful" versions of an object). For a String object Eclipse shows you the String literal and the equivalent character array, probably to make an analysis easier.

In other words: The internal representation of a String object might be an array of char, but this is not valid in your Java program. A String object stays a String object in your code!

Community
  • 1
  • 1
sruetti
  • 532
  • 2
  • 7
  • okay...! I think, by the way I understand it, the String to char array conversion is an internal representation. – user2082928 Mar 15 '17 at 16:17
  • I'm just saying there's an internal representation, I don't claim it's an array of char. But if you look at it on a byte level (and we're talking about internals) an array of char is very similar to a string (in some languages it's even equivalent), so this distinction is somewhat academic. BUT: As you write about Eclipse further down: A debugger, especially in a Java IDE might not show you the "physical" representation, but a user-friendly view. (AFAIK you can even extend the Eclipse Inspector to show "more meaningful" versions of an object). – sruetti Mar 15 '17 at 18:25
0

What you are saying is not correct.

Below code proves that java is creating an object of String class for "T".

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        String s="T";
         System.out.println(s.getClass().getName());//prints **java.lang.String**
    }
}

You may verify this by running the above code.

asad_hussain
  • 1,959
  • 1
  • 17
  • 27
  • I completely agree that String s = "T" means, java will initialize an object of type String. Sorry, I may not be clear previously. Let me try to re-phrase it. If we have the below code – user2082928 Mar 15 '17 at 15:19
  • 1)String s = "T"; 2) System.out.println(s); If you keep a breakpoint in point (2) and inspect the line (1) s value, The in the eclipse -->debugger --> variables you will see as follows: Variables value t "C" hash 0 value (id=26) [0] C Now if you see the array [0], my question, is how this got converted into an array. Also, when we say string is created, how the string constructor is called ? I kept break point in the java source code, but I did not see any String constructor code – user2082928 Mar 15 '17 at 15:28
  • U are not clear.Please write comments in formatted way so that it is understandable. – asad_hussain Mar 15 '17 at 15:47
  • I have only one question, here . I had downloaded the sourcecode and kept a break point in all possible String constructors in String.java. But when the line Sting s = "T" , got executed, none of the constructors is getting called. Am I missing anything ? I want to know, how the String is created, whenever, I instantiate a String to a String variable. – user2082928 Mar 15 '17 at 16:08
  • 1
    This [link](http://stackoverflow.com/questions/17651824/which-string-class-constructor-get-called-when-string-object-created-by-using-st) may help you to understand how java initializes a string object in case of literal. – asad_hussain Mar 15 '17 at 16:52
  • @a874: good link, but you have to read *all* answers and comments to get a good view about what's really going on. I'll try to add this to my answer later. – sruetti Mar 15 '17 at 18:29