unchecked conversion warning.

R

Roedy Green

what I consider unhelpful documentatio

Perhaps docs should be written by the folks who write the test code.
The get to ask the coders, decide what makes sense. They will than
answer the questions that need answering and clarify that which is
guaranteed.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Controlling complexity is the essence of computer programming.
~ Brian W. Kernighan 1942-01-01
..
 
M

Martin Gregorie

Perhaps docs should be written by the folks who write the test code.
The get to ask the coders, decide what makes sense. They will than
answer the questions that need answering and clarify that which is
guaranteed.
Ideally the javadocs would form at least part of the module-level
documentation and so should be written by the designers, not the coders.
I think there's a case for the module design documentation to be written
entirely as a javadoc package suite, i.e. they contain the descriptive
text as class and public method level comments as well as the associated
declarations. There's also a case for compiling, as a sanity check, them
before they are given to the coders.

Further, the design team are the people who should specify the module and
regression tests and (hopefully) should be writing them too.

IME you really don't want coders writing module tests. The problem is
that they will tend to write tests for what they coded rather than for
what the specification asked them to write and they may skimp on corner
cases and error handling.
 
L

Lew

Martin said:
Ideally the javadocs would form at least part of the module-level
documentation and so should be written by the designers, not the coders.
I think there's a case for the module design documentation to be written
entirely as a javadoc package suite, i.e. they contain the descriptive
text as class and public method level comments as well as the associated
declarations. There's also a case for compiling, as a sanity check, them
before they are given to the coders.

Further, the design team are the people who should specify the module and
regression tests and (hopefully) should be writing them too.

IME you really don't want coders writing module tests. The problem is
that they will tend to write tests for what they coded rather than for
what the specification asked them to write and they may skimp on corner
cases and error handling.

"Can't we all just get along?"

The business world speaks of "siloing" - "to silo" is to isolate personnel to
a narrow wedge of the overall project.

Siloing can be good, but in software development usually is not.

It's vanishingly unlikely to be good if it isn't done on purpose with full
understanding of its effects.

Siloing is not the same as division of responsibility. One can divide
responsibilities among a team who is generally, and to varying degrees
specifically aware of various dimensions of the project.

Siloing would be to keep programmers entirely away from test writing.

There is more than one type of test. Loosely, tests fall into unit tests and
everything else.

Unit tests are, as the name should indicate, aimed at rather minimal units of
code. They verify the behavior of a type's public face. Positively and negatively.

For example, from the spec for 'List#get(int)' it follows directly that the
argument might be negative, zero, or positive, and may equal, exceed or be
less than the size of the list. Any programmer worth their salt had better be
able to write a full suite of unit tests for such a method.

<http://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int)>

As the designer of a type, let's say 'Foo', with a method 'goof(Bar)', the
programmer knows exactly how the method should handle various possible values
of the 'Bar' argument. Ideally up front, whilst writing the Javadocs thereto.

From the Javadocs the tests follow directly. It shall do thus and so with a
'null' argument, and react in a certain way to a reference to an uninitialized
instance, and another to one in an inconsistent state, and yet another to one
in a valid, initialized state.

That part is unit testing. Unit tests operate on components in isolation,
separated from the context of a larger application. Their sources and sinks
(inputs and outputs) are simulated via mock objects and other tricks where
absolutely necessary. This form of testing is also called "hermetic", meaning
sealed away from the operational environment.

The rest goes by different names according to specific line of inquiry. Some
suggest "functional" test for everything regarding how components function /in
situ/, while others restrict it to just those that confirm behaviors'
conformance to spec /in situ/. That narrower sense is also called "acceptance"
testing or "regression" testing. (Note: I'm being a little loosey-goosey here.
If you have more refined definitions by all means share them.) Tests to ensure
that components function well with each other are "integration" tests. Tests
for performance and throughput or stability or load are called "performance",
or in that last case, "load" tests.

Unit tests

Everything else (Functional tests in the wide sense)
- Acceptance / regression (functional in the narrow sense)
- Integration
- Performance
- - Load

Your taxonomy may vary. It will cover pretty much the same range, though.

Cross that in a matrix with a second dimension of techniques: programmed
tests, fixtured tests (those with deterministic setup conditions),
simulations, random-event tests, parametrized probabilistic test scenarios,
and so on.
 
M

Martin Gregorie

The business world speaks of "siloing" - "to silo" is to isolate
personnel to a narrow wedge of the overall project.

