2

sorry if this is a dumb question. I'm newbie to Java ( coming from a PHP background ). So here is what I have.

    String variable = new String("whatever");
    //instanceof String == true here
    variable = null;
    //whereas here instanceof String == false
    variable = new Integer(3);// error

Doesn't setting the variable to null remove it's refference, since I instantiated it with an object type? Is there a way I can keep the variable name but change it's data type? Thank you

2
  • No a variables type is defined at it's declaration. Commented Sep 23, 2013 at 13:43
  • 4
    The reference - yes. The type - no. It stays there. Commented Sep 23, 2013 at 13:43

5 Answers 5

9

Java is a strongly typed language.

When you designate variable as a String, it is always a String. If you try to assign an invalid type to it, the compiler will give you an error because you've tried to mix types.

Setting something to null in Java doesn't change the type. Of course, you can allow a variable to go out of scope and create another one with a different type, for example:

if ( x == 1 ) {
  String variable = "whatever";
  ...
}
else {
  Integer variable = new Integer( 3 );
  ...
}

(Reusing variable names could be confusing of course).

Another thing you will notice is that subclasses can be assigned to types designated as the superclass. For example, the class Integer is a subclass of the class Number, so you can do this:

Number variable = new Integer( 3 );
Sign up to request clarification or add additional context in comments.

Comments

2

java is a strongly typed language. Once you have defined that as a String, its got to be a String or subclass of String (if String wouldn't have been marked as final).

Check this also: https://stackoverflow.com/questions/11954068/java-strongly-typed-or-weak-typed

Comments

1

Java is strongly, statically typed. So references(variables) can only point to objects of their own type or subclasses. So Object variable can refer to any other class in java since its a super class. String cannot be used to refer to Integer since it is not a child class of String.

The closet thing you have in java to a var is the Object.

Comments

1

Java is a strongly typed language and I'd discourage you from fighting that.

The normal thing to do in Java is to write

Integer myConvertedInt = Integer.parseInt(variable)

Also, Java has a syntatic shortcut, allowing you (for strings) to write

String variable = "whatever";

You could achieve what you want to do by using the Object type (which is the base class for all Java non-POD types). But that involves casting and would really be a misuse of the language.

Comments

1

There are two related, but distinct, issues here: The type of a variable and the class of an object.

instanceof tests the class of the object referenced by a variable or expression, and is defined to return true if there is no object because the reference is null.

If a reference is not null, it must point to an object of a class appropriate for its type.

The way to get flexibility is to use a common interface for the type of a variable. If you declare a variable to be type java.util.List, it can point to an ArrayList, or a LinkedList, ...

You can even make a variable type Object. In that case, it can reference any Object, but you lose a lot of the benefits of Java. You would actually be writing in a style more appropriate to languages without static types.

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.