Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> Introduction to scripting in Java

Pages: 1

Unregistered




Errors in your Java examples
      #51298 - 08/02/07 04:20 AM

Found a few errors in you Java examples:

Code:
String[] arr = new String[]{"Mike", "Joe", "Bruce"};
List list = Arrays.asList(arr);
for (String item : list) {
System.out.println(item);
}



Does not compile since you have to type the list with String:

Code:
List<String> list = Arrays.asList(arr);



Also I would write it as follows, which is equally short as your Phython version.

Code:
String[] arr = new String[]{"Mike", "Joe", "Bruce"};
for (String item : arr) {
System.out.println(item);
}



One last thing you write "With the more flexible for loop" which is not true the new for loop is shorter in syntax but LESS flexible than the old one (which is still needed when you, for example, want to remove an object from a Collection you are looping over)

On the actual content of your article I agree on most of it. A scripting language should be simple to write/change (and maybe also debug) that is the whole idea with using one in the first place. The sad thing is that most scripting languages grow to big and complex with time (trying to compete with full 3g programming languages), which today in my opinion includes perl, Phython and a few others. The only two that I found that didn't do that are Elisp and Tcl because both of them build on a really powerful and simple syntax (at least for the few programmers that actually understood the core of them).


Post Extras: Print Post   Remind Me!   Notify Moderator  
Dejan Bosanac
Unregistered




Re: Errors in your Java examples [Re: Anonymous]
      #51346 - 08/03/07 10:11 AM

Thanks for spotting. It must be lost in the editing process. I'll put it on the scriptinginjava.net in the "corrections" section as soon as possible.

Thanks.


Post Extras: Print Post   Remind Me!   Notify Moderator  

Unregistered




Re: Errors in your Java examples [Re: Dejan Bosanac]
      #51462 - 08/06/07 03:24 PM

This for loop is a really poor example. First--what is point using an outdated version of Java as your first example? I think it only serves to confuse the issue. Secondly, in your Java 5 example, there is no need to convert your String array to a List, the for loop syntax works perfectly on String arrays. A fair comparison would be:

list = ["Mike", "Joe", "Bruce"]
for item in list :
print item

to

String[] list = new String[]{"Mike", "Joe", "Bruce"};
for (String item : list) {
System.out.println(item);
}

Of course that wouldn't illustrate your point about Java not being concise--but it would be honest.


Post Extras: Print Post   Remind Me!   Notify Moderator  
cleberz
stranger


Reged: 08/21/07
Posts: 1
Re: Errors in your Java examples [Re: Dejan Bosanac]
      #52060 - 08/21/07 04:43 PM

Quote:

This for loop is a really poor example. First--what is point using an outdated version of Java as your first example? I think it only serves to confuse the issue. Secondly, in your Java 5 example, there is no need to convert your String array to a List, the for loop syntax works perfectly on String arrays. A fair comparison would be:

list = ["Mike", "Joe", "Bruce"]
for item in list :
print item

to

String[] list = new String[]{"Mike", "Joe", "Bruce"};
for (String item : list) {
System.out.println(item);
}

Of course that wouldn't illustrate your point about Java not being concise--but it would be honest.




I agree, I also think the examples are poor. If we are trying to show how clean code can be, we could made the first example even cleaner without using curly brackets, nor the new operator:
Code:

String[] list = {"Mike", "Joe", "Bruce"};
for (String item : list)
System.out.println(item);



Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 4169

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5