Siloing can be good, but in software development usually is not.
Yes, I'd agree, but still its desirable to have a separation between a
module's developer(s) and its test writer(s), if only to help prevent
misunderstandings getting baked into code.
It's vanishingly unlikely to be good if it isn't done on purpose with
full understanding of its effects.
Agreed.

Siloing is not the same as division of responsibility.
Yes.

One can divide
responsibilities among a team who is generally, and to varying degrees
specifically aware of various dimensions of the project.
and a team that tries compartmentalize a project may well be on the track
to failure.
Siloing would be to keep programmers entirely away from test writing.

There is more than one type of test. Loosely, tests fall into unit tests
and everything else.
I'd make a few more distinctions. Yes, unit teats are the very lowest
level, with module tests above them. By module tests I mean anything from
testing a function library, a small component program or, for that
matter, even a complex class for somewhat higher level functions that can
cover everything that module can do as an isolated component.

Next level is integration testing, which seeks to show that a system will
work when its modules are built into a realistic configuration.

Functional tests are essentially end-to-end, user-level activities within
a complete system. Performance tests are more of the same but with added
'stopwatches'. Recovery tests are probably the highest level testing that
gets done be for the user gets his hands on the system. Many shops seem
to skimp on both performance and recovery tests judging by the number of
systems I've seen that didn't perform adequately when data volumes got
near the design volumes: the last data warehouse I worked on had 500,000
facts in its test database, barely enough, yet I've seen and fixed
databases 'tuned' with 200 or so detail records to work on.

Then, of course, acceptance testing is simply functional testing, but
done on the target hardware with the involvement of end users.

You'll notice I've left out regression testing until now. Thats because
its just an automated version of some combination of unit, module, or
functional testing that is quick and easy to run and whose results are
simple to interpret. These days I script all unit and module tests in
such a way that I merely have to add a set of expected results and a
comparator for them to become regression tests: if the comparator doesn't
find any difference between expected and actual results its a pass.
 
B

Broad Liyn

在 2012å¹´5月30日星期三UTC+8下åˆ9æ—¶32分43秒,(未知)写é“:
This code compiles with an 'unchecked conversion' warning.
I have tried various corrections, for example casting like (Vector<Object>), but to no
avail.
What am I doing wrong?l
The code is the smallest demo I could make from the original application.

import java.util.Vector;
public class genericsdemo
{
private static Vector<Vector> vdata = new Vector<Vector>(); //a Vector of Vectors
private static Vector<Object> vrow = new Vector<Object>(); //a row

public static void main(String args[]) {
vrow.add(null); //two columns in the row
vrow.add(null);

vdata.add(vrow); //add the row to the Vector of Vectors

Vector vtmp = getrow(); //test
}

private static Vector<Object> getrow() {
return vdata.elementAt(0); //warning: [unchecked] unchecked conversion
}
}

JensJ
1.vector is no longer being supported.
2.
 
L

Lew

Broad said:
(未知)写é“:
This code compiles with an 'unchecked conversion' warning.
I have tried various corrections, for example casting like (Vector<Object>), but to no
avail.
What am I doing wrong?l
The code is the smallest demo I could make from the original application.

import java.util.Vector;
public class genericsdemo
{
private static Vector<Vector> vdata = new Vector<Vector>(); //a Vector of Vectors
Vector said:
private static Vector<Object> vrow = new Vector<Object>(); //a row

public static void main(String args[]) {
vrow.add(null); //two columns in the row
vrow.add(null);

vdata.add(vrow); //add the row to the Vector of Vectors

Vector vtmp = getrow(); //test

Raw types are bad.
}

private static Vector<Object> getrow() {
return vdata.elementAt(0); //warning: [unchecked] unchecked conversion
}
}
1.vector is no longer being supported.
2.

Do you mean 'Vector', as in 'java.util.Vector'? Because spelling counts in Java.

If so, then you're mistaken, and if not, you need to explain what you mean.
 
R

Robert Klemme

The documentation,
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html, says "If
a thread-safe implementation is not needed, it is recommended to use
ArrayList in place of Vector." which implies that Vector might be a
reasonable choice if thread safety is needed.

Personally, I prefer to use ArrayList as base, and wrap using
Collections.synchronizedCollection, but that is not mandatory.

I totally agree. Btw, there is one interesting detail: JavaDoc of
Collections.synchronizedList() and the other synchronized*() methods
explicitly state that the mutex used for synchronizing is that of the
returned object:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList(java.util.List)

