Mircea Voivod
Mircea Voivod

Reputation: 721

Java variable type

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

Upvotes: 2

Views: 295

Answers (5)

Ash
Ash

Reputation: 9426

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 );

Upvotes: 9

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

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.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234705

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.

Upvotes: 1

Abe
Abe

Reputation: 9031

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.

Upvotes: 1

Claudiu
Claudiu

Reputation: 1489

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

Upvotes: 2

Related Questions