alexanoid
alexanoid

Reputation: 25780

convert C or C++ constant to Java

I have the following C or C++ constant:

static const char *selstr2 = "oooo"
                             "oC o"
                             "o  o"
                             "oooo";

Right now I don't understand what is analog to this in Java. Is it a Java String or String[]?

Upvotes: 1

Views: 175

Answers (2)

Bathsheba
Bathsheba

Reputation: 234685

First thing, note that in this context, your C-style string is equivalent, upon unobfuscating the multiline presentation and the concatenating behaviour of successive "" in this context, to

static const char *selstr2 = "oooooC oo  ooooo";

selstr2 is a pointer to a char[17] literal in C and a const char[17] literal in C++ that's decayed to a const char* pointer type. Note the important divergence of C and C++ here: although the type is not const in C, you still cannot modify the contents.

You might be tempted to think that this is equivalent to a java.lang.String. But it isn't on two important points: (1) Java strings are wider; they consist of Java char types, which are 16 bit unsigned types. And (2) The NUL-terminator is not specified, implicit, or perhaps even present.

I'd say therefore that a java.lang.String is closer to a C++ std::basic_string<std::uint16_t> with UTF-16 encoding, and the C-style string in many ways is closest to an array of bytes in Java, with an extra 0 at the end.

This all said, you'll probably find that

static final java.lang.String selstr2 = "oooooC oo  ooooo";

will be fit for purpose, unless you intend to interoperate between the two languages.

Upvotes: 9

Sebastian Redl
Sebastian Redl

Reputation: 71939

It's equivalent to a String. (As close as a C string ever gets to that anyway.)

In C and C++, string literals that are only separated by whitespace are treated as a single long literal.

Upvotes: 2

Related Questions