No such statement is done about Vector. So while the source code of
Vector tells us that the Vector instance is used to synchronize the
JavaDoc does not give any guarantee about that and - at least
theoretically - Sun/Oracle could change that and synchronize on another
instance.

Kind regards

robert
 
L

Lew

Robert said:
I totally agree. Btw, there is one interesting detail: JavaDoc of
Collections.synchronizedList() and the other synchronized*() methods
explicitly state that the mutex used for synchronizing is that of the
returned object:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList(java.util.List)

I see no such promise there.

And in fact it's not true. The source for the 'synchronizedCollection()' family
of methods reveals that there's a separate field 'mutex' of type 'Object' on
which the methods synchronize.
No such statement is done about Vector. So while the source code of
Vector tells us that the Vector instance is used to synchronize the
JavaDoc does not give any guarantee about that and - at least
theoretically - Sun/Oracle could change that and synchronize on another
instance.

And that would rarely matter, unless a person were adding actions that tried to
synchronize on the same monitor.
 
R

Robert Klemme

I see no such promise there.

<quote>
It is imperative that the user manually synchronize on the returned list
when iterating over it:

List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}


Failure to follow this advice may result in non-deterministic behavior.
</quote>

Pardon, my wording was wrong: it's not explicitly stated but it follows
immediately from the quote. For me that was "explicit" enough. ;-)
And in fact it's not true. The source for the 'synchronizedCollection()' family
of methods reveals that there's a separate field 'mutex' of type 'Object' on
which the methods synchronize.

Did you see how it's initialized? It's initialized with this, i.e. the
wrapper instance. The field serves the sole purpose to be able to
synchronize on a parent collection when a dependent collection is
created (e.g. in
java.util.Collections.SynchronizedRandomAccessList.subList(int, int)).
So normally the monitor of the wrapper is used as illustrated by the
JavaDoc quoted.
And that would rarely matter, unless a person were adding actions that tried to
synchronize on the same monitor.

No, it also matters in cases like the example quoted above, i.e. when
you need to perform multiple operations on the Vector instance: when the
monitor which the Vector instance uses internally is different from the
one used for the external synchronization then you do not have thread
safety. Depending on the logic anything from inconsistent contents to
CME can happen.

Kind regards

robert
 
L

Lew

<quote>
It is imperative that the user manually synchronize on the returned list
when iterating over it:

List list = Collections.synchronizedList(new ArrayList());
...
synchronized (list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}


Failure to follow this advice may result in non-deterministic behavior.
</quote>

Pardon, my wording was wrong: it's not explicitly stated but it follows
immediately from the quote. For me that was "explicit" enough. ;-)

Okay, I see what you're saying, but it doesn't follow immediately.
You have to go through at least two logical steps.
No, it also matters in cases like the example quoted above, i.e. when
you need to perform multiple operations on the Vector instance: when the
monitor which the Vector instance uses internally is different from the
one used for the external synchronization then you do not have thread
safety. Depending on the logic anything from inconsistent contents to
CME can happen.

Nope.

The successive operations can synchronize on a second monitor without
harm even though the individual methods synchronize on the first one.
Provided all your code uses that monitor, of course, in addition to the
default one.

But then the Javadoc's advice wouldn't be right, so therefore you are.

Thanks for the eye-opener.
 
A

Arne Vajhøj

1.vector is no longer being supported.

It is supported.

It is not deprecated.

It is generally considered best practice to use ArrayList
instead of Vector and has been so for many years.

Arne
 
A

Arne Vajhøj

As of JDK 7, Vector is not even deprecated.

The documentation,
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html, says "If
a thread-safe implementation is not needed, it is recommended to use
ArrayList in place of Vector." which implies that Vector might be a
reasonable choice if thread safety is needed.

For other readers: thread safety here is for single calls.
Personally, I prefer to use ArrayList as base, and wrap using
Collections.synchronizedCollection, but that is not mandatory.

That is what is considered best practice today.

Arne
 
A

Arne Vajhøj

There's nothing fundamentally wrong with Vector. People will
moan and wring their hands over the cost of its synchronized methods,
but I haven't heard of any actual measurements.

Java Performance Tuning / Jack Shirazi has some tests
in Chapter 10 (Threading).

The difference were significant for 1.2.2, 1.3.1, 1.3.1 server
and 1.4.0 but not for 1.4.0 server.

The versions indicates something about the age of book.

I would expect the difference to be small for newer JVM's.
Synchronization has been optimized.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top