Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Joshua Bloch -- Strangeness of Java

Reply
Thread Tools

Joshua Bloch -- Strangeness of Java

 
 
Veloso
Guest
Posts: n/a
 
      10-29-2008
Joshua Bloch gives an interesting interview (http://java.sun.com/
developer/technicalArticles/Interviews/bloch_effective_08_qa.html) on
java.sun.com, promoting the revised version of “Effective Java”
wherein he talks about generics, enums, annotations, the under-use of
Java libraries, the importance of minimizing the accessibility of
classes and members and minimizing mutability, and other matters.

But the one question I’d never heard asked was, “What’s the strangest
thing about the Java platform?” His answer: “I'm going to say that the
strangest thing about the Java platform is that the byte type is
signed. I've never heard an explanation for this. It's quite
counterintuitive and causes all sorts of errors.”

Two questions. Does anyone have an explanation as to why the byte type
is signed? And what is your candidate for the strangest thing about
the Java platform?

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      10-29-2008
Veloso wrote:
> Two questions. Does anyone have an explanation as to why the byte type
> is signed? And what is your candidate for the strangest thing about
> the Java platform?


I have a few guesses:
1. Unsigned types were pulled late in the process and no one thought to
change byte to unsigned.
2. The other integral types (short, int, and long) are all signed. Byte
was originally signed for consistency without the developers thinking
about the impact on the most common use cases.

As for the strangest thing, I have to +1 the signed byte thing. It's
always the first thing on the tip of my tongue when I mention features I
wish to see in a perfect Java.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      10-29-2008
Veloso wrote:
>
> Two questions. Does anyone have an explanation as to why the byte
> type
> is signed? And what is your candidate for the strangest thing about
> the Java platform?


Inner classes. Their most common uses are handled much more
intuitively by method pointers.


 
Reply With Quote
 
Harold Yarmouth
Guest
Posts: n/a
 
      10-29-2008
Mike Schilling wrote:
> Veloso wrote:
>> Two questions. Does anyone have an explanation as to why the byte
>> type
>> is signed? And what is your candidate for the strangest thing about
>> the Java platform?

>
> Inner classes. Their most common uses are handled much more
> intuitively by method pointers.


Including iterators? These "method pointers" would have to be able to
carry a context with them that was distinct for each invocation of the
method-pointer-returning method. I.e. would have to be closures.

Anonymous inner classes perhaps.

Allowing function literals and use of static methods as
first-class-object functions would let you get rid of many of the
commonest anonymous inner classes -- Runnable, ActionListener, etc. --
but the multi-method event listeners would need to be handled
differently. Preferably by deprecating the lot of them and adding a new
event framework that puts all of the important information in the event
objects, has more separated events, and so forth. And gets rid of event
component getting. That leads to lots of ugly code, switches,
instanceofs, and casts included.

My usual pattern of use of event listeners is to use two different ones
where I want two different behaviors. So if button A should do X and
button B should do Y, I don't have

public void actionPerformed (ActionEvent e) {
Component c = // etc.
if (c == buttonA) doX();
if (c == buttonB) doY();
// etc.
}

but rather a button A action listener and a separate button B action
listener.

So which component can be inferred by which listener we're in.

For ActionEvent, this means I tend never to even refer to the event
object. A simple "somethingGotClicked()" callback with no args would
have done as well, methinks.

For other listeners ...

Well, consider WindowListener.

void windowOpened(WindowEvent e);
void windowClosing(WindowEvent e);
void windowClosed(WindowEvent e);
void windowIconified(WindowEvent e);
void windowDeiconified(WindowEvent e);
void windowActivated(WindowEvent e);
void windowDeactivated(WindowEvent e);

and WindowFocusListener

void windowGainedFocus(WindowEvent e);
void windowLostFocus(WindowEvent e);

and WindowStateListener

void windowStateChanged(WindowEvent e);

The latter supersedes windowIconified and windowDeiconified from
WindowListener, and puts all information needed in the event.

WindowFocusListener's methods likewise supersede windowActivated and
windowDeactivated. They could be combined into a single method,
windowFocusChanged(boolean nowFocused), with the focus state change in a
parameter, or encoded in an event object if that model was kept.

That leaves WindowListener with windowOpened, windowClosing, and
windowClosed.

The semantics of these are tricky and sometimes wacky.

A windowOpened that is only called once when a window is first
constructed might as well just be inlined in the code that builds the
window. This could stand to go away. Right now its only use is if a
window is constructed but not immediately shown, and something is lazily
initialized only when it's shown. The showing code can as easily lazily
create the whole damn window when it's first shown, e.g. by invoking a
singleton-returning factory method (if there's only going to be the one
window in the app), or (if it isn't supposed to preserve state across
user-visible instances) create a new one each time and dispose it when done.

