Question about literal syntax

R

Rhino

I've just discovered that there is an apparent inconsistency within Java
with respect to literal syntax. I'm wondering if anyone knows the reason?

Given two methods, displayShort() and displayLong():

private void displayShort(short myShort) {
System.out.println("myShort = " + myShort);
}

private void displayLong(long myLong) {
System.out.println("myLong = " + myLong);
}

I can test the displayLong method like this:

displayLong(555L);

but if I want to test the displayShort method, this:

displayShort(5);

will not compile; the compiler assumes that the 5 is an int and says that
doesn't match the signature of displayShort().

I can't use this either:

displayShort(5S);

The only way that works (without having to create an extra variable) is:

displayShort((short) 5);

So, why doesn't Java support:

displayShort(5S);

I'm just curious....

--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
 
D

Dotty

Rhino said:
I've just discovered that there is an apparent inconsistency within Java
with respect to literal syntax. I'm wondering if anyone knows the reason?

Given two methods, displayShort() and displayLong():

private void displayShort(short myShort) {
System.out.println("myShort = " + myShort);
}

private void displayLong(long myLong) {
System.out.println("myLong = " + myLong);
}

I can test the displayLong method like this:

displayLong(555L);

but if I want to test the displayShort method, this:

displayShort(5);

will not compile; the compiler assumes that the 5 is an int and says that
doesn't match the signature of displayShort().

I can't use this either:

displayShort(5S);

The only way that works (without having to create an extra variable) is:

displayShort((short) 5);

So, why doesn't Java support:

displayShort(5S);

I'm just curious....

They asked me about it, but I was in a bad mood that day so
I told them not to.
 
J

John C. Bollinger

Rhino said:
I've just discovered that there is an apparent inconsistency within Java
with respect to literal syntax. I'm wondering if anyone knows the reason?

[...]

There is no syntax for expressing literals of type byte or short. I
don't call that an inconsistency, but I suppose you can if you want. I
don't have definitive knowledge of the reason for the omission, but I
would guess that it was to keep the language a bit simpler. Primitives
are implicitly narrowed under circumstances that are clearly defined in
the Java Language Specification; they include some cases of assignments,
but explicitly exclude method invocation contexts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top