Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > void?

Reply
Thread Tools

void?

 
 
Draken
Guest
Posts: n/a
 
      06-30-2003
I have read the Java API and the definition of the reserved word "void" in
there doesnt seem to make much sence? What is void used for? What does it
mean? I have seen it in all the example source I have been reading.. "
public static VOID main (args[]) " but dont see what role it plays?

Thank you for anyone who can help explain this a little bit better for me.
-Dave


 
Reply With Quote
 
 
 
 
Guy Van Rinsveld
Guest
Posts: n/a
 
      06-30-2003
void is only to tell that there will be 'no return'.
it's a mandatory option in this case otherwise there should be a 'return
......' at the end of the method.
the main which is giving the project entry point is a typical 'no return'
method.

"Draken" <(E-Mail Removed)> wrote in message
news:3f006caa$0$(E-Mail Removed)...
> I have read the Java API and the definition of the reserved word "void" in
> there doesnt seem to make much sence? What is void used for? What does it
> mean? I have seen it in all the example source I have been reading.. "
> public static VOID main (args[]) " but dont see what role it plays?
>
> Thank you for anyone who can help explain this a little bit better for me.
> -Dave
>
>



 
Reply With Quote
 
 
 
 
Ole Oleson
Guest
Posts: n/a
 
      06-30-2003
Draken wrote:

Hi. I take it you're a beginner to C-like languages.

> I have read the Java API and the definition of the reserved word "void" in
> there doesnt seem to make much sence? What is void used for? What does it
> mean? I have seen it in all the example source I have been reading.. "
> public static VOID main (args[]) " but dont see what role it plays?


It just says the method won't return any value to its caller.
If you provide a method signature like
public static LONG doIt(double r) {
you will have to say something like
return Math.round(r) - 2; // << long integer
}
whereas a void method cannot return a value (like a PROCEDURE in Pascal).

BTW:
STATIC means the function can be called without creating an object of this
class, it has no access to non-static fields.
PUBLIC means anyone can call it.

> Thank you for anyone who can help explain this a little bit better for me.
> -Dave


 
Reply With Quote
 
Draken
Guest
Posts: n/a
 
      06-30-2003
Thank you to Guy and Ole,

I am not particularly new to OOP languages, I started off with ADA but my
university has changed its curriculum to use Java, and due to circumstances
I am starting off next semester in an advanced Java class (im doing the unit
"Data Structures using Java" when I did "Programming Principles with ADA")
when I haven't learned the basics (they suggest I should know how to program
from my knowledge of ADA since I got a high destinction in it). The problem
is in the differences between Java and ADA, as I dont remember ADA having
anything like static or void. From memory ADA also used functions and
procedures. So basically using void defines the difference between a
procedure and a function?

I now understand the word void, I had already found good explainations of
the public/private words and the static word.

I realise I made a mistake too, the examples I have seen use "public static
void main (String[] args)" which leads me to ask what the "String[] args"
part is for?

Thanks for your help,

-Dave


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-30-2003
Draken <(E-Mail Removed)> scribbled the following:
> Thank you to Guy and Ole,


> I am not particularly new to OOP languages, I started off with ADA but my
> university has changed its curriculum to use Java, and due to circumstances
> I am starting off next semester in an advanced Java class (im doing the unit
> "Data Structures using Java" when I did "Programming Principles with ADA")
> when I haven't learned the basics (they suggest I should know how to program
> from my knowledge of ADA since I got a high destinction in it). The problem
> is in the differences between Java and ADA, as I dont remember ADA having
> anything like static or void. From memory ADA also used functions and
> procedures. So basically using void defines the difference between a
> procedure and a function?


> I now understand the word void, I had already found good explainations of
> the public/private words and the static word.


> I realise I made a mistake too, the examples I have seen use "public static
> void main (String[] args)" which leads me to ask what the "String[] args"
> part is for?


It's a parameter for the main() method. A method parameter is a kind of
variable, except that you don't initialise it, it has automatically
been initialised when your method is called. For the main() method, the
args parameter is an array of java.lang.Strings, that contains the
command-line arguments.

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I will never display my bum in public again."
- Homer Simpson
 
Reply With Quote
 
Berislav Lopac
Guest
Posts: n/a
 
      06-30-2003
Draken wrote:
> From memory ADA also used functions and
> procedures. So basically using void defines the difference between a
> procedure and a function?


