Java Array Basic Questions

Asked 2 years ago, Updated 2 years ago, 42 views

class CodeRunner{
    public static void main(String[] args){

        char[] hex = {'C', 'A', 'F', 'E'};
        String[] binary = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
        String result="";

        for (int i=0; i<hex.length; i++) {
            if (hex[i]>='0' && hex[i]<='9'){
                result +=binary[hex[i]-'0'];
            }//end of if
                else {// If it's between 'A' and 'F',
                    result +=binary[hex[i]-'A'+10];
                }//end of else
        }//end of i
                                System.out.println("hex: "+  new String(hex));
                                System.out.println("binary: "+ result);
    }
}

This is the basic example code for array.p>

System.out.println ("hex: "+ new String(hex))); I wonder why the string should be brought as a new string in this part Is it necessary for transferring the char type to String?

java array string

2022-09-22 11:09

1 Answers

Since hex is a character array, the address value will be stamped if you print it out directly.

I think the new String was used to print out the actual value, not the address value.


2022-09-22 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.