I have the next example in C++:
this->name=new char[strlen(nom)+1];
So, I want to know how can I implement this example in java. The class have a :
private String name;
and I want to implement this method:
public void setNombre(String nom)
I try to do:
this.name= new String(nom.length()+1);
In C++:
this->name=new char[strlen(nom)+1];
I try to do:
this.name= new String(nom.length()+1);
Strings are immutable in java. In your C++ version it looks like you are creating an empty array of chars with the size of nom
+ 1.
You cannot assign an empty array of chars to a String in Java. Well, you can create a String full of '\ '
, but then you will never be able to modify your String after that, so I really don't see the purpose.
EDIT : I think that you just want to set the name to the value nom
so you can do:
public void setNombre(String nom){
this.name = nom;
}
But that's not what your C++ code does.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.