Java Arrays and Methods

Let's see some examples of using arrays to get values from and pass values to Java methods. We'll use three methods of the Java String class (from the java.lang package): valueOf(), getBytes(), and getChar(), which are described in Table 11-6.

Since the valueOf() method takes char[] as an argument, we need to create an array of primitive type char. In Java the [] notation denotes an array.

Table 11-6. Example Java String Class Methods
Method Description
static String valueOf(char[] data) Converts an array of primitive type char into a string
byte[] getBytes() Returns the byte data associated with a string
void getChars(int srcBegin, int srcEnd, char [] dst, int dstBegin) Copies characters from the string into a char array ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.