Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between keyword and literal in java

I am new to java and was reading a book and came across these lines:

"The literals true,falseand null are lowercase,not uppercase as in C++ language.Strictly speaking,these are not keywords but literals."

why these are literals, and what requirements are needed for some keywords to be called literal..?

like image 452
sum2000 Avatar asked Dec 17 '11 14:12

sum2000


People also ask

What is difference between literal and operator in Java?

The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator.

What is the difference between keyword and identifier in Java?

A keyword contains only alphabetical characters. An identifier can consist of alphabetical characters, digits and underscores. They help to identify a specific property that exists within a computer language.

What is keyword or identifier or literal in Java?

Keywords are predefined, reserved words used in Java programming that have special meanings to the compiler. For example: int score; Here, int is a keyword. It indicates that the variable score is of integer type (32-bit signed two's complement integer).


1 Answers

Keywords are words that are used as part of code structure, like for or while. They change the way a compiler handles a block of code, e.g. a for tells the compiler to execute the code within the specified scope repeatedly, until the given exit condition is reached. The class keyword tells the compiler to treat everything within the specified scope to be part of a particular class. Keyword names are restricted, so you can't use them as variable names.

Literals like true, false and null are values that can be assigned, but their names are restricted in the same way that keywords are, i.e. you can't have a variable called true or for. They form parts of expressions, but don't change the way a compiler handles code.

like image 103
Polynomial Avatar answered Sep 24 '22 15:09

Polynomial