Why is `+ 1` a valid expression in Java? [duplicate]
This question already has an answer here:
What is the purpose of Java's unary plus operator?
7 answers
The following code block
class Main {
public static void main(String args) {
System.out.println( + 1);
}
}
Compiles on java 1.8.
When this code is run 1
is printed.
Same with System.out.println(+ + 1);
However ++1
fails to compile.
+ + "str"
fails to compile.
+ + true
fails to compile.
So it looks like it is supported only for int, long and double.
What is the reason that this expression is valid for the above data types?
java
marked as duplicate by DYZ, Community♦ Jan 3 at 5:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is the purpose of Java's unary plus operator?
7 answers
The following code block
class Main {
public static void main(String args) {
System.out.println( + 1);
}
}
Compiles on java 1.8.
When this code is run 1
is printed.
Same with System.out.println(+ + 1);
However ++1
fails to compile.
+ + "str"
fails to compile.
+ + true
fails to compile.
So it looks like it is supported only for int, long and double.
What is the reason that this expression is valid for the above data types?
java
marked as duplicate by DYZ, Community♦ Jan 3 at 5:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What do you expect to happen by doing++1
?
– Nicholas K
Jan 3 at 5:54
++1
failing is fine since there is no variable to increment and store, however I am wondering why+ + 1
compiles.
– Prathik Rajendran M
Jan 3 at 5:55
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
1
is a v+1
that can also be written like+1
and similarly -ve can be written as-1
, (but not+1
is an expression but just1
is a literal) for negation en expression result you can put-
minus in front of expression as-exp
=> to negate result of expression+1
you can write-+1
...now this should give you an idea why++1
is valid and also+-1
or-----1
is valid
– Grijesh Chauhan
Jan 3 at 5:58
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37
add a comment |
This question already has an answer here:
What is the purpose of Java's unary plus operator?
7 answers
The following code block
class Main {
public static void main(String args) {
System.out.println( + 1);
}
}
Compiles on java 1.8.
When this code is run 1
is printed.
Same with System.out.println(+ + 1);
However ++1
fails to compile.
+ + "str"
fails to compile.
+ + true
fails to compile.
So it looks like it is supported only for int, long and double.
What is the reason that this expression is valid for the above data types?
java
This question already has an answer here:
What is the purpose of Java's unary plus operator?
7 answers
The following code block
class Main {
public static void main(String args) {
System.out.println( + 1);
}
}
Compiles on java 1.8.
When this code is run 1
is printed.
Same with System.out.println(+ + 1);
However ++1
fails to compile.
+ + "str"
fails to compile.
+ + true
fails to compile.
So it looks like it is supported only for int, long and double.
What is the reason that this expression is valid for the above data types?
This question already has an answer here:
What is the purpose of Java's unary plus operator?
7 answers
java
java
asked Jan 3 at 5:52
Prathik Rajendran MPrathik Rajendran M
891616
891616
marked as duplicate by DYZ, Community♦ Jan 3 at 5:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by DYZ, Community♦ Jan 3 at 5:59
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What do you expect to happen by doing++1
?
– Nicholas K
Jan 3 at 5:54
++1
failing is fine since there is no variable to increment and store, however I am wondering why+ + 1
compiles.
– Prathik Rajendran M
Jan 3 at 5:55
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
1
is a v+1
that can also be written like+1
and similarly -ve can be written as-1
, (but not+1
is an expression but just1
is a literal) for negation en expression result you can put-
minus in front of expression as-exp
=> to negate result of expression+1
you can write-+1
...now this should give you an idea why++1
is valid and also+-1
or-----1
is valid
– Grijesh Chauhan
Jan 3 at 5:58
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37
add a comment |
What do you expect to happen by doing++1
?
– Nicholas K
Jan 3 at 5:54
++1
failing is fine since there is no variable to increment and store, however I am wondering why+ + 1
compiles.
– Prathik Rajendran M
Jan 3 at 5:55
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
1
is a v+1
that can also be written like+1
and similarly -ve can be written as-1
, (but not+1
is an expression but just1
is a literal) for negation en expression result you can put-
minus in front of expression as-exp
=> to negate result of expression+1
you can write-+1
...now this should give you an idea why++1
is valid and also+-1
or-----1
is valid
– Grijesh Chauhan
Jan 3 at 5:58
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37
What do you expect to happen by doing
++1
?– Nicholas K
Jan 3 at 5:54
What do you expect to happen by doing
++1
?– Nicholas K
Jan 3 at 5:54
++1
failing is fine since there is no variable to increment and store, however I am wondering why + + 1
compiles.– Prathik Rajendran M
Jan 3 at 5:55
++1
failing is fine since there is no variable to increment and store, however I am wondering why + + 1
compiles.– Prathik Rajendran M
Jan 3 at 5:55
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
1
is a v+ 1
that can also be written like +1
and similarly -ve can be written as -1
, (but not +1
is an expression but just 1
is a literal) for negation en expression result you can put -
minus in front of expression as -exp
=> to negate result of expression +1
you can write -+1
...now this should give you an idea why ++1
is valid and also +-1
or -----1
is valid– Grijesh Chauhan
Jan 3 at 5:58
1
is a v+ 1
that can also be written like +1
and similarly -ve can be written as -1
, (but not +1
is an expression but just 1
is a literal) for negation en expression result you can put -
minus in front of expression as -exp
=> to negate result of expression +1
you can write -+1
...now this should give you an idea why ++1
is valid and also +-1
or -----1
is valid– Grijesh Chauhan
Jan 3 at 5:58
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37
add a comment |
3 Answers
3
active
oldest
votes
This is unary plus expression. It is here just to compliment unary minus expression.
Only numerical type suports it because for other types it doesn't make any sense.
++1
is not compiles because ++
is increment expression and requires variable or field as sub expression.
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
add a comment |
+1
is not an expression, it's an explicit way to say positive 1
. On the other hand, ++1
is a pre-increment expression on variable 1
, which doesn't exist, nor is it legal to have a variable name start with a digit. + + 1
is equivalent of +(+(1))
.
add a comment |
Because +
separated by space is treated as unary operator
For example
- - 5 => -(-5) => 5
Similarly
+ + 5 => +(+5) => 5
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is unary plus expression. It is here just to compliment unary minus expression.
Only numerical type suports it because for other types it doesn't make any sense.
++1
is not compiles because ++
is increment expression and requires variable or field as sub expression.
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
add a comment |
This is unary plus expression. It is here just to compliment unary minus expression.
Only numerical type suports it because for other types it doesn't make any sense.
++1
is not compiles because ++
is increment expression and requires variable or field as sub expression.
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
add a comment |
This is unary plus expression. It is here just to compliment unary minus expression.
Only numerical type suports it because for other types it doesn't make any sense.
++1
is not compiles because ++
is increment expression and requires variable or field as sub expression.
This is unary plus expression. It is here just to compliment unary minus expression.
Only numerical type suports it because for other types it doesn't make any sense.
++1
is not compiles because ++
is increment expression and requires variable or field as sub expression.
answered Jan 3 at 5:56
talextalex
11.7k11648
11.7k11648
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
add a comment |
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
Got it, this answer helps as well stackoverflow.com/questions/2624410/…
– Prathik Rajendran M
Jan 3 at 5:57
add a comment |
+1
is not an expression, it's an explicit way to say positive 1
. On the other hand, ++1
is a pre-increment expression on variable 1
, which doesn't exist, nor is it legal to have a variable name start with a digit. + + 1
is equivalent of +(+(1))
.
add a comment |
+1
is not an expression, it's an explicit way to say positive 1
. On the other hand, ++1
is a pre-increment expression on variable 1
, which doesn't exist, nor is it legal to have a variable name start with a digit. + + 1
is equivalent of +(+(1))
.
add a comment |
+1
is not an expression, it's an explicit way to say positive 1
. On the other hand, ++1
is a pre-increment expression on variable 1
, which doesn't exist, nor is it legal to have a variable name start with a digit. + + 1
is equivalent of +(+(1))
.
+1
is not an expression, it's an explicit way to say positive 1
. On the other hand, ++1
is a pre-increment expression on variable 1
, which doesn't exist, nor is it legal to have a variable name start with a digit. + + 1
is equivalent of +(+(1))
.
answered Jan 3 at 5:58
JaiJai
5,88411233
5,88411233
add a comment |
add a comment |
Because +
separated by space is treated as unary operator
For example
- - 5 => -(-5) => 5
Similarly
+ + 5 => +(+5) => 5
add a comment |
Because +
separated by space is treated as unary operator
For example
- - 5 => -(-5) => 5
Similarly
+ + 5 => +(+5) => 5
add a comment |
Because +
separated by space is treated as unary operator
For example
- - 5 => -(-5) => 5
Similarly
+ + 5 => +(+5) => 5
Because +
separated by space is treated as unary operator
For example
- - 5 => -(-5) => 5
Similarly
+ + 5 => +(+5) => 5
answered Jan 3 at 5:59
DocDoc
8721417
8721417
add a comment |
add a comment |
What do you expect to happen by doing
++1
?– Nicholas K
Jan 3 at 5:54
++1
failing is fine since there is no variable to increment and store, however I am wondering why+ + 1
compiles.– Prathik Rajendran M
Jan 3 at 5:55
BTW I stumbled upon this while adding string with a character, that is "test" + + '}' is a valid expression and I got to thinking why its allowed.
– Prathik Rajendran M
Jan 3 at 5:56
1
is a v+1
that can also be written like+1
and similarly -ve can be written as-1
, (but not+1
is an expression but just1
is a literal) for negation en expression result you can put-
minus in front of expression as-exp
=> to negate result of expression+1
you can write-+1
...now this should give you an idea why++1
is valid and also+-1
or-----1
is valid– Grijesh Chauhan
Jan 3 at 5:58
@GrijeshChauhan i think you have misspelled your comment in the end b/w invalid and valid. '....why ++1 is valid and also +-1 or -----1 is valid ' .
– Jabongg
Jan 3 at 7:37