Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How is this compatible with Java 1.4?

 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedList implements the Queue interface only as of Java 5. The following source code calls the offer() method on a LinkedList reference. Further, it also has a Queue reference hold a LinkedList object. When this code is compiled with javac -source 1.4, no error is thrown.

Here's the code:


How does this work?
[ August 26, 2006: Message edited by: Aniket Patil ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aniket Patil:
Any explanation?


Possibly not, yet.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just spent a minute (ok two) looking at the documentation for the java command:
-source release
Specifies the version of source code accepted. The following values for release are allowed:
1.3 the compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
1.4 the compiler accepts code containing assertions, which were introduced in JDK 1.4.
1.5 the compiler accepts code containing generics and other language features introduced in JDK 5. The compiler defaults to the version 5 behavior if the -source flag is not used.
5 Synonym for 1.5


You are not using any new language features: Generics, enums, enhanced for loops. So you are telling the compiler that you may be using assertion s (which you are not). There is no mention of the system libraries (e.g java.util.*).

The default target JVM is 1.4 when you use -source 1.4, so what happens when you run this code on a 1.4 JVM? Let us know soon, please.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, will install JDK version 1.4.2, and get back with the result. This was run on JDK 5.0.

BTW apologies for the unnecessary second post, guess i was too eager to know the reason. Happens to me often, when something remains unsolved!
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiled the code with JDK 1.4.2, and this gives 3 errors as expected:

javac LinkedDemo.java
LinkedDemo.java:10: cannot resolve symbol
symbol : method offer (java.lang.String)
location: class java.util.LinkedList
s.offer("A");
^
LinkedDemo.java:11: cannot resolve symbol
symbol : method offer (java.lang.String)
location: class java.util.LinkedList
s.offer("B");
^
LinkedDemo.java:17: cannot resolve symbol
symbol : class Queue
location: class LinkedDemo
Queue q = new LinkedList();
^
3 errors

Now the question is, how does javac -source 1.4 function, since it does not throw the above errors?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is to be expected if you compile it with JDK 1.4.2.
What if you compile it with 5.0 using -source 1.4 (implicitly taking -target 1.4) and running with the 1.4 JVM?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will get back to you about this.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Executed with Java 1.4.2_12, and here's the result:

java LinkedDemo
Exception in thread "main" java.lang.NoSuchMethodError: java.util.LinkedList.offer(Ljava/lang/Object ; ) Z
at LinkedDemo.main(LinkedDemo.java:8)

The JVM doesen't seem to have any issues with a Queue reference holding a LinkedList.
[ August 30, 2006: Message edited by: Aniket Patil ]
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Needs a "Bump"!
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that the purpose of "-source" is to check language syntax -- it is not to check the library usage. In order to do that the compiler will need to also have older libraries, which would lead to serious bloat to the JDK.

Henry
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means that on a JDK 5, -source 1.4 would use the upgraded inheritance relationships of Java 5. How would it ensure that the source is compatible with 1.4 release?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Needs another bump!
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aniket,

Unless you have more details to add, please stop bumping this thread.

This question seems to have been answered -- twice. The "-source" tag on the compiler only checks the language syntax and not the libraries in use, which is not what you want.

Sorry,
Henry
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps i should've added more clarity to my question. This is what i want to know:

I understand that using javac -source 1.4 on a JDK 5.0 checks only for a language syntax problem. Since none exists, it compiles fine.

Executing this on a JVM 1.4 leads to an exception for offer(), since no such method exists. But Queue is an interface that is added in J2SE 5, and there is no exception thrown for this. In fact, if the source is compiled with the Java 1.4 compiler, an error is thrown stating that the symbol "Queue" is unresolved.

Why does assignment of a LinkedList to a Queue fine with the JVM?
[ September 10, 2006: Message edited by: Aniket Patil ]
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aniket Patil:

Executing this on a JVM 1.4 leads to an exception for offer(), since no such method exists. But Queue is an interface that is added in J2SE 5, and there is no exception thrown for this. In fact, if the source is compiled with the Java 1.4 compiler, an error is thrown stating that the symbol "Queue" is unresolved.

Why does assignment of a LinkedList to a Queue fine with the JVM?


In the Java 5 core library, the LinkedList class implements the Queue interface, so the assignment is valid.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic