void?

Discussion in 'Java' started by Draken, Jun 30, 2003.

  1. Draken

    Draken Guest

    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
     
    Draken, Jun 30, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:3f006caa$0$...
    > 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
    >
    >
     
    Guy Van Rinsveld, Jun 30, 2003
    #2
    1. Advertising

  3. Draken

    Ole Oleson Guest

    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
     
    Ole Oleson, Jun 30, 2003
    #3
  4. Draken

    Draken Guest

    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
     
    Draken, Jun 30, 2003
    #4
  5. Draken <> 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 () ---------------------------\
    | 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
     
    Joona I Palaste, Jun 30, 2003
    #5
  6. 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
     
    Berislav Lopac, Jun 30, 2003
    #6
  7. Draken

    Ole Oleson Guest

    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
     
    Ole Oleson, Jun 30, 2003
    #7
  8. 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
     
    Lee Fesperman, Jul 1, 2003
    #8
  9. Draken

    Dale King Guest

    "GaryM" <> 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
     
    Dale King, Jul 1, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sergio

    returning a void (*)(void)

    Sergio, Jan 5, 2005, in forum: C++
    Replies:
    6
    Views:
    445
    Jonathan Turkanis
    Jan 5, 2005
  2. Ollej Reemt
    Replies:
    7
    Views:
    568
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    814
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    851
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    420
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page