You are on page 1of 17

JAVA TUTORIAL

By
Oni Blessing
What is JAVA?
JAVA is an object oriented language that
interprets using the JAVA virtual machine to
implement programs on the computer.
Basics Of JAVA Programing
• JAVA Fundamentals
• Classes an objects
• Method
• inheritance
JAVA Fundamentals
keywords:this are RESERVED words used in the program
(public,static,if,else,privatee.t.c)
Identifiers:They have some attributes
• sequence of one or more characters(N,n,hello)
• An identifier cannot be a keyword or true, false, or null.
• An identifier can consist of letters, digits 0–9, the underscore, or
the dollar sign.(x15)
• An identifier must start with a letter, an underscore, or a dollar
sign(long_name_i)
Data types:this are memories in which data are
stored(int,char,float,boolean,double)e.t.c
Control Structure: This consist of the loop
operations{if/else,switch,do/while,boolen
operators(>,<,&&,<+)}e.t.c
Classes and Object

Object: This are physical things such as


car,house,computer etc. but in JAVA objects
have (attribute and behavior)also called field
and method respectively.
Field: this is what make up an OBJECT
Method: this is the function of an object
Class: a class describes an object. We can have
many classes that defines an object.
Method
As it is in the previous slide METHOD is also the
behavior of an OBJECT. The function an object
perform.
They are subroutines used to describe an object
,they are represented by name and they can be
called at any point in the program.
e.g. public static main void(String [] args).
It simply means the behaviors of an object are
represented as methods in a class.
Types of Method
• Built-in: this are methods that are part of the
compiler package e.g. System.out.println()
• User defined: this are methods created by the
programmer e.g. public int getDate()(in the
workshop below)
A Simple JAVA Program
This is a simple program to display “HELLO
WORLD”

• 1. Write the source code.


• 2. Compile the source code.
• 3. Run the program.
Source code can be written with any text editor e.g.
Notepad. But it must be saved as a java file (.Java)for
example (helloworld.java)
Compiling: compiling will be done in the command
prompt using this syntax (javac “space” the java
file)e.g.(javac helloworld.java)
Running: this is done in the command prompt using
this syntax (java “space” the file name without
(.java)e.g.(java helloworld).this will only run if the
program was error free during compilation
Note: to run and compiling java programs you must
be in the directory of the java document for
example(C:\javafiles)
public class HelloWorld

public static void main(String[] args)

System.out.println("Hello World!");

}
public class program-name

optional-variable-declarations-and-subroutines

public static void main(String[] args)

statements

optional-variable-declarations-and-subroutines
}
Workshop
How many fields does the Date class have? How
many methods?
Program
public class Date

public int day, month, year;

public int getDay()

System.out.println(“Inside getDay method”);

return day;

public void printDate()

System.out.println(“Inside printDate method”);

System.out.println(month + “/” + day + “/” + year);

}
Anwser
The Date class has 3 fields and 3 Methods.
Field
int: day, month, and year,
Method
getDay() and printDate()
As you can see the program in the workshop
does not have a main.
For this program this main is called from another
public class. See the next slide for more
understanding.
Fig: Method
public class DateProgram

public static void main(String [] args)

System.out.println(“Within main...”);

Date today = new Date();

today.day = 25;

today.month = 12;

today.year = 2003;

System.out.println(“The day is “ + today.getDay());

System.out.println(“Printing the date...”);

today.printDate();

System.out.println(“What is displayed next?”);

today.getDay();

}
Expalnation

You might also like