Storing property value names as String constants - performance and memory usage?

I use about 1000 properties related to a specific java.util.Properties, which is supported by the file. The main reason for the file is to change them without recompiling the program and let users customize them to their liking. Some of the properties are used only in one place in the code, but there are some properties that are used several times in different segments of the code and even different classes.

I recently got the habit of declaring all those properties that are used as String constants, usually in a separate interface:

public interface AnimalConstants {
  public static final String WEIGHT_PROPERTY = "weight";
  public static final String RUNNING_SPEED_PROPERTY = "speedInKph";
  public static final String HOURS_OF_SLEEP_A_DAY_PROPERTY = "sleepHrs";
  ...
}

When a class needs to access some properties of an animal, I simply implement this interface, and I have access to all of these property constants declared. When I need a specific property, I just use the appropriate constant without worrying about what its exact name is (because abbreviations are often used) and, more importantly, the risk of mistakenly defining a property name is eliminated in this way. Another advantage is that if I go to the property later to make it more understandable for the advanced user who sets these properties), I just need to change this name in the interface where this property constant is declared (and, of course, properties file), so there is no need to “search and replace” the entire project. Finally, I can easily check if a property is being used or not;I just comment on it, compile the code and see if there is an error.

, , , . :

  • (1000 ) ? , ? ?
  • , String-, ( )? ?
  • , String ?

/ .

+3
2

1) (1000 ) ?

- . "String Literal Pool" . , ( String ).

2) , ?

"String Literal Pool" .

3) , ?

.

4) , String-, ( )? ?

.

5). , String ?

.:)

a) - - Java. , .

b) Javaranch.

+5

.

String , , . . (, , pi 3.975), , , . String , .

Properties ( ). . .

:

, . , . , - . . , . , . , ( ), , JAVA Properties. , JDBC, ..

+7

Source: https://habr.com/ru/post/1757547/


All Articles