Getting Start With Java

Java compliers are free to download, and may be use at no cost. We would need to download the jdk first. Many sites allow you to
download the jdk, and the most common and most often used one is "
www.java.sun.com". The complier that I'm using is called "jGrasp"
we can obtain it at "www.jgrasp.org". Once we have everything, we can start writing our programs.

 

The syntax of java is a little bit different from C++, I'll use examples to help us to understand how the syntax work in java.(Note: Java is case sensitive)
We can write comments in java. Comments are simply statements that is ignored by the compiler. It is often used by programmers to help themselves and/or other
people who is reading their program, to understand the program easier. Here are two ways of writing comments.

End of line comments

Anything you type after // will be ignored by the compiler.

Ex)

sum = sum + price;     // calculate the total price

Traditional comments

It begins with the sequence /* and ends with the sequence */. Anything inside /*    &    */    will be ignored by the compiler.

Ex)

/*    Hello! Reader, anything inside here is a comment, it will be
       ignored by the compiler. It does not matter how long your comment is
       as long as you didn't forget the end escape, this whole statement will be ignored

*/

 

 

Every java programs must consist of these things.

    public class NameOfClass
    {
            public static void main(String[] args)
            {
                 //statements goes here...
                //

            }
    }

            public class NameOfClass:    NameOfClass should be capital and should be a name that is meaningful ("Anyname is okay!"), the file should be saved
                                                      using the class name, which is the "NameOfClass."

            public static void main(String[] args):   When we run the program this is where we would start executing.

 

Lets see an example:

/*This program will print out "Hello World".

*
*/

public class HelloWorld
{
    public static void main(String[] args)
    {
            System.out.println("Hello World"); //Prints out Hello World
    }
}

Every statements or expressions of java should end with a semi-colon. Whenever we want to print something out, we would use System.out.println("").
That would print out whatever is inside the quotation and goes to the next line. If we use
System.out.print(""), it would print out whatever is inside the
quotation and stays on the same line.

ClickHere to download HelloWorld.java

 

Examples:

    /*    This program will print out multiple statements

    *
    */

    public class Mstatements
    {
        public static void main(String[] args)
        {
            System.out.println("Hi! My name is JJ");
            System.out.print("What is your name");
            System.out.println("My name is Jinny");

        }
    }

Here is the output of the program

Hi! My name is JJ
What is your nameMy name is Jinny

Now, look at our output, we can see that println would skip to the next line after the statement is finish executing, and print would stay on the same line.

ClickHere to download Mstatements.java

 

In java programming language, all variables must be first declared before they can be use. We use primitive data type
to declare what our variable can hold.



Data Types

-- byte:    uses one byte to store values from -128 to -127

-- short:    uses two bytes to store values from 32,768 to 32,767 values from 9

-- int:    uses four bytes to store values from 2,147,483,648 to 2,147,483,647

-- long:    uses eight bytes to store from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

-- float:    uses four bytes to store values from -1.4E-45 to 3,4028235E+38

-- double:    uses eight bytes to store values from -4.9E-324 to 1.7976931348623157E+308

-- char:    can hold the value of one character

-- boolean:    can hold two values, "True or False"

None primitive data type

-- String(or any object):    Could hold words or simply null.

 

 

In java we can use escape sequence to help us do several different things such as printing new line, printing quotes and so on.
Here is a list of "Escape Sequence" and it's usage.

 

Escape Sequence                Explanation

\b                                        Backspace

\t                                        Horizontal tab

\n                                        Linefeed

\f                                        Form feed

\r                                        Carriage return

\"                                        Double quote

\'                                        Single quote

\\                                        Backslash
 


 

We have seen examples on how to print out something in java, what if we want the user to interact with the program, such as asking them for inputs.
Luckily, java offers Scanner class that can help us do the job. Before we can use any of the classes offered by java, we have to import it first.
So, if we want to use the Scanner class, at the very top left corner we would have to type "
import java.util.Scanner; "       
 

Now, that you have a little background about java, lets go back to " Java 24 " for some more interesting examples.

 

 <Java 24><Home>