#How to return a string from an array of characters

1 messages Β· Page 1 of 1 (latest)

neon mural
#

Hello guys, consider this question:

Which of the following is the correct statement to return a string from an array a of characters?
Select one:
A. New String(a)
B. toString(a)
C. convertToString(a)
D. String.toString(a)

Can someone explain which answer is correct, why and why the others are incorrect please; according to chatgpt, the answer is a but I don't understand why... also I don't have the correct answer so I don't know if chatgpt is correct.

Thanks in advance

quiet walrusBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question πŸ€–. Maybe it helps! In any case, a human is on the way πŸ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

A. String str = new String(a);

primal tangle
#

A is correct, if you write new instead of New

neon mural
kind vigil
#

Why any of them would be correct or wrong And why?

primal tangle
neon mural
#

hmmm I would choose between A or B... A is creating a new object of type String and passing as arguments the array of characters... B is converting the array of characters into a string

kind vigil
primal tangle
#

I know from experience that new String(char[]) exists so I immediately knew A is the answer. All the other options do make sense, but it's just a matter of checking what's correct and what isn't.

neon mural
kind vigil
kind vigil
primal tangle
neon mural
#

I know that option B is used to convert an object into a string

kind vigil
neon mural
#

Ok, will read some documentation then, thanks guys, really appreciate πŸ‘

kind vigil
#

B and c are not valid java

neon mural
#

if B is incorrect so D also since it uses the toString method, right ?

kind vigil
#

B and c

#

You can't have a function in nothingness in java

#

You must put it in a class

kind vigil
#

It's just... that java doesn't have such method

neon mural
#

yeah I see, thanks !!

neon mural
primal tangle
# neon mural one last question.... can we say that we can easily get rid of B and C because h...

You are correct, let me explain πŸ™‚
When you call a method, Java searches it in 2 places:

public class User
{
    public void sayHi()
    {
        //1. in the class itself
        say("Hi!");
    }

    public void printSelf()
    {
        //2. in the Object class, which every class inherits
        System.out.println(toString());
    }

    private void say(String message)
    {
        System.out.println("Message > " + message);
    }
}

If the method was not defined in these places - the code cannot be compiled.
We can safely ignore choices B and C because the Object class doesn't offer either.

quiet walrusBOT
primal tangle
#

I edited the first sentence, it was incorrect.

#

You're welcome πŸ™‚

neon mural
#

By the way, how do we do to copy and paste a code from our IDE in discord? I mean like you have done above, it keeps the colour etc

primal tangle
#

Discord supports auto formatting for many languages

neon mural
#

oh okay didn't know how to do that, thanks once more !!