0
Can someone ecplain me the concatenation concept..I am.a bit confused with + & ++
7 Answers
+ 2
Java usually zeros data and might generate an error or warning. However, failure to properly initialize your data can become a impossible bug to find so it is best always do so.
+ 1
Not sure on the meaning of the question as you mention operators and concatenation, so here's a crash course.
+ and ++ are operators. The ++ operator usually means pre- or post-increment (when not overridden). It only requires an lvalue or rvalue.
ii++; // Adds 1 to ii
The + operator is pretty obvious. It requires an rvalue and lvalue:
ii + n; // Returns the sum of ii and n.
ii = ii + n; // Adds n to ii.
ii += n; // Shorthand for the above.
Concatenation is the act of joining to strings: one value appended to another. Using higher-level string handling either provided natively by a language or through functions/contains/etc, this is achieved. We'll discuss operator-specific stuff because of your question's wording:
String s1 = "Hi";
String s2 = "ya"';
s1 += s2; // s1 now reads "Hiya"
I don't recall the little Java I started learning years back so the semantics may be wrong, but conceptually in many languages, there's an operator + and += defined in the string class. For lower languages, there are functions, eg strcat and strlcat.
Semantically, the ++ operator is useless on strings, except in lower level languages (eg C) where strings are limited to boring old arrays (no containers or built-in handling -- not to say you cannot make one) traditionally and a pointer can be used to move back and forth using ++ or --. That's beyond the scope of this question, unless you'd like some example code. Back to the point, doing this (building on the previous example):
s1++;
Will probably cause an error, nothing, or "undefined behaviour".
Hope this fully clarifies whatever you were asking.
0
+ needs two arguments. It is the same like in math.
(5 + 2) gives you 7.
++ adds one to variable
int x=5
x++
now x is equal to 6.
It is avaliable to add two strings.
"Solo" + "Learn" gives you "SoloLearn"
0
what if we dont assign any value to x and y and we just write x+y
0
If x and y are variables and they are declared then the result is not defined. Don't know which language you are using but in c++ if we declare variable in main() then variable has garbage value from cache.
0
i am using java
0
However, you should avoid using not defined variables.
For example my IDE doesn't accept using undefined variables.