Skip to content
Advertisement

What is Coder in String Value?

I am reading a CSV file, by using com.opencsv.CSVReader Like below

String[] headers = csvReader.readNext();

Value for header is coming like below screen shot:

enter image description here

What’s coder here (Highlighted in yellow)?

Why the value is 1 for first index and 0 for all other?

Advertisement

Answer

The official response is “none of your business”, since it’s a private member 😛 Which means it can very well be implementation-specific and not found in other vendors’ version of the JVM.

The actual response can be found in the source code for the String class

The identifier of the encoding used to encode the bytes in. The supported values in this implementation are

  LATIN1
  UTF16
 

This field is trusted by the VM, and is a subject to constant folding if String instance is constant. Overwriting this field after construction will cause problems.

As to why the first one is different, that depends on how each String is instantiated. The choice of the default value depends on a parameter set by the JVM. A value different from the default one is a sign that the String was build from another String or a byte array.

In the first case it means the original String has that coder value itself.

In the second case it depends on the result of a call to the decode method of the StringCoding class which returns an object with the code value set depending on that initial parameter I talked about above (the one set by the JVM) and the encoding passed to the constructor of String.

Advertisement