ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
  Technologies to Watch: A Look at Four That May Challenge Java’s Development Dominance
Subject:   Not the best Java code example
Date:   2005-11-21 07:06:57
From:   JendaPerl
Response to: Not the best Java code example

I think you are missing something. The multiple variable assignment is not that handy if you just need to assign two constants to two variables. You are right you could write it as two assignments and it would actually be more readable. The beauty of the multivar assignment comes when there are functions (or methods if you insist) involved. Suppose you need to return two integers from a function. Isn't it better to write

min, max = find_supremes list

than to either return an array:

int []arr = find_supremes(list);
int min = arr[0];
int max = arr[1];

or pass the variables by reference?

Also suppose you need to switch the contents of two variables. Isn't

x, y = y, x

(or in the "oh-so-unreadable" Perl syntax ($x, $y) = ($y, $x);)

easier to read than

guessWhatType tmp = x;
x = y;
y = tmp;


1 to 2 of 2
  1. Java as an "important standard"
    2006-01-17 11:39:19  AdrienLamothe [View]

  2. Not the best Java code example
    2005-11-21 07:51:18  ikayak [View]

1 to 2 of 2