I am not sure what you mean by the "difference between a procedure and a
function", as in Java there are only "methods". However, if by that you mean
that one of them does some work and returns a value (or object), while the
other just does some work and has no return, then you are correct. In Java,
a method which returns something must specify the data type of what will be
returned (i.e. a primitive type for a value or a class for an object);
'void' is thus just a substitute for the data type of nothing.

> I realise I made a mistake too, the examples I have seen use "public
> static void main (String[] args)" which leads me to ask what the
> "String[] args" part is for?


When you call a program, say from a command line, you pass it zero or more
arguments; for example:

> java myProgram myFirstArgument 20


In this example, there are two arguments, "myFirstArgument" and "20". The
String[] args is an array which contains all the arguments that you have
passed, plus the name of the program itself (which is stored in the first
position of the array, at args[0].

Since there is no way for the program to initially distinguish what
datatypes the arguments belong to (i.e. it cannot tell that '20' is actually
a number), they are stored into an array of Strings, and the program has to
cast them properly before it can use them.

I don't know about Ada, but Java is a strongly-typed language, and
everything must have an explicitly stated type, either a primitive or an
object type.

HTH,

Berislav
--
Berislav Lopac
Web developer


 
Reply With Quote
 
Ole Oleson
Guest
Posts: n/a
 
      06-30-2003
Draken wrote:

> Thank you to Guy and Ole,
>
> I am not particularly new to OOP languages, I started off with ADA but my
> university has changed its curriculum to use Java, and due to
> circumstances I am starting off next semester in an advanced Java class
> (im doing the unit "Data Structures using Java" when I did "Programming
> Principles with ADA") when I haven't learned the basics (they suggest I
> should know how to program from my knowledge of ADA since I got a high
> destinction in it). The problem is in the differences between Java and
> ADA, as I dont remember ADA having anything like static or void. From
> memory ADA also used functions and procedures. So basically using void
> defines the difference between a procedure and a function?


If ADA is like Pascal, yes. So a void-function is used to perform side
effects only, whereas a "real" function is a function in the ADA-sense.

> I now understand the word void, I had already found good explainations of
> the public/private words and the static word.


That's because 'void' has much more philosophical substance.

> I realise I made a mistake too, the examples I have seen use "public
> static void main (String[] args)" which leads me to ask what the "String[]
> args" part is for?


String is a non-trivial kind of datatype, represented by a Class, but also
provided some native support for your convenience (You can code something
like
String complete = "one" + someString + "and the other";

The '[]' makes it an array of Strings. You can refere to its elements using
String[] whole = new String[] { "raz", "dva", "tri" };
String one = whole[0];
String two = whole[1];
String three = whole[2];

Arrays are not actually harmless in Java.

> Thanks for your help,
>
> -Dave


 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      07-01-2003
Berislav Lopac wrote:
>
> Draken wrote:
> > I realise I made a mistake too, the examples I have seen use "public
> > static void main (String[] args)" which leads me to ask what the
> > "String[] args" part is for?

>
> When you call a program, say from a command line, you pass it zero or more
> arguments; for example:
>
> > java myProgram myFirstArgument 20

>
> In this example, there are two arguments, "myFirstArgument" and "20". The
> String[] args is an array which contains all the arguments that you have
> passed, plus the name of the program itself (which is stored in the first
> position of the array, at args[0].


You are thinking C/C++. The name of the program is not passed to main() in Java, just
the arguments. In the example above, args would have two elements.

--
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
>
> Since there is no way for the program to initially distinguish what
> datatypes the arguments belong to (i.e. it cannot tell that '20' is actually
> a number), they are stored into an array of Strings, and the program has to
> cast them properly before it can use them.
>
> I don't know about Ada, but Java is a strongly-typed language, and
> everything must have an explicitly stated type, either a primitive or an
> object type.
>
> HTH,
>
> Berislav
> --
> Berislav Lopac
> Web developer

 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      07-01-2003
"GaryM" <(E-Mail Removed)> wrote in message
news:Xns93AAC028E9BE5R3344324357788499939@216.168. 3.44...
>
> When I first saw this return type I was confused because I always
> thought void meant invalid, as in 'void if removed'. It makes more
> sense when you think of void as 'empty'.



Void does not mean invalid in the legal context. Void in that context means,
"Having no legal force or validity". So in a round about way it means empty
there as well.
--
Dale King


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments