Introduction
Output:
Hello Rahul Here, the + operator combines two String values to create a new String.
String Concatenation
The + operator can be used to combine String values.
Output:
Rahul KumarMultiple String values can be combined in a single expression.
String with Numbers
The + operator can also be used with Strings and numbers.
Output:
Rahul is 20 years old.When a String is involved, the + operator performs String concatenation.
Order of Evaluation
The order of an expression can change the result.
Output:
Result: 1020Result: 30In the first expression, once Java encounters a String, the following + operations are treated as concatenation.
In the second expression, parentheses calculate 10 + 20 first.
String Comparison Expression
String expressions can also be used for comparison.
Output:
trueThe equals() method produces a boolean result based on the String content.
Using String Methods in Expressions
String methods can be part of an expression.
Output:
JAVA ProgrammingHere, toUpperCase() is used as part of the String expression.
String Expression with length()
A String method can also return a value that is used in another expression.
Output:
10The length() method returns 5, and then 5 is added to it.
Important Example
Output:
Name: Rahul, Age: 20, City: DelhiThis example combines multiple variables and values into one String expression.
Key Takeaways
- A String expression works with String values, variables, operators, and methods.
- The + operator is commonly used for String concatenation.
- Parentheses can be used to control the order of evaluation.
- String methods such as equals(), toUpperCase(), and length() can be used in expressions.
- String expressions can produce a String, boolean, int, or another value depending on the expression.