How to make copy of a string in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java";
        String copy = s;
         
        s = "c++";
        
        System.out.println(s);
        System.out.println(copy);
    }
}
 
 
 
 
/*
run:
 
c++
java
 
*/

 



answered Feb 6, 2021 by avibootz
edited Feb 7, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java";
        String copy =  new String(s);
         
        s = "c++";
        System.out.println(s);
        System.out.println(copy);
    }
}
 
 
 
 
/*
run:
 
c++
java
 
*/

 



answered Feb 6, 2021 by avibootz
edited Feb 7, 2021 by avibootz

Related questions

1 answer 105 views
2 answers 166 views
166 views asked Feb 7, 2021 by avibootz
2 answers 211 views
...