Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help Getting Started With Java (tutorial)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help Getting Started With Java (tutorial)

    I wrote the following tutorial for my friend and decided I'd post it on here to help out some people .

    Some basics that aren't covered in this tutorial:
    *applets
    *exceptions
    *cases
    and some other stuff.


    public class NameGoesHere {

    *pubic class indicates that the class is public.
    *NameGoesHere is the name of the class (this is case s. Meaning that capital letters matter.)
    *{ indicates the starting of the class block
    Note: we need to close the blocks with } at the end of each block.

    public static void main (String[] args) {

    *This sentence is needed in order to be able to execute the code, we will be
    needing this in all of our codes (args stands for arguements).
    *{ indicates the starting of the code block

    Numbers:

    int = integer (does not have a decimal & it does not round the number, it removes the decimal value).
    double = an integer with a decimal.
    float = a double but with more numbers (e.g: 0.00000000001).

    int count = 12;

    *This gives count a value of 12, whenever count is mentioned the stored value
    of 12 will be used.
    *; is required at the end of each statement in order to be executed, think
    of it as a period in english.

    Display:
    System.out.print();
    System.out.println();
    System.out.print("");
    System.out.println("");

    *System.out.print command is used to display a certain value or string on
    the output.

    Example:
    System.out.println("Hello");

    Output:
    Hello


    *System.out represents the object while print is the method & ln stands for
    new line (prints on a new line).
    *The " indicates the string, we need these quotation marks whenever we use
    a string.


    Packages & imports:
    Packages are the set of classes, while imports are used so we can use certain
    classes and objects.
    Example: import.java.util.Scanner;
    This import must be declared in order for the scanner to work in the code.

    We can either import single imports of the utilities or all of them using the *.

    Example:
    import java.util.*;
    import java.util.Scanner;
    Now let's use what we just learnt to make a small program.

    package package.name.goes.here;
     
    public class Example {
    	public static void main(String[] args) {
    		int count = 25;
    		System.out.println(count);
    	}
    }

    This will print out:
    25 //which is the value of count.

    Comments:

    Comments are totally ignored by Java, they are used to help people to understand
    your code.

    //this is for a single line comment

    /*
    this comment is usually used when explaining what the program does, or stating
    the author names.
    **/

    Examples:
     
    package package.name.goes.here;
     
    public class Example {
    	public static void main(String[] args) {
    		int count = 25; //count is 25
    		System.out.println(count); // this will print out count
    	}
    }

    Definitions (just the basic ones we're going to be using)
    boolean = either true or false
    final = constant / can't be changed
    char = character (we use single quotes such as 'H')
    String = words & sentences, we use double quotes when using strings.

    A full set of Java reserved words (which you can look up):
    abstract
    assert
    break
    byte
    case
    catch
    class
    const* //no use yet
    continue
    default
    do
    double
    else
    enum
    extends
    boolean
    char
    false
    final
    finally
    float
    for
    goto* //no use yet
    if
    implements
    import
    instanceof
    int
    interface
    long
    native
    new
    null
    package
    private
    protected
    public
    return
    short
    static
    strictfp
    super
    switch
    synchronized
    this
    throw
    throws
    transient
    true
    try
    void
    volatile
    while

    The if statement:
    if (condition)
    statement;

    so basically if the statement is true the condition will be executed, otherwise
    it will be fully ignored.

    Example:
    public class Example {
    	public static void main(String[] args) {
    		int count = 25;
     
    		if (count == 30)
    		System.out.println("Hello");
    	}
    }
    Nothing will be produced in the above code because the condition is false.

    We use else so that the program will produce something else if the condition is false.

    Example:
    public class Example {
    	public static void main(String[] args) {
    		int count = 25;
     
    		if (count == 30)
    		System.out.println("Hello");
    		else
    		System.out.println("Hi");
    	}
    }
    This program will print out Hi because the first condition is false.

    We don't need braces } { while using if statements unless we have a lot
    of conditions

    Example:
    public class Example {
    	public static void main(String[] args) {
    		int count = 25;
     
    		if (count == 30)
    		System.out.println("Hello");
    		else
    		{
    		System.out.println("Hi");
    		System.out.println("Hi1");
    		System.out.println("Hi2");
    		}
    	}
    }

    While using conditions we must use operators:

    == means = to
    != means not equal to
    >= means greater than or equal to
    =< means smaller than or equal to
    && means and
    || means or


    Spaces:

    White space in java is totally ignored, we use spaces to make our code readable.
    This means that:
    public class Example {
    	public static void main(String[] args) {
    		int count = 25;
     
    		if (count == 30)
    		System.out.println("Hello");
    		else
    		System.out.println("Hi");
    	}
    }

    is the exact same as:

    public class Example { public static void main(String[] args) {
    int count = 25			if (count == 30)
    		System.out.println("Hello");
    	else System.out.println("Hi");
    }}


    Types of errors:


    compile-time error: this means that you have wrong syntax or you're trying
    to do something that java doesn't allow (example: if you forget to use ;
    at the end of a print statement then you'll get a compile-time error).
    If a compile time error occurs the class file will not be produced.

    logical error: this means that the program does compile and run but the output
    is incorrect. Usually happens when you have the wrong formula, etc.

    runtime error: forces the program to crash, usually happens when we do things
    that cause it to terminate abnormally such as dividing by zero.

    We debug and test programs to look for errors.


    The while loop:
    while (condition) {
    statement;
    }

    if the condition is true then the statement will be repeated until the condition
    turns false.

    Example:
    public class Example {
    	public static void main(String[] args) {
    		int count = 0;
    		final int max = 5; //the amount of times we want it to be executed
     
    		while (count < max) { //while count is smaller than max
    		count++; //adds 1 to count everytime the program is executed
    		System.out.println("Hello");
    		}
    	}
    }
    This will print out Hello 5 times, after the 5th time the while condition
    will be false and the loop will be ignored.

    Nested loops: you can have a while loop inside a while loop, these are called
    nested loops.

    Escape sequences (Strings):
    \n = new line
    \" = double quote
    \\ = backslash
    \b = backspace
    \t = tab
    \' = single quote
    \r = carriage return

    Examples of use:
    public class Example {
    	public static void main(String[] args) {
    		System.out.println("Hello\nThis is an example\n\tto show you\n\"escape sequences.\"");
    	}
    }
    This will print out:
    Hello
    This is an example
    to show you
    "escape sequences."


    Now let's use what we just learnt to make a program that will read 10 inputs
    and prints out the highest number.

    Note: I will not be going into detail with the scanner class, formats, math
    classes, etc. You'll need to study these on your own.

    import java.util.Scanner;
    import java.text.DecimalFormat;

    public class Main {
    public static void main(String[] args) {
    Scanner scan = new Scanner (System.in); //command we use to be able to read an input from the user
    DecimalFormat fmt = new DecimalFormat ("0.###");

    int count = 1;
    final int max = 10;
    double highest = -999999999;
    double number;

    System.out.print("Enter a value: ");
    number = scan.nextDouble(); //gives number a value from the user

    while (count < max) {
    count++;
    if (number > highest)
    highest = number;
    System.out.print("Enter a value: "); //we add this so we don't get an infinite loop
    number = scan.nextDouble();
    if (number > highest)
    highest = number;
    }
    System.out.println(); //prints a new line
    System.out.println("The highest number was: "+highest);
    }
    }


    We can make a program that has the exact same outcome but with different code, example:

    import java.util.*;
    import java.text.*;
     
    public class Main {
        public static void main(String[] args) {
            Scanner scan = new Scanner (System.in);
            DecimalFormat fmt = new DecimalFormat ("0.###");
     
            double number1;
            double number2;
            double number3;
            double number4;
            double number5;
            double number6;
            double number7;
            double number8;
            double number9;
            double number10;
            double highest = -999999999;
     
            System.out.print("Enter a number: ");
            number1 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number2 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number3 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number4 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number5 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number6 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number7 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number8 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number9 = scan.nextDouble();
            System.out.print("Enter a number: ");
            number10 = scan.nextDouble();
     
            if (number1 > highest)
                highest = number1;
            if (number2 > highest)
                highest = number2;
            if (number3 > highest)
                highest = number3;
            if (number4 > highest)
                highest = number4;
            if (number5 > highest)
                highest = number5;
            if (number6 > highest)
                highest = number6;
            if (number7 > highest)
                highest = number7;
            if (number8 > highest)
                highest = number8;
            if (number9 > highest)
                highest = number9;
            if (number10 > highest)
                highest = number10;
     
            System.out.println("The highest number entered was: "+highest);
        }
    }
    This program has the exact same outcome as the previous one but does not
    use any while loops.

    Note: you can also use arrays.



    A quick note:

    System.out.println("25 plus 25 is: "+25+25");

    This will print out 2525 because we did not add the ().

    System.out.println("25 plus 25 is: "+ (25+25));



    Adding, subtracting, etc.:

    / = divide
    * = multiply
    + = plus
    - = minus
    ++ = +1 //example count++; this will add 1 to count's value
    -- = -1 //example count--; this will subtract one from count's value
    += means this plus this and store the value in the first one // example: count += number; this means count + number = count.
    -= opposite of the +=

    the for loop:
    for (initialization; condition; increment)
    statement;

    Example:
    for (int x = 1; x < 5; x++)
    System.out.println(x);

    If you have any questions post below.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help Getting Started With Java (tutorial)

    Please edit the post to add code or highlight tags around the code snippets. It's very hard to read as it is.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Getting Started With Java (tutorial)

    Quote Originally Posted by GregBrannon View Post
    Please edit the post to add code or highlight tags around the code snippets. It's very hard to read as it is.
    I'll work on this thread when I open my laptop.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Help Getting Started With Java (tutorial)

    Your attempt is good, but looks like implementation failed a bit...

    Perhaps it is worth splitting into several posts - or even putting to some small blog... Did not you consider this idea?

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help Getting Started With Java (tutorial)

    Quote Originally Posted by rodiongork View Post
    Your attempt is good, but looks like implementation failed a bit...

    Perhaps it is worth splitting into several posts - or even putting to some small blog... Did not you consider this idea?
    Thanks for the feedback.


    Update: made it easier to read + added for loop.

Similar Threads

  1. Getting started with java spring
    By Doyle Raymond in forum Web Frameworks
    Replies: 4
    Last Post: December 19th, 2012, 09:56 AM
  2. Java Unit Testing How-To Gettin' started
    By Massaslayer in forum Java Theory & Questions
    Replies: 2
    Last Post: May 1st, 2012, 07:58 AM
  3. newbie: Getting started with java, need help on this
    By umairrockx in forum Java Theory & Questions
    Replies: 2
    Last Post: July 21st, 2011, 05:47 AM
  4. “Getting started with RMI” tutorial for beginners
    By danielstoner in forum Java Programming Tutorials
    Replies: 0
    Last Post: October 7th, 2008, 11:18 PM
  5. “Getting started with RMI” tutorial for beginners
    By danielstoner in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: October 7th, 2008, 11:18 PM