Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change an array to a string without creating a string

I wish to accomplish:

    String []beef = new String[3];
    beef[0] = "Water";
    beef[1] = "Chicken";
    beef[2] = "Paper";

    String empo = Arrays.toString(beef);

    if (empo.isEmpty()){
        empo = "Nothing";
        System.out.println(empo);
    }else{
        System.out.println(empo);
    }

without having to create the string.


Something like:

    String []beef = new String[3];
    beef[0] = "Water";
    beef[1] = "Chicken";
    beef[2] = "Paper";

    Arrays.toString(beef);  //change beef to just a plain string

    if(beef.isEmpty()||beef==""){
    no = "Nothing";

    System.out.println(beef);

How would one go about doing this?

like image 904
House3272 Avatar asked Apr 28 '26 00:04

House3272


1 Answers

You can't.

Java is a strongly and statically typed language. That means you have to tell it what type a thing will be when you declare it (strong typing), and you can't ever change it's type after that (static typing).

You will just have to create a new String.

like image 84
tckmn Avatar answered Apr 30 '26 13:04

tckmn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!