Home Is variable type the same as value type?
Reply: 2

Is variable type the same as value type?

Andres Alvarez
Andres Alvarez Published in 2018-02-11 01:00:17Z

If I write:

int x = 7;

Can we conclude that the value type of 7 is int and therefore the type of x is also int?

What is 'int' indicating here? That the value is int or that 7 is an int?

Spyros K
Spyros K Reply to 2018-02-12 01:28:06Z

int refers to x. It is indicating the type of the variable x. This is also informative of the type of the values that would be stored there.

  int x =7;

Consists of 2 parts. One is:

   int x;

That declares x as an int in this scope. This shows that int refers to x. The other part of the expression is:


That actually assigns the int value 7 to x.Of course the assignment needs to be valid.So the value should be of a suitable type. It also would be valid to omit the assignment in a program (if for example you do not use this variable.) On the other hand you cannot omit the declaration since java is strongly typed. For example writing:


Does not give enough information unless y is declared. y could be for example an int , Integer , long or Long. What y is depends on its declaration.

Andrey Tyukin
Andrey Tyukin Reply to 2018-02-11 01:26:30Z

It indicates that the variable x is of type int.

The expression 7 also happens to have type int, therefore the example is not that interesting.

Consider a more interesting example:

List<String> myList = new ArrayList<>();

Here, the compiler could infer that the type of new ArrayList<>() on the right hand side is ArrayList<String>. However, we might want to hide the concrete implementing class ArrayList, and instead declare myList to be of a more general type List<String>, which is a supertype of ArrayList<String>. This is useful if we want to prevent abstraction leak from the members of a class, or if we use myList as a local variable, and for some reason want to set it to an LinkedList later.

That's all I had to say on the question itself, but I want to give some context, and I want to argue that Java's current syntax is actually not too bad.

This syntax might seem verbose, one might think that one can often omit the type declaration on the left hand side. However, consider a language with stronger type inference. For example, a closely related statically strongly typed language Scala tends to rely more on type inference. For example, if you write

var myList = List("foo", "bar")

it will automatically infer that myList is a variable of type List[String].

But this often leads to unwanted results. For example, given the assignment

var myList = List.empty

the Scala compiler would happily infer that the right hand side is a List[Nothing], where Nothing is a special type that signifies absurd impossible results "returned" by thrown Exceptions and Errors which break the normal program flow. You can't do anything with this variable. In order to make this variable useful, we have to write:

var myList: List[String] = List.empty

Now Scala compiler would understand that myList is supposed to be List[String], which is more general than the type inferred from the right hand side. As you can see: now you have both val keyword and an additional List[String]-type declaration on the left hand side, which is not necessarily much shorter than the corresponding Java version. It is actually longer, despite all the fancy type-inference magic. So, Java's syntax for variable declarations is... OK, at least it's not trivial to make it much better.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299485 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO