basic JAVA tutorial
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: basic JAVA tutorial

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    590

    basic JAVA tutorial

    OK, well, I really liked Mystic Ravenous's tutorial on C (me learning it now) and I thought I'd do something very similar with java, cos I'm kinda learning it now and know the basics of it.

    Here goes...

    This first program will be similar to Mystic's first program, I'll store some variables and then print them onto the screen - then we'll progress from there. (don't worry too much about classes and methods at the moment.)

    public class Basic //in java, things always need to be in a class
    {//opens class
    public static void main(String[] args) //methods reside inside classes
    {//opens method
    String theString = "Hello"; //declaring and initializing a string (not capital S)
    char theChar = 'A'; //declaring and initializing a character
    int theInt = 45; //declaring and initializing a integer
    double theDouble = 4.3688394; //declaring and initializing a double (there are also floats, but they are similar to doubles)

    System.out.println(theString); //prints to screen the data held in 'theString' variable
    System.out.println(theChar); //you get the point
    System.out.println(theInt);
    System.out.println(theDouble);
    System.out.println("This just prints anything inside the quotes...3u73...39dhsd...yup");
    System.out.println("the integer value is: " + theInt); //use + to seperate your own output and variables
    }
    }//closes class

    ok, here is the code without the comments (note, as with C, everything after the // is ignored by the compiler - comment):

    public class Basic
    {
    public static void main(String[] args)
    {
    String theString = "Hello";
    char theChar = 'A';
    int theInt = 45;
    double theDouble = 4.3688394;

    System.out.println(theString);
    System.out.println(theChar);
    System.out.println(theInt);
    System.out.println(theDouble);
    System.out.println("This just prints anything inside the quotes...3u73...39dhsd...yup");
    System.out.println("the integer value is: " + theInt);
    }
    }

    Some explanations:
    1. 'public static void main(String[] args)' is the MAIN method in java that almost all programs have - except for applets...just remember that line and you'll be right - I don't even really know what all those words in it mean
    2. 'System.out' this is a built-in class that comes with java and is used to display things on the screen - use '.println' after it to display data and then move to new line or use '.print' to display without moving to a new line
    3. note: all data types begin with lower case except for String - so just keep this in mind.

    ok, now I'm sure I probably forgot to explain something, so if I did or you just don't get something, just post a reply and I'll answer it.

    I will hopefully return to post another tutorial that teaches you something else...hope this helps someone!! (thanks to Mystic Ravenous who gave me the idea to do this)


    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  2. #2
    Senior Member
    Join Date
    Dec 2001
    Posts
    590
    ok, just wanted to add a few things for all people who don't know how to start.

    The basics you need to write a java program (like the one above) is to have a basic text editor and the JDK (Java Development Kit) - (or SDK now I think)...either, it doesn't matter.

    You can get the JDK/SDK from Sun's website:
    http://java.sun.com/j2se/
    then simply follow the links that apply to you

    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  3. #3
    Very cool, and informative, I'm glad other people are writing tutorials.. Once you gain some knwoledge distribute it. ..Help others, As of others helped you,..


    cheers,
    MR

  4. #4
    Junior Member
    Join Date
    Feb 2002
    Posts
    6
    Not so informative IMO for a beginner

  5. #5
    Banned
    Join Date
    Mar 2002
    Posts
    32
    1. 'public static void main(String[] args)' is the MAIN method in java that almost all programs have - except for applets...just remember that line and you'll be right - I don't even really know what all those words in it mean

    -> Servlets do not have a main method as well. And no, many objects do not have these. For example, objects that are used solely for utility purposes. You also fail to mention what public static void means. public means that the method can be accessed from all other objects. Static means you do not need to create an instance of the object to use the method for only one instance of the method will always reside in memory. void means that the method will return nothing.String[] args is an argument. This particular one puts any command line options into an array called args. In java objects begin with a capital letter so that would really be Args. Unlike C, the Args array, begins with the first command line argument at index=0.


    2. 'System.out' this is a built-in class that comes with java and is used to display things on the screen - use '.println' after it to display data and then move to new line or use '.print' to display without moving to a new line

    -> System.out is an object not a class. It contains methods for output. print() and println() are methods of the object.

    3. note: all data types begin with lower case except for String - so just keep this in mind.

    -> all primitive data types are lower case (int, float, char, double, boolean, byte, short,long), reference types are also lower case (array, class, interface). String is uppercase because it is an object. For example String, Integer, Hashtable, Vector.

    if you really are interested in learning java, go read the tutorial on java.sun.com, it is fine and you wont find much better.

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    What is the best way to input data from the keyboard?

    An example would be quite helpful.

  7. #7
    Junior Member
    Join Date
    Aug 2002
    Posts
    21
    great job.. helping people learn is always great...

  8. #8
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  9. #9
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    Thanks Guus! My teacher is trying to suck us into jbuilder. I prefer the jdk but didn't have anything to help with keyboard input. I had tried Sun's site but wading through the documentation can be a chore. The site is quite helpful.

  10. #10
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Before you go to bed tonight, sit down on your knees and say a prayer to the uber-god of Google
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •