• Introduction to Object
• Elements of an object : attribute, behavior, state
• Characteristics of OOP : abstraction, encapsulation, inheritance, polymorphism
• Messages passing
1.0 HISTORY OF JAVA
- Java was developed by a team led by James Gosling at Sun Microsystems, a company best known for its Sun workstations.
- 1991 – designed by Sun Microsystems as an internal corporate research project originally called Green then Oak. It was designed for use in embedded consumer electronic applications.
- Mid 1990‟s - Java was used to write applets.
- 1993 - World Wide Web exploded in popularity – Sun saw the potential of using Java to create Web pages – with interactive and dynamic content.
- 1998 - Java 2 was released and become popular for writing application program.
- The power of Java is not limited to Web applications, for it is a general-purpose programming language.
- It has full programming features and can be used to develop standalone applications.
1.1.1 Java Is Simple
- No language is simple, but Java is a bit easier that the popular object-oriented programming language C++, which was the dominant software-development language before Java.
- Java is partially modeled on C++, but greatly simplified and improved.
- For instance, Java uses automatic memory allocation and garbage collection, whereas C++ requires the programmer to allocate memory and collect garbage.
- Computers do not understand human languages, so you need to use computer languages to communicate with them.
- There are more than 100 programming languages. Some of the most popular language are:
- COBOL (COmmon Business Oriented Language)
- FORTRAN (FORmula TRANslation)
- BASIC(Beginner All-purpose Symbolic Instructional Code)
- C(So named because its developer designed B first)
- C++ (an object-oriented language, based on C)
- All of these languages except C++ are procedural programming languages – based on paradigm of procedures.
- Object-oriented programming models the real world in terms of objects.
- Everything in the world can be modeled as object – student, car, book, account, apple, etc.
- A Java program is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together.
- Distributed computing involves several computers working together on a network.
- Java is designed to make distributed computing easy.
- As an example, Figure 1.1 shows three programs running on three different system; the three programs communicate with one another to perform a joint task.
Figure 1.1 Java programs can run on different systems that work together.
1.1.4 Java Is Interpreted
- Java programs are compiled into the Java Virtual Machine code called bytecode.
- The bytecode is machine-independent and can run on any machine that has a Java interpreter.
- Robust means reliable.
- No programming language can ensure complete reliability.
- Java puts a lot of emphasis on early checking for possible errors, because Java compilers can detect many problems that would first show up at execution time in other languages.
- Java has a runtime exception-handling feature to provide programming support for robustness.
- As an Internet programming language, Java is used in a networked and distributed environment.
- If you download a Java applet and run it on your computer, it will not damage your system because Java implements several security mechanisms to protect your system against harm caused by stray program.
- The security is based on the premise that nothing should be trusted.
- Java is interpreted. This feature enables Java to be architecture-neutral/platform-independent.
- With a Java Virtual Machine (JVM), you can write one program that will run on any platform.
- Java is architecture neutral.
- This feature enables Java programs to be portable because they can be run on any platform without being recompiled.
- Moreover, there are no platform-specific features in the Java language.
- The Java environment is portable to new hardware and operating systems. In fact, the Java compiler itself is written in Java.
- Java‟s performance is sometimes criticized.
- The execution of the bytecode is never as fast as it would be with a compiled language, such as C++.
- Because Java is interpreted, the bytecode is not directly executed by the system, but is run through the interpreter.
- However the speed is more than adequate for most interactive applications, where the CPU is often idle, waiting for input or for data from other sources.
- Sun recently developed the Java HotSpot Performance Engine, which includes a compiler for optimizing the frequently used code. It can be plugged into a JVM to dramatically boost its performance.
- Multithreading is a program‟s capability to perform several tasks simultaneously.
- For example, downloading a video file while playing the video would be considered multithreading.
- Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading.
- Java was designed to adapt to an evolving environment.
- New code can be loaded on the fly without recompilation.
- There is no need for developers to create, and for users to install, major new software versions.
- New features can be incorporated transparently as needed.
- The following table shows the differences between C++ and Java:
1.3 INTRODUCTION TO JAVA APPLICATION
- Java programs:
- Java applications – standalone programs; applications can be executed from any computer with a Java interpreter.
- Applets – special kinds of Java programs that can run directly from a Java-compatible Web browser; suitable for Web projects.
- Writing simple Java application:
//---------------------------------------------------------------------------------------
/* Program name: FirstProgram.java
Purpose: This application programs prints
Welcome to Java Programming
*/
public class FirstProgram
{
public static void main(String args[])
{
System.out.println(“Welcome to Java Programming”);
}//end of method main
}//end of class
//---------------------------------------------------------------------------------------
1.4 IDENTIFIERS
- Identifier is a name given to a variable, class or method.
- Identifiers start with a letter, underscore ( _ ), or dollar sign ($).
- Subsequent characters can be digits.
- Identifiers are case-sensitive and have no maximum length.
- Examples:
- student
- mySchool
- student_name
- subject1
- $money
- _a
- An identifier cannot be a keyword, but it can contain a keyword as part of its name. For example, thisOne is a valid identifier, but this is not because this is a Java technology keyword.
1.5 JAVA KEYWORDS
- Keywords have special meaning to the Java technology compiler.
- They identify a data type name or program construct name.
- The following table list the keywords that are used in the Java programming language:
Table
1.6 DATA TYPES
- There are two categories of data types – class/object types and primitive types.
- Primitive types are simple values, are not objects.
- Class types are used for more complex types, including all the types you declare yourself.
- Class types are used to create objects.
- The Java programming language defines eight primitive data types, which can be considered in four categories:
- Logical – boolean
- Textual – char and String
- Integral – byte, short, int, and long
- Floating point – double and float
1.7 Logical – Boolean
- The boolean data type has two literals, true and false.
- For example, the statement:
- boolean result = true;
- declares the variable result as boolean type and assigns it a value of true.
1.8 Textual – char & String
- char – represents a 16-bit Unicode character
- Must have its literal enclosed in single quotes („ ‟)
- For example:
- char letter = „a‟;
- String – is not a primitive data type; it is a class
- Has its literal enclosed in double quotes (“ ”)
- Can be used as follows:
- String greeting = “Assalamualaikum”;
- String errorMessage = “Record not found!!”;
1.9 Integral – byte, short, int, and long
- An integer literal can be assigned to an integer variable as along as it can fit into the variable.
- Integral data types have the following ranges:
Table
- For example:
- int number = 12345;
1.10 Floating point – double and float
- Floating point literals are written with a decimal point.
- By default, a floating point literal is treated as a double type value.
- For example:
- double price = 27.70;
- double mark = 85.5;
1.11 Constants
- The value of a variable may change during the execution of the program, but a constant represents permanent data that never change.
- The word final is a Java keyword which means that the constant cannot be changed.
- For example:
- final double PI = 3.14159;
1.12 Type Casting
- Casting means assigning a value of one type to a variable of another type.
- If the two types are compatible, the Java software performs the conversion automatically.
- For example, an int value can always be assigned to a long variable.
- The desired target type is placed in parenthesis and used as a prefix to the expression that must be notified.
- For example:
- double number1 = 2.7, number2 = 7.5;
- int result1 = (int)(number1 + number2); // = ?
- int result2 = (int)number1 + (int)number2; // = ?
1.13 ARRAY OF PRIMITIVES
- An array is a collection of data values.
- If your program needs to deal with 100 integers, 500 Account objects, 365 real numbers, etc., you will use an array.
- In Java, an array is an indexed collection of data values of the same type.
- Array declaration:
- The index of the first position in an array is 0.
- Sample array processing:
//-----------------------------------------------------------------------------------------
double[] rainfall = new double[12];
double annualAverage,sum = 0.0;
for (int i = 0; i < rainfall.length; i++) {
rainfall[i] = Double.parseDouble(
JOptionPane.showinputDialog("Rainfall for month " + (i+1) ) );
sum += rainfall[i];
}
annualAverage = sum / rainfall.length;
//-----------------------------------------------------------------------------------------
Array Initialization
- Like other data types, it is possible to declare and initialize an array at the same time.
- Example:
- Variable-size Declaration
- In Java, we are not limited to fixed-size array declaration.
- The following code prompts the user for the size of an array and declares an array of designated size:
- Example:
Figure
1.14 CONTROL STRUCTURE
1.14.1 Branching Statements
- The Java programming language supports the if and switch statements for two-way and multiple-way branching, respectively.
- The if, else statement:
if (boolean expression){
statement or block;
}
if (boolean expression){
statement or block;
}
else{
statement or block;
}
- The switch statement:
switch (expr1){
case constant2: statements;
break;
case constant3: statements;
break;
default: statements;
break;
}
1.14.2 Looping Statements
- Looping statements allow you to execute blocks of statements repeatedly.
- The Java programming language supports three types of loop constructs: for, while and do loops.
- The for loops:
for(init_expr; boolean testexpr; alter_expr){
statements or block;
}
- The while loops:
while (boolean){
statements or block;
}
- The do/while loops:
statements or block;
} while(boolean test);
1.15 PACKAGES
- A Java package is a group of related classes and interfaces.
- It facilitates software reuse by allowing programs to import classes from other packages libraries to be shared by many users.
- For example: to use the JOptionPane class in the javax.swing package, include the import statement before class declaration:
- import javax.swing.JOptionPane;
1.16 INPUT & OUTPUT STATEMENT
1.16.1 Using Dialog Box
- Input – the showInputDialog method in javax.swing.JoptionPane package is use.
- Has one String parameter which is the message to be displayed to the user.
- Returns the String keyed in by the user and returns the null String if user cancels. Example:
input = JOptionPane.showInputDialog(“Enter a
number”);
- To do mathematical operations on the given data, the String returned by showInputDialog must be converted to the required numerical data type.
- The parse functions in various wrapper classes are used:
float nom = Float.parseFloat(input);
double x = Double.parseDouble(input);
- Output – the showMessageDialog method is used.
- Has one String parameter which is the message to be displayed to the user.
output = “The answer is “ + num;
JOptionPane.showMessageDialog(null, output);
1.16.2 Using Console I/O
- Input using the Scanner class (available in Java 5).
- Create a Scanner object by passing an input stream to the constructor
- Use the nextInt(), nextDouble(), nextLine(), next() methods to read data typed on the keyboard.
int num = scanner.nextInt();
- Output is done through the System.out.print or System.out.println methods:
System.out.println(“x = ”+ x + “y = ” + y);
Reference:
- Liang, Y. Daniel, Introduction to Java Programming, 8th Edition, Pearson, 2011.
- Wu, C. Thomas, An Introduction to Object Oriented Programming with Java, 4th Edition, Mc Graw Hill, 2006.
No comments:
Post a Comment