CompileTime Vs RunTime Resolution of Strings
Compile-Time Resolution of Strings:
When Strings are created with the help of String literals and ‘+’ operator, they get concatenated at compile time. This is referred to as Compile-Time Resolution of Strings. Compiler eliminates the concatenation operator and optimizes the string.
Example:
Consider the below code:
String str = "Geeks " + "for" + "Geeks"; |
The above code gets optimized at by the Compiler through Compile-Time Resolution of Strings as:
String str = "GeeksforGeeks"; |
RunTime Resolution of Strings:
When Strings are created with the help of String literals along with variables and ‘+’ operator, they get concatenated at runtime only, as the value of the variables cannot be predicted beforehand. This is referred to as the RunTime Resolution of Strings.
Example:
Consider the below code:
String str = "Geeks " + var + "Geeks"; |
The above code cannot be optimized at by the Compiler at Compile-Time as the value of variable ‘var’ is unknown. Hence RunTime Resolution of Strings occurs here.
Different scenarios and identifying the type of resolution:
- Suppose the String is defined using a StringBuffer:
String str = (newStringBuffer()).append("Geeks").append("for").append("Geeks").toString();chevron_rightfilter_noneType of String Resolution: Run-Time Resolution of String
Here the compiler cannot resolve at compile time because object creation is a runtime activity. Hence the above string will be resolved at run-time.
- Suppose the String is defined using a StringBuffer:
String str ="Geeks"+" "+"for"+" "+"Geeks";chevron_rightfilter_noneType of String Resolution: Compile-Time Resolution of String
Here the compiler can resolve at compile time because given Strings are String Literals. Hence the above string will be resolved at compile-time.
- Suppose the String is defined in a return statement:
publicstaticString func(String var){return"Geeks"+ var +"Geeks";}chevron_rightfilter_noneType of String Resolution: Run-Time Resolution of String
Here the compiler cannot resolve at compile time because the value of the variable ‘var’ is a runtime activity. Hence the above string will be resolved at run-time.
Recommended Posts:
- How is CompileTime classpath different from RunTime classpath in Java?
- How to create a CSS rule or class at runtime using jQuery ?
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Calling an External Program in Java using Process and Runtime
- Java.lang.Runtime class in Java
- JavaScript | Strings
- Strings in Java
- C# | Arrays of Strings
- How to get a substring between two strings in PHP?
- Check if two strings are permutation of each other
- Compare two Strings in Java
- How to trim all strings in an array in PHP ?
- How to sort strings in JavaScript?
- Find all palindromic sub-strings of a given string | Set 2
- Compact Strings in Java 9 with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

