For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
Viv.java uses unchecked or unsafe operations - explanation and cure
Are you getting Java compiler messages like this?
Dorothy-2:java grahamellis$ javac Viv.java
Note: Viv.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Dorothy-2:java grahamellis$ javac -Xlint Viv.java
Which when you expand the warning with -Xlint gives:
Dorothy-2:java grahamellis$ javac -Xlint Viv.java
Viv.java:20: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
trowbridge.add(new Rolf("First Great Western",75,3,60));
^
If so, let me explain.
Java's collection classes - such as ArrayLists and Vectors - can hold any type of object, and the mechanisms of the base Java language allow yo to put in any object - including ones that may not be appropriate to a particular use. Alas - Java is NOT a language that trusts the programmer and the compiler wails that there may be a problem (if you're a good programmer, there probably isn't a problem!).
The problem is caused by a declaration such as this:
java.util.ArrayList trowbridge =
new java.util.ArrayList();
and can be overcome by telling java what sort of objects you expect to be in the ArrayList so that it can satisfy itself by making the checks on you. Here's how you tell it:
java.util.ArrayList<Peter> trowbridge =
new java.util.ArrayList<Peter>();
(I'm my example, I have objects of class "Peter" ... Peter is actually an abstract class, and the objects are really "Rolf"s and "Steve"s.
From Java Bootcamp. See source code of this and other early examples on this course (as run this week) here
See also Java Generics (written 2009-09-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles J714 - Java - Fundamental classes [42] Do languages change? - (2004-09-08) [1062] Java sorting - comparable v comparator - (2007-02-02) [1502] Java, sorting, ArrayList example, generics - (2008-01-11) [1910] Java - Generics - (2008-11-27) [2323] Java Collection Objects in the java.util package - (2009-08-05) [2421] Sorting Collections of Objects in Java - (2009-09-25) [2649] Length, size or capacity in Java? - (2010-02-24) [2734] for and foreach in Java - (2010-04-22) [2920] Sorting - naturally, or into a different order - (2010-08-14) [3048] String handling - from first steps to practical examples - (2010-11-13) [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27) [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16) [4421] How healthy are the stars of stage and screen? - (2015-02-09) [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
Some other Articles
Looking inside Java classes - javap and javadocExceptions in Java - why and howWhere is my Java class?Viv.java uses unchecked or unsafe operations - explanation and cureJava Programming FundamentalsAutomating access to a page obscured behind a holding pageVariable names like i and j - why?Hello World - a good traditional start to a Java courseSanity checking the price, and selling up to increase income
|
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|