Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes My code compiled well in JDK1.4 but warning in JDK1.5. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "My code compiled well in JDK1.4 but warning in JDK1.5. " Watch "My code compiled well in JDK1.4 but warning in JDK1.5. " New topic
Author

My code compiled well in JDK1.4 but warning in JDK1.5.

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,

My Programmer Certification is 1.4, so I dont know the new features in JDK1.5. After I have finished most of my codes which compiled and run in JDK1.4, I tried to move to the JDK1.5 which is required by the assignment instruction. While I compile the Data.java[URLyBird], the computer says unchecked or unsafe implemention. Then I compile once again with appendix
"-Xlint:unchecked" there are total 3 warnings: java.util.HashMap 's member put(K, V)'s calling is unchecked; java.util.Vector 's member add(E )'s calling is unchecked [this appeared twice]
Could you tell me why?


SCJP 1.4 SCJD
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
You don't use genericised collections.
Your Vector stores something of class A with keys of class B.
Define it as Vector<B, A> rather than Vector.


42
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Jeroen T Wenting:
You don't use genericised collections.
Your Vector stores something of class A with keys of class B.
Define it as Vector<B, A> rather than Vector.


I think Jeroen was in a hurry when posting this

I'm sure he meant use HashMap<A, B> rather than vector<A,B>. The vector definition would be vector<A> for some classes A.

So for example, use HashMap<String, Integer> map = new HashMap<String, Integer>(); or vector<String> myList = new Vector<String>();

You should find that you can get rid of some casts when you get elements out of such collections. If you are using Iterators you should declare them with the appropriate type. For instance Iterator<String> when iterating over an ArrayList<String>.

There's lots more but the above should help you get rid of a few of those warnings.


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

Joined: Apr 21, 2006
Posts: 1847
have to leave something for the compiler to complain about, else it won't ever be happy

Yeah, mixed up the Vector and Map there, never post while the doorbell is ringing
Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi Barry,

Thanks for your help. Now such warning is disappeared. I only rectified HashMap<Integer, String[]> and Vector<Integer> satisfiedRecNo. But Iterator which iterats satisfiedRecNo did no change,
For instance Iterator<String> when iterating over an ArrayList<String>.
so is such a step no need?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
The call to get the Iterator from your ArrayList<String> will automatically return an Iterator<String>.

It would be cleaner (and probably get you a higher score) to do away with that Iterator and instead use the new forEach loop syntax:


Mind you won't have an index number here, but then when you iterate over an iterator you won't have one either.

And also remember you no longer need to cast the values retrieved from your Vector or Map to the type you defined it to be for.
So in the above example List you'd not need to do String t = (String) s.get(0); but you'd just do String t = s.get(0);
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What I meant about the Iterator is shown in the following


The declaration of the iterator that you get from the list needs to be declared with same type as the list elements if the cast is to be avoided. (For simplicity I'm not showing the loop constructs here)
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

I have a same dilemma, row or not in the interfaces ?
Example :

I have a interface :


and its implementation :



and its usage is :



Is the line :


correct or not ?

Regards,
Mihai


SCJP, SCJD, SCWCD, OCPJBCD
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
That would be correct. List<Integer> is a valid datatype and indeed the exact type returned by the method.
 
Consider Paul's rocket mass heater.
 
subject: My code compiled well in JDK1.4 but warning in JDK1.5.
 
Similar Threads
Enum substitute in Jdk1.4
How can we get the file version
JBuilder Installation and JDK Clash
assertion doubt
Problem in JDK compatibility