官术网_书友最值得收藏!

Difference between strings and variables

In Chapter 1, First Programming Steps in Java, we printed a string and the variable. If we look closely, when we print a variable we are not using double quotes, but when we print a string, we are using them. This is because the value is already present in the variable, hence we need not use any double quotes. If we use them, Java considers it to be a string and the output will be printed as the letter a in the following example. If we run this and observe the output, the letter a will be printed as shown in the following screenshot:

Output displaying the value of a as per the code

If we don't use double quotes, Java will check whether there is any variable defined with this letter. If so, it prints the value present in that variable. If there is no variable defined, then it gives an error. If we comment out the variable declaration, what we see is an error. Hovering the mouse over the variable, we will get a prompt saying Create a local variable 'a', or we can use it by adding double quotes:

Quick fixes drop down with suggestions to correct the code error

In short, if we simply use double quotes, the variable will be treated as a string, but if we don't use double quotes, we must declare the variable somewhere. That is the difference between printing out strings and printing out variables.

主站蜘蛛池模板: 孙吴县| 鱼台县| 思茅市| 清流县| 会理县| 乌审旗| 即墨市| 巧家县| 吉林市| 双柏县| 保亭| 揭阳市| 平远县| 黑水县| 广元市| 荆州市| 拜泉县| 金山区| 博爱县| 崇义县| 东阳市| 锡林郭勒盟| 扎鲁特旗| 施甸县| 沾益县| 东源县| 泉州市| 山东| 广河县| 永丰县| 通辽市| 河源市| 饶阳县| 车险| 江西省| 呼和浩特市| 昭平县| 科技| 阿鲁科尔沁旗| 神农架林区| 北川|