Lesson 5: Using Strings
Strings have always been and will always be special. That being said, the integration between Java and .NET is actually very simple.
In contrast to all other types, Java String
is translated to C# string
in the C# proxy types. This is a great convenience for you most of the time but has a few consequences
that should almost never impact you.
On the plus side, you do not have to worry about converting a Java proxy String
to a
.NET string; the runtime does that for you automatically.
There are a couple of minuses:
- You lose access to the underlying Java
String
instance, which means you cannot perform any operations on it. This is hardly ever necessary and if it ever is, you could resort to runtime API methods to achieve your gaol. - We always marshal and unmarshal strings between the .NET and Java side whenever they are used.
This has a negative performance impact when compared with reusing a proxy
String
instance.
We carefully weighed the advantaged and disadvantages and decided that the benefits in terms of usability
outweighed the costs, which were largely theoretical. As a consequence, you can use .NET strings in any place
where a Java String
or Object
is expected.