• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

basic syntax question ?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have couple questions for this program.(i got this code from JAVA 2)
// Char
class Char {
public static void main (String args[]) {
char ch;

ch = 'X';
System.out.println ("ch contains " + ch);
ch++;
System.out.println ("ch is now " + ch);
ch = 90;
System.out.println ("ch is now " + ch);
}
}
1. on the ' public static void main (String args[])', but i looked another book 'Thinking in JAVA by Bruce Eckel' and he wrote '(String[] args)'. what's the different?
2. on this output 'ch is now z' not 90 because from ASCII. can you tell me where i can find this document?
thank you for your help.
yuan
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
for your second question:
ascii coed table
check out the ASCII value in the row where DECIMAL value is 90.
for the first:
there is no difference. that easy.

i dont know the exact reason for this. i prefer the String[] way.
k
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by karl koch:
ascii coed table


An ascii co-ed table eh?
:roll:
Are the lower-case ones female?
 
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the mixed case ones are definitely teenagers.
doco
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String myStringArray[]; // is for us old C programmers ( well not quite: char* myStringArray[10] would be it )
String[] myStringArray; // is for cool kids
[ March 18, 2003: Message edited by: Barry Gaunt ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The different syntax styles also allow for unnecessarily compounded declarations of different data types.
int a, b[];
a would be an int while b would be an int array.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am interested in this topic. But I am a little confused. Does this mean that we use String a[] to creat an array a[] with type String and, on the other hand, we use String[] a to creat a, which is an array of String (String[])?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shen Lin:
I am interested in this topic. But I am a little confused. Does this mean that we use String a[] to creat an array a[] with type String and, on the other hand, we use String[] a to creat a, which is an array of String (String[])?


String a[] is an array of Strings, the array is named a.
String[] a is an array of Strings, this array is also named a.
(The type of a is in both cases String[].)
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
now that i reread my post...douuuugh
but no worries:
i wont correct my typo so you can continue to make fun of it :-)

k
 
Donald R. Cossitt
buckaroo
Posts: 401
Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String a[] is an array of Strings, the array is named a.
String[] a is an array of Strings, this array is also named a.
(The type of a is in both cases String[].)


What is the advantage of using one over the other? Or when would one use String[] a and not String a[]?
doco
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by doco mastadon:

What is the advantage of using one over the other? Or when would one use String[] a and not String a[]?
doco


They are equivalent expressions. There is no advantage of one over the other.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you consider those unnecessarily compounded declarations an advantage. I'd tend to consider the ability to do funny things like that a curse.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I was reading through here to see if I could offer advice or more likely learn something, then I got confused.
I thought that arrays could be only int or float, etc. I just learned about vectors, similar to arrays but they hold objects such as strings. Am I misunderstanding something?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Candy, since your question is a bit different from this thread's topic, I've begun a new thread for you where we can discuss your question.
[ March 19, 2003: Message edited by: Dirk Schreckmann ]
 
Let's get him boys! We'll make him read this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic