This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Valid code, but what does it mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Valid code, but what does it mean?" Watch "Valid code, but what does it mean?" New topic
Author

Valid code, but what does it mean?

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441



bset is initialized but aset isn't - is it interpreted as something in generics?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It means that aset is a HashSet<Character> variable which isn't initialized, and bset is a HashSet<Character> variable which is initialized to refer to a HashSet<Character> object. It's nothing to do with generics; consider this:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

It is exactly the same as this:

I wouldn't use such a notation to declare variables in one line and initialize only one of them, because it's confusing.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Jesper de Jong wrote:I wouldn't use such a notation to declare variables in one line and initialize only one of them, because it's confusing.


And here we see a live example of that confusion!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Indeed! Remember that just because you can do something doesn't mean that you should!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
An example of what Bear says: it is possible to open a car door when travelling at 99mph. Something you can do, but don't do . . . at least not twice
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Would it be utterly horrible to type?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

To me, yeah. It's much less clear than each in their statement. Why obfuscate the code when you can make it clear?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Saves typing... global RSI incidence rates must have rocketed since the introduction of Java generics...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

In my opinion, saving typing is never a good excuse for obfuscating code.
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Bear Bibeault wrote:In my opinion, saving typing is never a good excuse for obfuscating code.


Can't agree more. Its just not comfortable later to read obfuscated code, rather than later trying to read obfuscated code, its easier to type few letters more.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Luigi Plinge wrote:Saves typing... global RSI incidence rates must have rocketed since the introduction of Java generics...

You'll be pleased with one of the new features in Java 7, which will allow you to write this:

instead of:


Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Good news... but I think there are still quite a few redundant characters in that code.



*runs off to start learning Groovy*
 
GeeCON Prague 2014
 
subject: Valid code, but what does it mean?