Write once, run anywhere?

R

Roedy Green

public boolean isCDDriveOpen {
if ( can_the_CD_drive_be_opened ) {
if ( CD_drive_is_open ) {
return true;
} else
return false;
}

The ifs are unbalanced. You would normally write that as:

public boolean isCDDriveOpen
{
return can_the_CD_drive_be_opened && CD_drive_is_open;
}

IntelliJ often offers to "simplify" code like yours into code like
mine.
See http://mindprod.com/jgloss/intellij.html
 
J

Jag

How about this (dumb) example?

class AAA {
AAA() {
System.out.println("AAA");
}
}

class Aaa {
Aaa() {
System.out.println("Aaa");
}
}

And in main()
AAA a1;
Aaa s1;

On Windows, one might give a ClassNotFoundException.
On Linux, it would run normally.

UNT,
Jag
 
L

Lars Enderin

JT skrev:
public boolean isCDDriveOpen {
if ( can_the_CD_drive_be_opened ) {
if ( CD_drive_is_open ) {
return true;
} else
return false;
}

What's wrong with
public boolean isCDDriveOpen() {
return CD_drive_is_open;
}
?
 
L

Lars Enderin

Lars Enderin skrev:
JT skrev:

What's wrong with
public boolean isCDDriveOpen() {
I was careless. Obviously this is correct:
return can_the_CD_drive_be_opened && CD_drive_is_open;
 
S

Stefan Ram

Lars Enderin said:
return can_the_CD_drive_be_opened && CD_drive_is_open;

The Java Code Conventions do not endorse such readable naming.

According to them, names should be written like

»canTheCdDriveBeOpenend«

Now, a human can not read it anymore, but still javac will
happily compile it.

I wrote a preprocessor for Java source code, so now
- in my precode - I might write this as

»'can the cd drive be opened'«

And the preprocessor will convert it to the above.

The preprocessor is written in Perl 5 and the code
actually is

while( $text =~ s{^([^"'\r\n]*(?:(?:"(?:[^"\r\n]|\\")*"|'(?:[^'\r\n]|\\')*')[^"'\r\n]*)*)'([^'\\\r\n][^'\r\n]+)'}{ $1 . camel( $path, $2 ) }gem ){}

with the sub camel defined as follows.

sub camel($$)
{ my( $path, $text )= @_;
$text =~ s{^\043([a-zA-Z])}{uc($1)}ge;
{ my $sp = chr( 160 );
$text =~ s{,${sp}}{_${sp}}g;
$text =~ s{${sp}([a-zA-Z0-9_])}{uc($1)}ge; }
{ my $sp = chr( 32 );
$text =~ s{,${sp}}{_${sp}}g;
$text =~ s{${sp}([a-zA-Z0-9_])}{uc($1)}ge; }
if( $text eq 'class' ){ $text = 'class_'; }
return $text; }

This is intended to replace

»'abc def'« by »abcDef« and
»'#abc def'« or »'Abc def'« by »AbcDef« and
»'class'« by »class_« (as a special rule that might be
extended to all keywords).

It allows for ' ' to be either the normal space (32) or
the non-breaking space (160).

But it will not replace 'a' (because this is only a single
character within the single quotes) nor "...'a'..." (because
this is used in double quotes), so one can still use the
single quote as intended in Java most of the time.

I might even admit for a question mark as in

»'can the cd drive be opened?'«

which could be converted to

»canTheCdDriveBeOpenend$F3$« or so.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

JT said:
But none of you have really answered my question. Without using the
specific case of the CD drawer (which I was only intending as a
for-instance), my question boils down to this:

Isn't it true that the JVM has a limited amount of what it can do based
on the commonality of all the different platforms. If someone is
writing a program, don't they have to know all the places it's going to
be running so that they can avoid coding things that just won't work?
And if this is true, doesn't it break the mantra of "write once, run
anywhere"?

Java is a very good language for writing stuff that are platform
independent so that is truly write once run anywhere.

But Java is not a very good language for writing platform
specific stuff including hardware interfacing.

You can obvious not access a feature that does not exist.

The Java API as a consequence of that mostly covers things
that are present on all systems.

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top