Whenever I try to print the char arrays to the console, I'm getting the result in integer format, but whenever I try to print integer arrays to the console, I'm getting the result in hashcode format. Could anyone please tell me why?
char[] arr={'4','5','6'};
System.out.println(arr); //456
int[] arr={4,5,6};
System.out.println(arr) //[I#3e25a5]
java.io.PrintStream (the class of System.out) has a special print-method for char[], but not for int[]. So for the char[], this special method is used, while int[] is printed via the generic version, which prints the hashcode (or, to be more precise, the result of String.valueOf() called with the object as parameter).
Simply because there's no method for which handles int[] specially. It would be printed by String#valueOf() instead which implicitly calls Object#toString(). If the Object#toString() isn't overridden in the given object type, then the following will get printed (as per the aforelinked API).
getClass().getName() + '#' + Integer.toHexString(hashCode())
The int[] class has a name of [I.
To achieve what you want, you need Arrays#toString() instead:
int[] arr = {4, 5, 6};
System.out.println(Arrays.toString(arr)); // [4, 5, 6]
In the first case the character array is just used like a string (which is in fact also just an array of characters).
In the second it has no overload for the type of integer array and just prints out the object reference.
I think that the first one is treated as a CharSequence... like a String.
Related
I'm confused about this issue. Platform is on win7 java8.
Sample code:
String encryptedData = "0019ZfGO0nefTb2kIuHO0M3hGO09ZfGF";
Base64.Decoder decoder = Base64.getDecoder();
byte[] dataByte = decoder.decode(encryptedData);
System.out.println(dataByte);
dataByte = decoder.decode(encryptedData);
System.out.println(dataByte);
The output:
[B#15db9742
[B#6d06d69c
The exact input got different result.
Don't know if there's anyway to clear the status and make the result consistent every time?
Thanks!
In Java, arrays don't override toString(), so if you try to print one directly, you get the "className + # + the hex of the hashCode of the array", as defined by Object.toString()
Note:Just printing the array by reference variable means you are calling the toString() method of that array object.
As decoder.decode(encryptedData) returns a new byte[] every-time, therefore it gives a different value when you just print the reference variable.
Ex: System.out.println(dataByte);//output:[B#15db9742
You can use the standard library functions to print the contains of the array. There are many ways to achieve this. Just some examples are below:
System.out.println(Arrays.toString(dataByte));
System.out.println(dataByte.toList());
Why does my toString method have an error?
String score1 = Arrays.toString(newGame.top3Score[0]);
I am trying to take out the array value then convert them into strings.
Arrays.toString is expecting an array, yet you are only passing one element, try using:
Arrays.toString(newgame.top3Score)
This question already has answers here:
If a char array is an Object in Java, why does printing it not display its hash code?
(6 answers)
Closed 5 years ago.
When I run the following code I get the address of the array:
int arr[] = {2,5,3};
System.out.println(arr); // [I#3fe993
But when I declare a character array and print it the same way it gives me the actual content of the array. Why?
char ch[] = {'a','b','c'};
System.out.println(ch); // abc
Class PrintStream (which is what System.out is) has a dedicated method overload println(char[]) which prints the characters of a char array.
It has no special overloads for other arrays, so when you pass an int[] the called method is println(Object). That method converts the passed object to a string by calling its toString() method.
The toString() method for all arrays is simply the default one inherited from class Object, which displays their class name and default hashcode, which is why it's not so informative. You can use Arrays.toString(int[]) to get a string representation of your int array's contents.
P.S. Contrary to what the doc says, the default hashcode of an object is not typically the object's address, but a randomly generated number.
When you say
System.out.println(ch);
It results in a call to print(char[] s) then println()
The JavaDoc for println says:
Prints a character and then terminate the line. This method behaves as though it invokes print(char) and then println().
A integer variable is not char, so the print(int[] s) get the address of array.
I am trying to output information with System.out.format using a double[] array as the argument. This does not work:
out.format("New dimensions:\n" +
"Length: %f\n" +
"Width: %f\n\n",
doubleArray);
This, however, does:
out.format("New dimensions:\n" +
"Length: %f\n" +
"Width: %f\n\n",
doubleArray[0], doubleArray[1]);
Why doesn't the first format work? It supposedly works with strings just fine.
Java will autobox your double to a Double, but it won't autobox your double[] to a Double[], so it doesn't match Object[]. As a result, instead of being unpacked into the Object... varargs, your array is being treated as the array itself -- which, obviously, can't be formatted as a double.
If you declare your array as Double[] instead of double[], the call to format works.
It doesn't work because an array isn't a double(in Java an array is a class, so it's like a general pointer here). You need to specify exactly what will be outputted, and you did - it's the %f format specifier. ArraySomething[] doesn't match..
See here for more on Java's Formatting and here - How does array class work in Java? , for Java arrays.
System.out.println("hello world".getBytes("UTF-8"));
occasionally returns a different value, why is that??
Sorry, I'm still a noob at Java.
This code prints an array (byte[]), but there is no standard array printing in Java. So instead of printing the content of the array, the code displays some cryptic memory reference to the array. Eg "[B#6bbc4459". This information is not very useful and is likely to change between programm executions.
If you want to display the content of the array, you must iterate through it.
You're printing the result of calling toString() on a byte array. That doesn't show you the contents, as arrays don't override toString() - it's just showing you something like [B#ABCDEF01 where the [B shows that it's a byte array, and the value after the # is a hash code.
If you want to show the byte array contents as numbers, you want something like Arrays.toString:
byte[] data = "hello world".getBytes("UTF-8");
System.out.println(Arrays.toString(data));