Category: Proxy usage

Q

How do I create a Java string from a C++ string?

A

The short answer is:

String      str = "test";
String      wstr = L"test";

It really is that simple, but there are a few related issues you should be aware of.

By default, every Java string instance creation involves a pretty elaborate process. The runtime library has to:

  • determine the C++ string's length
  • convert the string characters to a Java byte[]
  • create a Java String using the String(byte[])constructor.

If performance is important for you, you can definitely optimize this process. You can get a pretty significant improvement almost free if you know for sure that your C++ strings are ASCII or UTF-8. In that case, you can set the runtime library's default encoding to "UTF-8", which improves conversion performance significantly. Take a look at the String reference for more details on this subject.

 


Copyright 2006-2011 by Codemesh, Inc., ALL RIGHTS RESERVED

:
frequently asked questions
home products support customers partners newsroom about us contact us