Prev Next

Java / Java Identifiers

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Java Identifiers.

Identifiers are the names of variables, methods, classes, packages and interfaces.

In the below example, MyClass, MyInterface, method_name, packagename and intVariable are identifiers.

class MyClass {}.

interface MyInterface {}.

void method_name() {}.

package packagename;

int intvariable;

2. Is null a keyword?

The null is not a keyword. null is a literal as true or false.

3. Can a digit form the first character of an identifier?

No. Only characters are allowed. However they may be used after the first character.

4. Explain var keyword in Java.

var keyword is introduced in Java 10 to enhance the Java Language to extend type inference to declarations of local variables with initializers.

var map = new HashMap<String, Integer>();

var is not a keyword in Java, but rather it is a reserved type name. var cannot be used for fields, return types, class names, or interface names.

var can only be used for implicit type inference in the following contexts.

  • local variables with initializers.
  • indexes in the enhanced for-loop.
  • locals declared in a traditional for-loop.

Since var is a reserved type name, it can still be used for package name, method name, and variable name along with its new type-interference role. For example, the following are all examples of valid uses of var in Java.

var map = new HashMap<String, Integer>();
var k= 0;
var var = 10;
for (var i = 0; i < 100; i++) { /* ... */ }
public int var() { return -1; }
package var;

Comments & Discussions