-1

The output of the program is:

[B@171ccb0[B@35378d
[B@1d23632

Required output is:

[B@171ccb0[B@35378d
[B@171ccb0[B@35378d

Please help...

import java.util.Scanner;
import java.io.InputStreamReader;

public class testme {
    public static void main(String[] args) {
        Scanner in = new Scanner(new InputStreamReader(System.in));
        String s = "hello";
        String sb = "hi";
        String sc = s.concat(sb);
        byte[] a, b;
        a = s.getBytes();
        b = sb.getBytes();
        byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        System.out.println(a + "" + b + "\n" + c);
    }
}
jrbedard
  • 3,662
  • 5
  • 30
  • 34
  • what's the purpose of the scanner in your example? – Martin Frank Dec 12 '16 at 10:43
  • What's the purpose of the whole program? – Kayaman Dec 12 '16 at 10:44
  • That makes no sense. You'll never see "[B" four time in your output if you only print out three arrays (toStrings of those arrays, to be precise). Also that String representation depends on the objectId of those arrays, which is not deterministic. You probably want to print the array contents and not the toString .... – Joeri Hendrickx Dec 12 '16 at 10:49
  • 1
    @MartinFrank sorry please ignore the scanner – Shridhar Chini Dec 12 '16 at 10:56
  • @Kayaman purpose is to get the byte values of the concatenated string same as values before concatenation – Shridhar Chini Dec 12 '16 at 11:01
  • @JoeriHendrickx lets say string s's byte value is 171ccb0 and string b's byte value is 35378d after concatenation value must be 171ccb035378d not 1d23632 – Shridhar Chini Dec 12 '16 at 11:07
  • Those are not "string's byte values". They're hashcodes for the byte arrays. If you want to display the contents of those arrays, use `Arrays.toString(a);`. – Kayaman Dec 12 '16 at 11:09

1 Answers1

0

In your result

[B@171ccb0, the [B means byte[] and 171ccb0 is the memory address of a variable used, separated by an @.

Similarly, in [B@35378d , 35378d is the memory address of b variable created, and

in [B@1d23632, 1d23632 is the memory address of c variable created.

if you want to know that values are concatenated or not try printing Arrays.toString method,

import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        String s = "hello";
        String sb = "hi";
        byte[] a, b;
        a = s.getBytes();
        b = sb.getBytes();

        byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);

        System.out.println(" Arrays.toString(a)= "+Arrays.toString(a));
        System.out.println(" Arrays.toString(b)= "+Arrays.toString(b));
        System.out.println(" Arrays.toString(c)= "+Arrays.toString(c));

    }
}

Output is:

Arrays.toString(a)= [104, 101, 108, 108, 111] 
Arrays.toString(b)= [104, 105] 
Arrays.toString(c)= [104, 101, 108, 108, 111, 104, 105]

Reference : How to convert Java String into byte[]?

Community
  • 1
  • 1
Chetan
  • 644
  • 6
  • 7