I have to set the value of a string object "result" depending on the results of different methods and different if/else conditions. In the end, there would be one (last value set) in the string that i need to use. The first way i did it is:
public void MyClass(){
public void validateApples() {
String result = "failure";
if (condition 1) {
//do something
}
if (condition 2) {
//do something
}
if(condition 3) {
//do something
}
if (condition 4) {
if (condition a)
result = "success";
} else{
//One of the first three conditions resulted in failure so don't change string value
}
}
if (condition 5) {
//Do validation only for xml files
result = validateMachintoshApples ();
}
}
private String validateMachintoshApples() {
String result;
result = oneStepValidation();
return result;
}
private String oneStepValidation(){
String result;
if (some condition) {
// some code
result = "success";
}else{
// some code
result = "failure";
}
return result;
}
}
I feel that i am recreating a new String object many times in different methods of the same class. I don't think its efficient in terms of memory, object creation time and coding standards.
I than moved on to creating one string object "result" for the whole class and assign it "failure" or "success" as i think that would be more clean code.(see code below)
1) But since Stings are immutable in JAVA, will I achieve something this way?
2) Am i still creating new String each time with result = "failure" or result = "success" or is it only a value change? I doubt it is a value change as strings are immutable.
3) Is the second way less readable since the functions are setting the value for result but not returning anything?
4) What should be the best practice in this case?
My second way of coding looks like:
public void MyClass(){
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public void validateApples() {
if (condition 1) {
//do something
result = "failure";
}
if (condition 2) {
//do something
result = "failure";
}
if(condition 3) {
//do something
result = "failure";
}
if (condition 4) {
if (condition a)
result = "success";
} else{
//One of the first three conditions resulted in failure
}
}
if (condition 5) {
//Do validation only for xml files
validateMachintoshApples ();
}
}
private void validateMachintoshApples() {
result = oneStepValidation();
}
private void oneStepValidation(){
if (some condition) {
// some code
result = "success";
}else{
// some code
result = "failure";
}
}
}
result = "foobar";
does not create a new String object every time it gets executed. There will be only one "foobar" string, which is created when the class is loaded. If, for some reason you actually wanted a new string every time, then you would writeresult=new String("foobar");
enum
s forresult
values you wouldn't even have this problem.result
is a reference variable, which is effectively a pointer to a string. It is not a string object."hello"
is a string object. In your sample code, no string objects are being changed or copied, only the reference variable is being updated to point to different string objects.