ann dro
ann dro

Reputation: 135

Is 5 * 5 a literal or just an expression?

I know that for example '5' is literal of type char but is 5 * 5 a literal? This is expression, however it has fixed value.

Upvotes: 5

Views: 143

Answers (4)

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

I'm not sure, but doesnt that terms work something like that?

  • while you use some value to define exactly value of variable "in straight way" it should be called as "literal"

    var num =5; var stringLabel = "someText"; etc.

  • while you use some formula or prescription it should be called as "expression"

    var result = 5*5; var res2 = a*x;

But of course- in result that also constant si it also can be literal in fact ..

That's how get it at least me..

It's just about terms- also like in java there are methods, "C languages" are functions, etc. - in meanings its also the same things, so it depends just on the limit what will you tolerate at all :D

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200168

"Literal" is a grammatical term, not semantical. It is the basic building block that denotes a constant value.

Although by specification 5 * 5 and 25 mean exactly the same, grammatically the first one is a binary expression involving two literals and an operator, and the second one is just a literal.

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100239

As described in Java Language Specification §15.28, it's a particular case of expression called "compile-time constant expression". It's not a literal.

Upvotes: 12

cgarner
cgarner

Reputation: 134

I don't think you can call it a literal. It's possible that some compilers will treat it the same way they deal with a literal, see the comment about which language you are using.

Upvotes: 1

Related Questions