• Welcome to Valhalla Legends Archive.
 

Java help

Started by j0k3r, November 23, 2003, 07:43 AM

Previous topic - Next topic

j0k3r


public class BasicsDemo {
   public static void main(String[] args) {
       int sum = 0;
       for (int current = 1; current <= 10; current++) {
           sum += current;
       }
       System.out.println("Sum = " + sum);
   }
}


Why is the entire program incased in a class?
And what determines the arguments for public class void main()?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

#1
Ok, time to answer this!!

Ok, every file in Java has a public class, with the same name as it's file (like, iago.java would have public class iago).  This class can be used from anywhere in the folder (like, in /projects/iago/iago.java, /projects/iago/joe.java could also use class iago).

When you run a Java program (on commandline, by typing "java iago [args]"), it will call class.main(args).  main() has to be static, so it can be called without an instance of the class being made, public so it can be called from outside, and it returns nothing.

In Java, everything is in a class.

[Edit] Personally, I *only* use public classes.  I make a new file whenever I need a new class.  See my Java Bot. :)

Also, you should use a more descriptive topic name :P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Banana fanna fo fanna

#2
Quote from: iago on November 23, 2003, 07:47 AM
Ok, time to answer this!!

Ok, every file in Java has a public class, with the same name as it's file (like, iago.java would have public class iago).  This class can be used from anywhere in the folder (like, in /projects/iago/iago.java, /projects/iago/joe.java could also use class iago).

When you run a Java program (on commandline, by typing "java iago [args]"), it will call class.main(args).  main() has to be static, so it can be called without an instance of the class being made, public so it can be called from outside, and it returns nothing.

In Java, everything is in a class.

[Edit] Personally, I *only* use public classes.  I make a new file whenever I need a new class.  See my Java Bot. :)

Also, you should use a more descriptive topic name :P

Not neccessarily. A file doesn't need to have a public class in it. It can just be a normal class, which has package access. This is useful when writing libraries.

And not everything is a class...everything except the primitive data types (int boolean etc).

Kp

As iago tried to hint at, a public class must be in a file of the same name.  However, as St0rm mentioned, the reverse is not true: a file can contain zero or more non-public classes of any name whatsoever.  For some design decision which I fail to understand, it is a compile time error to try to put a public class in a file which has a nonmatching name.  Nonpublic classes are particularly useful/attractive when you want to have "helper objects."  For instance, if you decided to write your own LinkedList implementation, it'd be logical to have the nodes of that list be a nonpublic class since it really doesn't make much sense to have random functions outside your linked list able to create linked list nodes. :)

Also, to clarify what iago said, when you do java X, the java VM looks for X.class in your classpath.  Class X must have a public static void main(String[]) function, or the program cannot be executed.  Note that it is theoretically possible to have multiple entrypoints for your program by having several different classes, each with their own main(String[]) function.  Which one you reference on the command line controls which entry point you get.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

j0k3r

Quote from: iago on November 23, 2003, 07:47 AM
In Java, everything is in a class.
He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more.

What determines the arguments for public class void main()?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

xsemaphorex

<code>

//Filename Foo.java

public class Foo {
   public static void main(String[] myvar) {
   }
}

</code>
main(String[] data) is the application entry point for the class <insert class name here> when you run the java program using the command line:

java Foo

The String[] parameter is simply the commands after Foo on the command line starting at 0 being the first argument. (Note that -1 is not accessible and will throw an ArrayIndexOutOfBounds Exception if you try accessing it.)

For example:

java Foo my name is mud

will send the parameter String[] data = {"my", "name", "is", "mud"}.

and

java Foo "my name is" mud

will send the parameter String[] data = {"my name is", "mud"}

There's much more.

In java every single member belongs to a class; be it an instance of that class or the class itself. (ie static members). There are no 'classless' members. This lends to code that is much cleaner and simpler to understand (in theory of course.. as I work with a bunch of towell-head indians that code as if they took a single prep class and then lied on their resume concerning their experience and education from some obscure university like calcutta tech... but HEY I'm not bitter :P)

I'm sure though that if you have more questions... you'll ask.. :)

Kp

Quote from: j0k3r on November 23, 2003, 12:23 PM
What determines the arguments for public class void main()?

The user.  If you were to do java MyApp -a 1 2 3, main would receive a String[] with 4 elements: "-a", "1", "2", and "3".  Anything after the class name on the command line becomes an argument to the entry point function.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

j0k3r

Quote from: j0k3r on November 23, 2003, 07:43 AM

   public static void main(String[] args) {


Ok, that leads me to asking what you would do with it and where are the strings stored? Do you have to give it a variable to store it in? Are the arguments mandatory?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

It's just like in C
int main(int argc, char *argv[])

since it's all taken care of for you.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

#9
Quote from: iago on November 23, 2003, 05:00 PM
It's just like in C
int main(int argc, char *argv[])

since it's all taken care of for you.

Not quite.  In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument.  In Java, args[0] is the first command-line argument.

Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation.

j0k3r

Quote from: iago on November 23, 2003, 05:00 PM
It's just like in C
int main(int argc, char *argv[])

since it's all taken care of for you.

I don't know C.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

Quote from: Skywing on November 23, 2003, 05:25 PM
Quote from: iago on November 23, 2003, 05:00 PM
It's just like in C
int main(int argc, char *argv[])

since it's all taken care of for you.

Not quite.  In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument.  In Java, args[0] is the first command-line argument.

Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation.

I actually didn't mean it that way, I meant that the OS fills it in :P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Banana fanna fo fanna

Quote from: j0k3r on November 23, 2003, 12:23 PM
Quote from: iago on November 23, 2003, 07:47 AM
In Java, everything is in a class.
He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more.

What determines the arguments for public class void main()?

He changed his post.

j0k3r

Quote from: iago on November 24, 2003, 02:30 AM
Quote from: Skywing on November 23, 2003, 05:25 PM
Quote from: iago on November 23, 2003, 05:00 PM
It's just like in C
int main(int argc, char *argv[])

since it's all taken care of for you.

Not quite.  In C, argv[0] is typically the executable name, and argv[1] is the first command-line argument.  In Java, args[0] is the first command-line argument.

Edit: Enclosed in code tags due to an annoying forum bug that breaks subscript notation.

I actually didn't mean it that way, I meant that the OS fills it in :P
So I can just leave it blank and not worry about it?
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

iago

Quote from: St0rm.iD on November 24, 2003, 06:05 AM
Quote from: j0k3r on November 23, 2003, 12:23 PM
Quote from: iago on November 23, 2003, 07:47 AM
In Java, everything is in a class.
He said everything is IN a class, not everything IS a class, but thanks, it clears some of the smoke I'll have to read up on it some more.

What determines the arguments for public class void main()?

He changed his post.

If you mean me, that's a lie, I didn't change that part of my post.



And no, Java doesnt' let you leave it blank, it ONLY allows public static void main(String[] args);
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*