Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How is this compatible with Java 1.4? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How is this compatible with Java 1.4?" Watch "How is this compatible with Java 1.4?" New topic
Author

How is this compatible with Java 1.4?

Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
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 ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Aniket Patil:
Any explanation?


Possibly not, yet.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: May 02, 2006
Posts: 218
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

Joined: May 02, 2006
Posts: 218
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: May 02, 2006
Posts: 218
Will get back to you about this.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
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

Joined: May 02, 2006
Posts: 218
Needs a "Bump"!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
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

Joined: May 02, 2006
Posts: 218
Needs another bump!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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

Joined: May 02, 2006
Posts: 218
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How is this compatible with Java 1.4?
 
Similar Threads
LinkedList and Comparator
LinkedList problem
Abstract Data Type for a Queue
Best implementation for Queue interface.
Queue interface