void?

D

Draken

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
 
G

Guy Van Rinsveld

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.
 
O

Ole Oleson

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.
 
D

Draken

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
 
J

Joona I Palaste

Draken said:
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 ([email protected]) ---------------------------\
| 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
 
B

Berislav Lopac

Draken said:
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
 
O

Ole Oleson

Draken said:
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.
 
L

Lee Fesperman

Berislav said:
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.
 
D

Dale King

GaryM said:
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.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top