Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in my Java prg

 
omkar patkar
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of my java prg based on collections on compliling my IDE(JCreator 2.5 LE) shows following output :-
--------------------Configuration: JDK version 1.5.0 <Default>--------------------
Note: F:\LEARN\JAVA APPS\EXPERIMENTS\CompDemo1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Process completed.

The programs runs Completely!...no prb with prg just, whats the meaning of it (....i.e "unchecked or unsafe operations" )? B'coz i had never heard of such terms "unsafe" or "unchecked" in Java, they are in C# !

one more thing ......how does the integer value obtained from my userdefined comparator is used by the tree set " EXACTLY! " ?

Can comparison using comparator be only " = ", " <= ", " >= " ?....i mean when we have operators, why is their a separate need of comparators ?

i have some more dbts in Collections (...in java alltogether) , Hope the moderator does not think of my dbts as burst of interview dbts ! Please! Am trying to get deep down in java on my own by refering books and when they are unclear i make a list and ask them, javaranch does not allow me to ask the list of dbts in one thread else...i would have!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The warning message means that in at least one spot, you're using a Collection in the "old" (before 1.5) way:

ArrayList list = new ArrayList();
list.add("foo");
String s = (String) list.get(0);

This is "unsafe" because of that cast. In Java 1.5, you have generics, which allow you to write this:

ArrayList<String> list = new ArrayList<String>();
list.add("foo");
String s = list.get(0);

Note there is no more cast. Google for "Java generics" to find lots of information on this topic.

As to your second question, the operators you list don't work on Objects, only on numbers, right? So you need Comparators or Comparables to provide ways of comparing Objects.

The integer returned by a Comparator is used by a TreeSet to put objects in order. If the number is positive, one object should come first; if it's negative, then the second should be first. If it's zero, they're the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic