Working with Data Variables

Download Report

Transcript Working with Data Variables

Java Basics
Tonga Institute of Higher
Education
How Object Oriented Programs
Work
The building block of an object-oriented
language is an object.
 Object - A self-contained entity that
contains data and procedures to
manipulate the data.
 An object is like a tool that we can use to
do things.

Classes
/*
This program displays “Hello World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
//Displays a message
System.out.println(“Hello World!");
}
}



Class - The blue print or design for creating an object.
Each program must have a class
Each class has a name






Class names should be nouns
One word
The first letter is capitalized.
The first letter of each internal word is capitalized.
Keep your class names simple and descriptive.
Example:




Customer
SalesOrder
You need an open bracket and a close bracket after the name of the class. The contents of the
class is between the brackets
Everything inside a class is indented.
Demonstration
Create a class
Methods
/**
This program displays “Hello World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
//Displays a message
System.out.println(“Hello World!");
}
}


Methods - Pieces of code that perform a single function
Each method has a name






Method names should be verbs
One word
The first letter is lower case.
The first letter of each internal word is capitalized.
Keep your method names simple and descriptive.
Example:





runFast
getBackground
showWelcomeMessage
You need an open bracket and a close bracket after the name of the method. The
contents of the method is between the brackets
Everything inside a method is indented.
main() Method
/**
This program displays “Hello World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
//Displays a message
System.out.println(“Hello World!");
}
}


Each Java program must have a method called main().
The work of the program is started in the main method
when the class is run from the command line prompt.
Demonstration
Creating methods
Creating main methods
Details

Java requires a lot of attention to detail!

All Java statements must end with a semi-colon.
 Line breaks are ignored.
 Indentations are ignored.
 Even spaces are ignored


except in quoted Strings.
Use indentations and spaces to improve
readability
Case is important. Always check capitalization!
 Good
 System.out.println(“I love Java”);
 Bad
 system.out.Println(“I love Java”);