0

In Java, the word "String" is not a keyword. That is, we can write:

String String = "";

without the compiling error.

Also, java.lang.String class can not be used as a second parameter of the "instanceof" operator.

Why? What's so special about the String class for the Java compiler and why it isn`t a keyword?

1
  • 3
    It's not a keyword because it's not a base type. You wouldn't want every standard library class to be a reserved keyword, or else you couldn't call things Socket or File, etc. Commented Mar 5, 2012 at 22:43

3 Answers 3

3

First, String is a Class name, not a keyword. So you are free to use it as a variable name, although it's not recommended.

Second, I don't think your statement on "java.lang.String class can not be used as a second parameter of the "instanceof" operator" is correct.

I have created a test program named T2.java, it compiles and runs as expected:

import java.util.*;
import java.util.regex.*;
import java.lang.reflect.*;

public class T2 {

    public static void main(String[] args) throws Exception {
      String String = "abc";
      outputAll((String instanceof java.lang.String), String);
    }

    public static void outputAll(Object... args) {
      System.out.println();
      for (Object o: args) {
        System.out.println(o);
      }
    }

    public static void output(String msg, Object... args) {
      System.out.println(String.format(msg, args));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Of course you can use String as second parameter of instanceof. There is nothing special about the String class. The second parameter of instanceof wants a Class and the first parameter wants an expression (a variable is a valid expression).

This works for me (tried it):

String String = "";
if(String instanceof String) {
     System.out.println("hallo");
}

1 Comment

Tried it out - and "instanceof" works with String type. Sorry, that was my fault.
2

The String class can be used as the class parameter of the instanceof operator. My guess is that if you have:

String String = ""
if(String instanceof String)
  // ...

The compiler sees String on the right hand side of instanceof and thinks it isn't a class name since you defined a variable named String in that scope.

Try renaming your variable & see what happens.

Edit: After seeing alexvetter's answer, I realize I (in the above) am wrong. String instanceof String works fine for me.

String isn't a keyword because it's simply a normal class. Keywords are generally reserved for things that control the language & that - if used as variables - would confuse meaning & make for difficulty in parsing. You can use a String in all of the same ways you can use any other object in Java.

2 Comments

No, your sample code compiles and runs good. My JDK version is 1.6.0_24
Yes, thus my "Edit: ..." I realized my mistake.

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.