Need some help with JAVA coding.

The forums have been archived. Please read this thread for more information.

  • Need some help with JAVA coding.

    Need some help with JAVA coding.
    So im new to this stuff and i really wanna go deep, because if you dont go deep and not understand what everything means, Its gonna get really though.
    So i wondered :
    What does it mean with Main(string[]args) how does it connect to system.out.Println? I know that Void means return, and im guessing that in this case the void is Main(string. Wich is the main ''mission'' (Method?) of the hello world program.
    _______________________________________
    public class MyProgram {

    public static void main(String[] args) {
    System.out.println(
    "Hello world!");
    }
    }
    _______________________________________

    And can someone explain System.out? explain the class please. i know its all classes.

    I would love to know how you know where to put these ''( and { all of those things like ; . Im so confused about that:S :whistling:
  • well, I am a JAVA scripter, experienced or not XD

    anyway, to answer your question, main(String[] args) is actually used for running in the command line.
    you can specify command line arguments (args) like java MyProgram.java "hello world"
    in the program itself you could do System.out.println(args[0]); (first line of the array args)

    anyway, to answer your other question, JAVA has an API that uses various packages, like System for example.
    in these packages you go deeper, System has out, in or err for example
    so you start with the most global package and define it more and more.

    just to give an example, JAVA's primitive data types are int, long etc. but String is NOT a primitive data type.
    it's actually a class with the name String, but actually it is under Java.lang.String.
    but for convenience it's just referenced as 'String'.

    a method is just reusable code. if you want to compute something and do it more than one time (not in a loop, but more as when you press a button) a method is what to choose. a method needs a return type, you can use String or int or whatever you like.
    void means NO return type, so a void method doesn't return anything.

    also, a method always uses '()'(a method's name always ends with (), to let the compiler know it's a method), an array uses '[]'(actually only the name) and opening/closing blocks of code uses '{}'.

    any questions? something not clear? I don't know if you understand or not, I am not the best at explaining :P
  • shredder121 wrote:

    well, I am a JAVA scripter, experienced or not XD

    anyway, to answer your question, main(String[] args) is actually used for running in the command line.
    you can specify command line arguments (args) like java MyProgram.java "hello world"
    in the program itself you could do System.out.println(args[0]); (first line of the array args)

    anyway, to answer your other question, JAVA has an API that uses various packages, like System for example.
    in these packages you go deeper, System has out, in or err for example
    so you start with the most global package and define it more and more.

    just to give an example, JAVA's primitive data types are int, long etc. but String is NOT a primitive data type.
    it's actually a class with the name String, but actually it is under Java.lang.String.
    but for convenience it's just referenced as 'String'.

    a method is just reusable code. if you want to compute something and do it more than one time (not in a loop, but more as when you press a button) a method is what to choose. a method needs a return type, you can use String or int or whatever you like.
    void means NO return type, so a void method doesn't return anything.

    also, a method always uses '()'(a method's name always ends with (), to let the compiler know it's a method), an array uses '[]'(actually only the name) and opening/closing blocks of code uses '{}'.

    any questions? something not clear? I don't know if you understand or not, I am not the best at explaining :P


    hey thanks for a great answer! i understood what you said with the () stuff but i still wonder what happens if you type out or err after system?
    And i also wonder if this one works, i got some of it from google but i changed a bit. just wonder if i did it right!
    _______________________________________________________
    Import java.io.console;
    private class Myprogram {

    Public.static.void main (string[] args) {
    system.out.println("hi whats your name?");
    console terminal = system.console ();
    if (terminal == null) {
    system.err.println("terminal not accesable");
    } else {
    string name = terminal readline();
    system.out.println("hello, " + name + " your name begins with " + name.charat(0));
    string nice = terminal readline();
    system.out.println("yes it sure is " + nice);
    }
    }
    }

    _______________________________________________
    Wich one of these commands makes your avaible to type? haven't got that yet :S but i guessed it was the ''string"

    and what did you mean with running in command line? i didn't really get that, but hey thanks for a great answer anyways :D this learned me something atleast! :thumbsup:


    i think i got this thing with command line, stupid me. So kind of, it starts everything that comes after it? if you haven't speciefied the argu?

    The post was edited 1 time, last by Liftylym ().

  • well, it has got some work to do, but it is not entirely hopeless..

    classes must start with a capital letter, so Console, System and yes, also String (string isn't a primitive data type, it is a class)

    string name = terminal readline();


    the seperator is '.' so terminal.readLine
    note: JAVA uses CamelCaps. so after every 'word' you have to start the next one with a capital letter
    ForExample, thisSentenceIsBetterReadable
    thanalongandalllowercaseifyouknowwhatimean.
    so, terminal.readLine.

    to be honest, I haven't used the terminal class that much, I just started with a simple 'application' that did run on the console, but didn't use the terminal class.. what do you want to program?
    also, you should try and learn how to read javadoc, many IDE's integrate a javadoc viewer to easily read about when to use what.
    what IDE do you use? if you even do?

    uhm, let's see..
    Wich one of these commands makes your avaible to type? haven't got that yet but i guessed it was the ''string"

    as I said before, I haven't used the Console class that much, but after some digging I think you have to use terminal.reader().read(char[] target) or something.
    but it depends on what kind of application you make, if you don't use the Console class you can just use System.in.read() to type.

    and what did you mean with running in command line? i didn't really get that, but hey thanks for a great answer anyways this learned me something atleast!

    well, a JAVA program can be accessed from the command line(cmd in windows, terminal in linux (and mac?)) with java (java has a path variable in your os, so it should point to the right executable)
    anyway, a program's execution ALWAYS starts at the main(). everything that is in main() will be run.

    outside the main you put repetitive code in methods, or organize methods and attributes in classes.
    but it won't run. because it is not in main().
    so once you run a program, main() is invoked.

    when you type java myProgram.java, myProgram's main is run. so when you give it additional parameters, like -xmx2048 it will run main(xmx2048).
    java will recognize it, because it is something that is built in in java, but you can use it too with your own arguments.
    if you make a java program that squares the argument for example, you could make the program quicker(as in quick in, quick out) by using arguments, instead of running it and listening to input.
  • shredder121 wrote:

    well, it has got some work to do, but it is not entirely hopeless..

    classes must start with a capital letter, so Console, System and yes, also String (string isn't a primitive data type, it is a class)

    string name = terminal readline();


    the seperator is '.' so terminal.readLine
    note: JAVA uses CamelCaps. so after every 'word' you have to start the next one with a capital letter
    ForExample, thisSentenceIsBetterReadable
    thanalongandalllowercaseifyouknowwhatimean.
    so, terminal.readLine.

    to be honest, I haven't used the terminal class that much, I just started with a simple 'application' that did run on the console, but didn't use the terminal class.. what do you want to program?
    also, you should try and learn how to read javadoc, many IDE's integrate a javadoc viewer to easily read about when to use what.
    what IDE do you use? if you even do?

    uhm, let's see..
    Wich one of these commands makes your avaible to type? haven't got that yet but i guessed it was the ''string"

    as I said before, I haven't used the Console class that much, but after some digging I think you have to use terminal.reader().read(char[] target) or something.
    but it depends on what kind of application you make, if you don't use the Console class you can just use System.in.read() to type.

    and what did you mean with running in command line? i didn't really get that, but hey thanks for a great answer anyways this learned me something atleast!

    well, a JAVA program can be accessed from the command line(cmd in windows, terminal in linux (and mac?)) with java (java has a path variable in your os, so it should point to the right executable)
    anyway, a program's execution ALWAYS starts at the main(). everything that is in main() will be run.

    outside the main you put repetitive code in methods, or organize methods and attributes in classes.
    but it won't run. because it is not in main().
    so once you run a program, main() is invoked.

    when you type java myProgram.java, myProgram's main is run. so when you give it additional parameters, like -xmx2048 it will run main(xmx2048).
    java will recognize it, because it is something that is built in in java, but you can use it too with your own arguments.
    if you make a java program that squares the argument for example, you could make the program quicker(as in quick in, quick out) by using arguments, instead of running it and listening to input.


    _______________________________________________________________________________________________________________

    Im using notepad + +, but in here the text changes.
    ¨
    And whats the difference between like.. hm, terminal, console? is it different type of windows?

    The thing is, i just want to learn me java to do some ''easier games'' in the future, so im just trying to learn the difference between different classes, well theres many.
    _________________________________________________________________________________________
    Maybe i chould start out with a math game in the terminal window?
    So it sais, something like this
    __________________________________________
    Import java.io.console;
    Public class MathProblem {

    Public static void main (string[] args) { -i still dont really know what this line is used for :S
    system.out.println("Welcome to math game, what is 4 x 5");
    console terminal = system.console ();
    string answer = terminal.readline ();
    If (terminal == (20) {
    system.err.println("Good " + answer "is correct! "); -could you explain the difference between err and out?
    } else {
    system.out.println("That was not correct, do you want a new number? Type yes or no"); - why do you have a ';' ?
    string answer = terminal.readline ();
    If terminal == ( no ) {
    Stop -i guessed it was stop
    } else {

    Public static void main (string[] args) {
    - and it all starts from the beginning? how do you make a random math number? like a random number?
    How do you start a new thing? like i just did? a new math number?


    Sorry but these code gets messed up when i write it here so :S

    The post was edited 1 time, last by Liftylym ().