0

Given this case:

    String String = ""; //valid

Why is this valid?

Also, why is it that:

    int int = 0;  // is invalid

I'm baffled.

2 Answers 2

3

int is a reserved keyword. Reserved keywords may not be used as part of any formal variable name - the same is true of true, false, and null as literals. There's a list of those such keywords available.

String is a class name and cannot be a reserved keyword. This is because you cannot predict what the name of a class will be in general.

By convention, reserved keywords are lower case, variable names are camelCased, and classes are TitleCased. Following these conventions will ensure that your code doesn't run into these simple errors.

Sign up to request clarification or add additional context in comments.

Comments

2

Reserved words can't be used in variable names.

List of reserved words:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Same goes for class names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.