Why is the result of adding a number to a string? (interview question)

Source: Internet
Author: User
Why is the result of adding a string and a number (interview question) a few days ago, I attended an interview and wrote a running result. The question is probably a string and a number are assigned to a variable, output variable: $ test & nbsp; linux & nbsp; + & nbsp; 6; echo & nbsp; $ test; after running the command, the result is actually the following number, but I still don't understand why. please give me some guidance. thank you. ------ Solution-why is the result of adding a number to a string? (interview question)
I took an interview a few days ago and wrote a question and the running result. The question is probably a string with a number assigned to a variable, and the output variable is:
$ Test = 'Linux '+ 6;
Echo $ test;
Later, I ran the command and learned that the result is actually the following number, but I still don't understand why. please give me some guidance. thank you.

------ Solution --------------------
$ Test = 'Linux '+ 6;
You are doing addition!
In addition, strings must be converted to numbers before calculation.
Strings starting with a number can only be converted to 0.
That is:
$ Test = 'Linux '+ 6;
Equivalent
$ Test = 0 + 6;
So echo $ test; output 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.