82

I have a string array selectCancel with setter and getter methods, which is a checkbox in my form. I am trying to get the checked values and I am getting the above result when I print.

I tried the Arrays.toString() method but it still prints the same.

I then did the following:

String checked = Arrays.toString(Employee.getSelectCancel());

I also tried with the Arrays.asList() and Arrays.copyOf()

So, how do I read this string?

blackpanther
  • 10,998
  • 11
  • 48
  • 78
Srikanth Sridhar
  • 2,317
  • 7
  • 30
  • 50

6 Answers6

110

The method works if you provide an array. The output of

String[] helloWorld = {"Hello", "World"};
System.out.println(helloWorld);
System.out.println(Arrays.toString(helloWorld));

is

[Ljava.lang.String;@45a877
[Hello, World]

(the number after @ is almost always different)

Please tell us the return type of Employee.getSelectCancel()

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
31

Ljava.lang.String;@ is returned where you used string arrays as strings. Employee.getSelectCancel() does not seem to return a String[]

anvarik
  • 6,417
  • 5
  • 39
  • 53
  • Once the string array is sorted with Arrays.sort(a); convert it into strings by using Arrays.toString(a) and then print it. Whole code will look like this: import java.util.Arrays; class Playground { public static void main(String[ ] args) { System.out.println("Hello Java"); String [] a = { "ew" , "ds" , "as" }; Arrays.sort(a); System.out.println (Arrays.toString(a)); } } – Saif Siddiqui Jun 21 '19 at 06:52
12

According to the Java Virtual Machine Specification (Java SE 8), JVM §4.3.2. Field Descriptors:

FieldType term | Type      | Interpretation
-------------- | --------- | --------------
L ClassName ;  | reference | an instance of class ClassName
[              | reference | one array dimension
...            | ...       | ...

the expression [Ljava.lang.String;@45a877 means this is an array ( [ ) of class java.lang.String ( Ljava.lang.String; ). And @45a877 is the address where the String object is stored in memory.

Mincong Huang
  • 5,284
  • 8
  • 39
  • 62
12

[ stands for single dimension array
Ljava.lang.String stands for the string class (L followed by class/interface name)

Few Examples:

  1. Class.forName("[D") -> Array of primitive double
  2. Class.forName("[[Ljava.lang.String") -> Two dimensional array of strings.

List of notations:
Element Type : Notation
boolean : Z
byte : B
char : C
class or interface : Lclassname
double : D
float : F
int : I
long : J
short : S

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
Saurabh Gupta
  • 121
  • 1
  • 2
1

I have the same trouble: I make my own methods: So if I gonna call method like this:

Show("Additional String like this:"+ MyArray);//wrong command

have error! It's must be without "Additional String like this:" just do like this:

Show(AnyArray);//right command

package j;

class J{
public static String [] AnyArray = new String[3];

public static void main(String[] args) {

AnyArray[0]="String_0";
AnyArray[1]="String_1";
AnyArray[2]="String_2";

/******************************************************/
Show(AnyArray); //right
/*****************************************************/
Show("Additional String like this"+AnyArray);//wrong
/****************************************************/
}


public static void Show(String[] MyArray)
{
for(int i=0;i<=MyArray.length-1;i++){
System.out.println("MyArray ["+i+"]: "+MyArray[i]+"");
 }
}



public static void Show(String MyString)
{
System.out.println(MyString);
 }

}
Arthur Alunts
  • 115
  • 10
0

I also met this problem when I've made ListView for android app:

Map<String, Object> m;

for(int i=0; i < dates.length; i++){
    m = new HashMap<String, Object>();
    m.put(ATTR_DATES, dates[i]);
    m.put(ATTR_SQUATS, squats[i]);
    m.put(ATTR_BP, benchpress[i]);
    m.put(ATTR_ROW, row[i]);
    data.add(m);
}

The problem was that I've forgotten to use the [i] index inside the loop

Leo240
  • 766
  • 7
  • 16