Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in JAVA

Similar presentations


Presentation on theme: "Object Oriented Programming in JAVA"— Presentation transcript:

1 Object Oriented Programming in JAVA
Object Oriented Programming in JAVA DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1

2 Introduction to OOP The Java platform realizes the differing challenges facing developers and offers the choice of different Java technologies depending on the user's needs .. Java Standard Edition The Java Standard Edition (Java SE) is for building desktop applications and applets. These applications typically serve only a small number of users at one time. Java Enterprise Edition The Java Enterprise Edition (Java EE) is tailored for more complex applications to suit medium to large businesses. Typically they will be server based applications focusing on serving the needs of lots users at one time. Java Micro Edition The Java Micro Edition is for applications used on mobile (e.g., cell phone, PDA) and embedded devices (e.g., TV tuner box, printers

3 Java Standard Edition(J2SE)
.. Using Java Standard Edition we can develop two types of Java programs. Stand-alone applications Are the programs written in Java to carry out certain tasks on a stand-alone local computer. Web applets Applets are small Java programs developed for Internet applications. An applet located on a distant computer (Server) can be downloaded via Internet and executed on a local computer. (Client) using a Java-capable browser.

4 Java Standard Edition(J2SE)
..

5 Simple Java Program .. Class Declaration
First line class Hello declares a class. Java is a pure object oriented language and therefore, everything must be placed inside a class. Class is a keyword and declares that a new class definition follows. Hello is a Java identifier that specifies the name of the class to be defined.

6 Simple Java Program… Opening Brace
.. Opening Brace Every class definition in Java begins with an opening brace “{” and ends with a matching closing brace “}”, appears in the last line in the above example. The main Line. public static void main(String args[]) defines a method named main. Conceptually, this is similar to the main() function in C. Every Java application program must include the main() method. This is the starting point for the interpreter to begin the execution of the program. A Java application can have any number of classes but only one of them must include a main method to initiate the execution.(note that Java applets will not use the main method at all.)

7 Simple Java Program… .. This line contains a number of keywords, public, static and void.

8 Simple Java Program… The Output Line
.. The Output Line The only executable statement in the program is System.out.println(“Hello World!”); This is similar to the printf() statement in C .The println method is a member of the out object, which is instance of System class. This line prints the string; Hello World Note the semicolon at the end of the statement. Every Java statement must end with a semicolon.

9 Simple Java Program… Creating the Program.
A Java program can be created using any text editor (notepad is widely used). This file is called the source file and should be saved in the format filename.java. have the extension Java. Compiling the Program To compile the program should have the Java Compiler javac. This can be installed to a local computer by installing the Java Development Kit (JDK). After installation, the source code can be compiled using the following command on the command line. javac Hello.java .. If there are no syntax errors in the source code, the javac compiler creates a file called Hello.class containing the bytecodes of the program.

10 Simple Java Program… Running the Program
.. Running the Program Java interpreter is use to run a stand-alone programs by typing the following at the command prompt. java Hello Now the interpreter looks for the main method in the program and begins execution from there. Then displays Hello World! on the screen. Note that to run the program simply type “Hello” at the command line and not “Hello.class”.

11 Machine Neural (Platform Independent)
..

12 Machine Neural (Platform Independent)…
.. JAVA VIRTUAL MACHINE All language compilers translate source code into machine code for a specific computer. Java compiler also does the same thing. Then, how does Java achieve platform independence? The answer is that the Java compiler produces an intermediate code known as bytecode for a machine that does not exist. This machine is called the Java Virtual Machine and it exists only inside the computer memory. It is a simulated computer within the computer and does all major functions of a real computer.

13 Machine Neural (Platform Independent)…
.. JAVA VIRTUAL MACHINE The virtual machine code is not machine specific. The machine specific code (known as machine code) is generated by the Java interpreter by acting as an intermediary between the virtual machine and the real machine. Note that the interpreter is different for different machines.

14 Constants Variables and Data Types
Constants Variables and Data Types DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 14 14

15 Constants .. Constants in Java refer to fixed values that do not change during the execution of a program. Java supports several types of constants. 1. Integer Constants 2. Real Constants 3. Single Character Constants 4. String Constants 5. Backslash Character Constants.

16 Constants…

17 Constants… Integer Constants
An integer constant refers to a sequence of digits. There are three types of integers decimal integer octal integer hexadecimal integer Decimal integers consist of a set of digits, 0 through 9, preceded by an optional minus sign. Valid examples of decimal integer constants are: Embedded spaces, commas, and non-digit characters are not permitted between digits. For example. ,000 $1000 are illegal numbers.

18 Constants… Real Constants
Integer number are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices, and so on. These quantities are represented by numbers containing fractional parts like Such numbers are called real (or floating point) constants. It is possible that the number may not have digits before the decimal point or digits after the decimal point.

19 Constants… Single Character Constants
a single character constant (or simply character constant) contains a single character enclosed within a pair of single quote marks. Examples of character constants are: ' 5 ' ' X ‘ ' ; ' Note that the character constant ' 5 ' is not the same as the number 5. String Constants A string constant is a sequence of characters enclosed between double quotes. The characters may be alphabets, digits, special characters and blank spaces. String constants are: “ Hello Java ” “ 1997 ” “ ?....! ” “ 5+3 ” “ X ”

20 Constants… Backslash Character Constants
Java supports some special backslash character constants that are used in output methods. Note that each one of them represents one character, although they consists of two characters.

21 Variables A variable is an identifier that denotes a stored location used to store a data value. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different times during the execution of the program. Declaration of a variable Syntax: <data_type><variable_name>; Eg: int a; float total; String name;

22 Data Types

23 Data Types… Integer Types
Integer types can hold whole numbers such as 123, -96 or 5639. The size of the values that can be stored depends on the integer data type we choose. Java supports four types of integer as shown below.

24 Data Types… Floating Point Types
Integer types can hold whole numbers and therefore floating point number types are used to hold numbers containing fractional parts such as and There are two kinds of floating point storage in Java as shown below.

25 Data Types… Floating Point Types
Floating point numbers are treated as double-precision quantities. To force them to be in single-precision mode, we must append f or F to the numbers. Example: 1.23f 1.23F Double-precision types are used when we need greater precision in storage of floating point numbers. All mathematical functions, such as sin, cos and sqrt return double type values.

26 Data Types… Character Type
In order to store character constants in memory, Java provides a character data type called char. The char type assumes a size of 2 bytes but, basically, it can hold only a single character. Boolean Type Boolean type is used when we want to test a particular condition during the execution of the program. There are only two values that a boolean type can take: true or false. Both these words have declared as keywords. Boolean uses only one bit of storage.

27 Naming Variables Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use. Rules and conventions for naming your variables can be summarized as follows: All variable names must begin with a letter of the alphabet, an underscore ( _ ), or a dollar sign ( $ ). But the convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged. After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces or special characters such as *, %, & and so on are allowed. Variable names are case-sensitive. It is a conversion, when there are more than one word for a variable name First letter of the first word is lower case . And the first letter of the proceeding words are upper case.

28 Scope of Variables… Java variables are classified into three kinds.
Instance Variables Instance variables are declared inside a class but outside the methods of the class. Instance variables are created when the objects are instantiated and therefore they are associated with the objects. They take different values of each object. Class variables(Static variables) Class variables are also declared inside a class but outside of all the methods in the class. Class variables global to a class and belong to the entire set of objects created from that class. Only one memory location is allocated for each instance of the class variables.

29 Scope of Variables Local Variables
Variables declared and used inside methods are called local variables. They are not available for use outside the method definition. Local variables can also be declared inside program blocks that are defined between an opening brace “{” and a closing brace “}”. These variables are visible to the program only from the beginning of its program block to the end of the program block. When the program control leaves a block, all the variables in the block will cease to exist


Download ppt "Object Oriented Programming in JAVA"

Similar presentations


Ads by Google