Overview
1+1=2 in mathematics, 1+1=2 in the program, and 1+ "1" =?
Around the problem of 1+ "1", let's think about this problem.
Directory:
One, in the. NET code
Second, in the JavaScript code
Third, in SQL Server code
Iv. in the PowerShell code
Five, there are more languages worthy of our discussion
one, in the. NET Code(1) The code looks like this:
Console.Write ("Example 1:"); Console.WriteLine ("1 +"+'"'+"1"+'"'+" = ?");intA =1; Console.WriteLine ("a = {0}", a); Console.WriteLine ("type of a: {0}", A.gettype ());stringb ="1"; Console.WriteLine ("B ="+'"'+"1"+'"'); Console.WriteLine ("type of B: {0}", B.gettype ()); Console.WriteLine ("A + b = {0}", A +b); Console.WriteLine ("type of a + B: {0}", (A +b). GetType ()); Console.WriteLine (); Console.WriteLine (); Console.Write ("Example 2:"); Console.WriteLine ("1 + ' 1 ' =?"); Console.WriteLine ("a = {0}", a); Console.WriteLine ("type of a: {0}", A.gettype ());Charc ='1'; Console.WriteLine ("C ="+"' 1 '"); Console.WriteLine ("type of C: {0}", C.gettype ()); Console.WriteLine ("A + c = {0}", A +c); Console.WriteLine ("type of A + C: {0}", (A + C). GetType ());
View Code(2) Operation result:
(3) Conclusion:
In the. NET code
- The int type and string type are "+" operations, the int type is cast by default to string type, so the final result is a concatenation of two string strings, so the result is "1" + "1" = "11";
- The int type and char type do a "+" operation, and the char type will default to the ASCII code corresponding to the value (char type ' 1 ' corresponds to the ASCII code value of 49), and the value is int type, so the final result is a two int type to add the string, so the result is 1+ 49=50;
(4) Appendix I: ASCII Code table
second, in the JavaScript code
(1) The code looks like this:
//example 1:1 + "1" =? varA = 1; varb = "1"; varresult = A +b; alert (result); //example 2:1 + ' 1 ' =? varc = ' 1 '; varresult = A +C; alert (result); //Example 3:1 + ' a ' =? varc = ' a '; varresult = A +C; alert (result); </script><body></body>(2) Operation result:Example 1, example 2 operation results are 11,1
Figure 1
Example 3 operation result is 1a,2
Figure 2
Figure 3
(3) Conclusion:In the JavaScript code
- According to figure three can be judged that JavaScript does not have a char type;
- The int type and string type are "+" operations, the int type is cast by default to string type, so the final result is a concatenation of two string strings, so the result is "1" + "1" = "11";
- This form of ' 1 ' is parsed into a string type, so the result of 1+ ' 1 ' is the same as the result of 1+ "1".
third, in SQL Server code(1) The code looks like this:----Example 1--declare @a int--declare @b char--Set @a=1--set @b= ' 1 '--print ' Example 1 results: '--print @[email protected]--2----Example 2--declare @c char--set @c= '--print ' Example 2 results: '--print @[email protected]--2----Example 3--declare @d char--set @d= ' d '--print ' Example 3 results: '--print @[email protected]--failed when the varchar value ' d ' was converted to a data type int. --Example 4Print 'Example 4 results:'Select 1+'1' as 'Example 4 results'--Example 5Print 'Example 5 results:'Select 1+' A' as 'Example 5 results'
(2) Operation result:(3) Conclusion:In SQL Server code
- There is no string of this type;
- From Example 1, example 2 can be introduced: a variable of type int and char type for "+" operation, the char type will be cast by default the first character is an int type, the following word Fusche, so the final result is two int type to add the string, so the result is 1+ ' 1 ' =2,1+ ' 12 ' = 2;
- By example 3 can be introduced, the type int variable and char type "+" operation, when the value of char type is not composed of 0~9 these numbers, will be an error: when the varchar value ' d ' is converted to the data type int, the failure;
- By Example 4, example 5 can be introduced: When using a select query, the type of int variable and char type "+" operation, the char type will be cast by default int type, so the final result is two int type to add, so the query out of the collection is 1+ ' 1 ' =2,1+ ' 12 ' = 13.
Iv. in the PowerShell code(1) The code looks like this:#Example 1 1+ ' 1 ' =?$a=1$b= ' 1'$a+$b#Example 2 ' 1+ ' =?$a=1$b= ' 12'$a+$b#Example 3 1+ "1" =?$a=1$c="1"$a+$c#Example 3 1+ "a" =?$a=1$d="a"$a+$d
(2) Operation result:(3) Conclusion:In the PowerShell code
- There is no char type, and "is the same identifier that identifies a string type.
- The int type and string type are "+" operations, and the string type is cast by default to type int, so the final result is a two int type to add, so the result is 1+ ' 1 ' =2,1+ "1" =2,1+ "12" = 13;
Five, there are more languages worthy of our discussionAs shown in the following:
"Question and thought" 1+ "1" =?