开发者

Confusion with Literal term in Java

开发者 https://www.devze.com 2023-04-01 14:46 出处:网络
I always come across the term literal. What is that? Is it that eve开发者_如何学Gory primitive type variable is called as literal?Literal is any value, fixed, constant, used in your code.

I always come across the term literal. What is that?

Is it that eve开发者_如何学Gory primitive type variable is called as literal?


Literal is any value, fixed, constant, used in your code.

For example: int a =0 ;

0 is a literal. The value of "0" is literally 0.


Java Language Specification §3.10:

A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or the null type (§4.1)...


A literal is a string that is in quotes - basically a string in and of itself, not in a variable:

string variable = "This is the literal section of the string";

This makes the distinction between a string variable and a string literal. You need variables in your code, but you would like to remove literals out to an external file, so they are not compiled with the application and you can change them on the fly.


It comes down to assigning a value to a variable without the use of the new keyword (or referring to an already existing object).

See Oracle's tutorial on primitive datatypes. Even though I'd try to refer to Oracle these days, the references in the other answers seem to be more exhaustive (also pointing to true and false for example).

Cheers, Wim

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号