what is Ljava.lang.String;@
JavaArraysStringJava Problem Overview
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?
Java Solutions
Solution 1 - Java
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()
Solution 2 - Java
Ljava.lang.String;@
is returned where you used string arrays as strings. Employee.getSelectCancel()
does not seem to return a String[]
Solution 3 - Java
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.
Solution 4 - Java
[
stands for single dimension array
Ljava.lang.String
stands for the string class (L followed by class/interface name)
Few Examples:
Class.forName("[D")
-> Array of primitive doubleClass.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
Solution 5 - Java
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);
}
}
Solution 6 - Java
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