Getting rid of that leaves windowClosing and windowClosed. Closing has
tricky semantics -- is it hiding-or-disposing or only disposing? Does it
count if the close box is clicked but the window has DO_NOTHING_ON_CLOSE
set?

I'd redesign this so that there was a WindowCloseButtonListener and the
options were DO_NOTHING_ON_CLOSE (greys the "X" button), HIDE_ON_CLOSE,
DISPOSE_ON_CLOSE, and a new one, GENERATE_EVENT_ON_CLOSE. Setting this
last would make the "X" button enabled and cause it to call a
WindowCloseBoxListener when clicked. This could hide or dispose the
window, return without doing either, pop up a "you have unsaved changes?
are you sure?", or whatever, at the coder's pleasure, more or less as
windowClosing is currently used. The other choices would behave exactly
like (and be implemented under the hood as) a listener that just does a
setVisible(false) or a dispose(), aside from DO_NOTHING, which disables
the button.

(And there really should be obvious ways to enable/disable the maximize
and, separately, the minimize buttons, too. Affecting their
corresponding window-icon-menu items. With appropriate translations to
other platforms.)

And we're left with windowClosed, which might be made the sole method of
a WindowCloseListener. This would run if a window was hidden or
disposed, and get a boolean parameter or an event object containing a
boolean that was "true" if the window was disposed.

That's the window listeners (and the old ones would be preserved, with
their present semantics, for compatibility purposes, but deprecated).
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-29-2008
On Tue, 28 Oct 2008 20:56:49 -0700 (PDT), Veloso
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>Two questions. Does anyone have an explanation as to why the byte type
>is signed? And what is your candidate for the strangest thing about
>the Java platform?


It is inherited from the original C. It made more sense back then
when chars were 7-bits. It also may trace back to the instruction set
of the PDP-11. I have not coded PDP-11 since the early 80s.
--
Roedy Green Canadian Mind Products
http://mindprod.com
A vote for McCain is fearful clinging to McSame.
A vote for Obama is a shot at Obamalot.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-29-2008
On Tue, 28 Oct 2008 20:56:49 -0700 (PDT), Veloso
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>
>But the one question I’d never heard asked was, “What’s the strangest
>thing about the Java platform?”


I would say it is the generics syntax. It is a bit like a seat belt
that requires a garage technician to help lock and undo.
--
Roedy Green Canadian Mind Products
http://mindprod.com
A vote for McCain is fearful clinging to McSame.
A vote for Obama is a shot at Obamalot.
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-29-2008
Roedy Green wrote:
> On Tue, 28 Oct 2008 20:56:49 -0700 (PDT), Veloso
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone
> who said :
>
>> Two questions. Does anyone have an explanation as to why the byte
>> type is signed? And what is your candidate for the strangest thing
>> about the Java platform?

>
> It is inherited from the original C.


C left it up to the implementation whether chars were signed or
unsigned.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-29-2008
Roedy Green wrote:
> On Tue, 28 Oct 2008 20:56:49 -0700 (PDT), Veloso
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone
> who said :
>
>>
>> But the one question I'd never heard asked was, "What's the
>> strangest
>> thing about the Java platform?"

>
> I would say it is the generics syntax. It is a bit like a seat belt
> that requires a garage technician to help lock and undo.


It's ugly but not strange, in that it's adopted from C++.


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-29-2008
Eric Sosman wrote:

>
> I've always supposed that the signedness of Java's byte is an
> illustration of Emerson's famous observation about consistency.


The why aren't chars signed? Presumably because while a char is
techinically an integral type, it's really used to hold characters
rather than numbers, and sign-extending them would be foolish. The
answer, then, is that bytes are signed because they were considered to
be small integers, rather than (more sensibly) non-numeric octets..


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-30-2008
Mike Schilling wrote:
> Veloso wrote:
>> Two questions. Does anyone have an explanation as to why the byte
>> type
>> is signed? And what is your candidate for the strangest thing about
>> the Java platform?

>
> Inner classes. Their most common uses are handled much more
> intuitively by method pointers.

Do you mean inner classes or anonymous classes?

I actually find both of those more intuitive (but/and less terse) than
method pointers.

The strangest thing about Java to me is the overloading of "final" and
the lack of a concept that is the same as c++ "const *".

The second strangest thing IMO is erasure.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Video Podcast- OnSoftware Effective Java Programming, Part 1 by Josh Bloch EDG Java 0 06-29-2007 05:46 PM
NYJavaSIG 04/19/06 Meeting - An Eventing with Josh Bloch, Chief Java Architect at Google Frank D. Greco Java 0 04-04-2006 02:20 AM
Effective Java by Josh Bloch stixwix Java 8 11-18-2005 08:42 PM
HEY! Joshua Zyber! Please Read!!!! - Disc News Not Updated!!!! FRUSTRATED DVD Video 4 10-13-2004 03:11 AM
Joshua Tree motel recommendations? Photo Digital Photography 10 02-28-2004 08:50 AM



Advertisments