Professional Documents
Culture Documents
References: Java JDK6 documentations. http://java.sun.com/javase/ Java How to Program, Sixth Edition
History of Java
Java
Originally for intelligent consumer-electronic devices (cell phones) Then used for creating Web pages with dynamic content Now also used for:
Develop large-scale enterprise applications
2
JVM an Portability
Through the Java VM, the same application is capable of running on multiple platforms.
Java Technology
Java EE vs. Java SE
EE: enterprise edition (web services, distribution, RMI, ) SE: standard edition (stand alone app.)
Development Tools
The main tools you'll be using are the javac compiler, the java launcher (java), and the javadoc documentation tool.
Integration Libraries
Application Programming Interface (API) Java RMI, and Java Remote Method Invocation over Internet Inter-ORB Protocol Technology (Java RMI-IIOP Technology) enable database access.
9
Install the Textpad editor which we will use to develop our applications.
10
11
Hello world
To begin you need a text editor.
Notepad, TextPad, Create a new directory on C: name it JavaProjects.
Open new file, save it as HelloWorld.java in new directory HelloWorld inside the JavaProjects Directory. Write the following code and save the file: class HelloWorld { /*simple java application*/ public static void main(String[] args) { System.out.println(Hello World); //displays a string } } Note: the file name should have the same name as the class
12
Closer Look
Three primary components: source code comments, the HelloWorld class definition, and the main method. Comments are ignored by the compiler but are useful to other programmers. Two supported kinds of comments /* text */ The compiler ignores everything from /* to */. // text The compiler ignores everything from // to the end of the line. The most basic form of a class definition is
14
class name {} Every program must have at least one class, and this class should contain the main method.
This is the string array that will contains the command line arrguments.
The main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
System.out.println("Hello World!");
15
uses the System class from the API to print the "Hello World!" message to standard output.
Example, Prints.java
import java.lang.System;
class Prints { public static void main(String[] args) { System.out.print(":Haya "); System.out.println("This Text in one line"); System.out.printf("\nFormated text;\n%s\t%s\n%s\t%d\n%s\t%d\n","Student","Mark","Ahmad",3,"Sami",5); System.out.printf(" %x\n",15); // f System.out.printf(" %o\n",15); // 17 } }
16
17
Example
Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Java is case sensitive Examples: Print, $Print, _Print, Print7 is valid 7print is invalid
18
Summation cont.
import: used to get the library (Java API) contains the definition for the used classes We need the scanner class defined in java.util.scanner package All imports must be at the beginning, before class definition. Using a Java API without importing the required package cause syntax error. A Scanner enables a program to read data (e.g., numbers). The data can come from many sources, such as a file on disk or the user at the keyboard (System.in). By default, package java.lang is imported in every Java program, this package contains the definition for System class. We uses Scanner object input's nextInt method to obtain an integer from the user at the keyboard.
20
21
What Is an Object?
Real world consists of objects (desk, your television set, your bicycle). They share two characteristics: They all have state and behavior. Bicycles have state (current gear, current speed) and behavior (changing gear, applying brakes). Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation.
22
What Is a Class?
In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. class Bicycle {
int speed = 0; int gear = 1; void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println( speed:"+speed+" gear:"+gear); }
}
24
What Is Inheritance?
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from: class MountainBike extends Bicycle { // new fields and methods defining a mountain bike // would go here }
26
What Is an Interface?
An interface is a group of related methods with empty bodies. interface Bicycle {
void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement);
} To implement this interface, the name of your class would change (to ACMEBicycle, for example), and you'd use the implements keyword in the class declaration: class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }
27
What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces. you can think of packages as being similar to different folders on your computer.
28
Language Basics
Variables Operators Expressions, Statements, and Blocks Control Flow Statements
29
Variables
Four kinds: Instance Variables (Non-Static Fields).
Values are unique to each instance (object) of a class
Variables Naming
Variable names are case sensitive. Can be any legal identifier an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign, "$", or the underscore character, "_". If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. Ex: GearRatio
31
byte: 8-bit signed integer. short : 16-bit signed integer int: 32-bit signed integer long: 64-bit signed integer
32
33
34
Literals
Ex: boolean result = true; char capitalC = 'C' ; byte b = 100; short s =10000; int i = 100000; int: int decVal = 26; // The number 26, in decimal int hexVal = 0x1a; // The number 26, in hexadecimal float and double: E or e (for scientific notation) F or f (32-bit float literal) D or d (64-bit double literal). double d1 = 123.4; double d2 = 1.234e2; // same value as d1, but in scientific notation float f1 = 123.4f;
35
Literals cont.
char and String: char ch='\u0108;
36
Arrays
An array is a container object. Each item in an array is called an element, and each element is accessed by its numerical index. Declaring a Variable to Refer to an Array type[] <arrayname>; Ex: int[] arrayOfIntegers; float arrayOfFloats[ ]; // this form is discouraged Creating, Initializing, and Accessing an Array <array_name>=new type[<size>]; Ex: arrayOfIntegers = new int[10]; // create an array of integers Or you can create and initialize at the same time: int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}; 37
38
Multidimensional Arrays
39
Operator
40
Operators cont.
The + operator can also be used for concatenating (joining) two strings together, as shown in the following ConcatDemo class ConcatDemo {
public static void main(String[] args) { String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; System.out.println(thirdString); }
41
42
43
Decision-making statements .
void applyBrakes(){
if (isMoving){ // the "if" clause: bicycle must be moving currentSpeed--; // the "then" clause: // decrease current speed } } The opening and closing braces are optional, provided that the "then" clause contains only one statement void applyBrakes(){ if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle has already stopped!"); }
}
46
47
48
49
50
}
51
}
52
}
53