basic of java mr. dharmesh m shah, (asst. prof) kalol inst. of management (mca) mca semester- iii...

28
Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Upload: lonnie-shaddox

Post on 28-Mar-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Basic Of Java

Mr. Dharmesh M Shah, (Asst. Prof)

Kalol Inst. Of Management (MCA)

MCA Semester- III

Subject : Fundamentals Of JAVA Programming (630002)

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 2: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

History of JAVAJAVA is an object oriented programming language. It was developed by Sun Microsystems in 1991 as a part of the research work to develop software for consumer electronics. It was designed to be small, simple and portable across platforms and operating systems, both at the source and at the binary level. It was developed as a full-fledged programming language in which one can accomplish the same sorts of tasks and solve the similar problems that one can in other programming languages, such as C++, BASIC etc.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 3: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

About JAVAModeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, both at thesource and at the binary level.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 4: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Why JAVAJava language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devices- television sets, VCRs, toasters, and the other sorts of machines you can buy at any department store. Java's goals at that time were to be small, fast, efficient, and easily portable to a wide range of hardware devices. It is those same goals that made Java an ideal language for distributing executable programs via the World Wide Web, and also a general-purpose programming language for developing programs that are easily usable and portable across different platforms. This is the major reason why Java is said to be the most successful platform independent language.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 5: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Advantages of JavaJava as a language has significant advantages over other languages and other programming environments that make it suitable for just about any programming task. Some of them are listed as follows:

AppletsAppletsappear in a Web page much in the same way as images do, but unlike images, applets are dynamic and interactive.Applets can be used to create animations, figures, or areas that can respond to input from the reader, games, or other interactive effects on the same Web pages among the text and graphics. Java enabled browsers can successfully run applets and thus give a truly rich experience at the user ends.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 6: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Platform IndependencePlatform independence is one of the most significant advantages that Java has over other programming languages, particularly for systems that need to work on many different platforms. Java is platform-independent at both the source and the binary level. Platform- independence is a program's capability of moving easily from one computer system to another. Java binary files called byte-codes are also platform-independent and can run on multiple platforms without the need to recompile the source. Byte-codes are a set of instructions that look a lot like machine code, but are not specific to any one processor. Because of them, compilation happens just once; interpretation occurs each time the program is executed. Java byte-codes help make "write once, run anywhere possible Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 7: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

SimplicityIn addition to its portability and object- orientation, one of Java's initial design goals was to be small and simple, and therefore easier to write, easier to compile, easier to debug, and, best of all, easy to learn. Keeping the language small also makes it more robust because there are fewer chances for programmers to make difficult-to-find mistakes. Despite its size and simple design, however, Java still has a great deal of power and flexibility.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 8: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Better Cousin of C, C++ Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you, because you have most of the foundation already. Although Java looks similar to C and C++, most of the more complex parts of those languages have been excluded from Java, making the language simpler without sacrificing much of its power. There are no pointers in Java, nor is there pointer arithmetic. Strings and arrays are real objects in Java. Memory management is automatic. To an experienced programmer, these omissions may be difficult to get used to, but to beginners or programmers who have worked in other languages; they make the Java language far easier to learn.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 9: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Applets and ApplicationsThe most common types of programs written in the Java programming language are applets and applications.If you've surfed the Web, you're probably already familiar with applets. An applet is a program that adheres to certain conventions that allow it to run within a Java-enabled browser. An application is a standalone program that runs directly on the Java platform. A special kind of application known as a server serves and supports clients on a network. Examples of servers are Web servers, proxy servers, mail servers, and print servers.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 10: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Another specialized program is a servlet. A servlet can almost be thought of as an applet that runs on the server side. Java Servlets are a popular choice for building interactive web applications, replacing the use of CGI scripts. Servlets are similar to applets in that they are runtime extensions of applications. Instead of working in browsers, though, servlets run within Java Web servers configuring or tailoring the server.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 11: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Dual Utility of JAVAThe programs developed in Java fall into two main categories : applications and applets.The Java Applications are general programs like any other programming language. They can perform any required operation/task or application.The Java Applets are Java Programs that are downloaded over the World Wide Web and are executed as a part of Web page, by a Web Browser on the machine. The Applets can create games, interactive programs, animations and develop multimedia effects on the web page.A program written in JAVA can be either an application or an applet or both, depending on the writing structure of the program.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 12: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Character set of JAVALetters A-Z and a-z, 0-9 digits,special symbols like Space, +, -, *, /,^, \, (, ), {, }, [, ], =, !=, <, >, ., ‘,“, $, ;, :, %, !, &, ?, -, #, @, <=, >=and white spaces like blank space,horizontal tab, carriage return,newline, form feed etc

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 13: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Tokens of JAVAJAVA comprises of tokens as smallest element of theprogram. They implement various features within aprogram.

* Keywords* Identifiers* Literals* Separators* Operators

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 14: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

STRUCTURE OF A JAVA PROGRAMThe program written in Java has two main parts :* Enclosure of the program in a class definition.

* Enclosure of the body of the program in the functions.Example Program in JAVAClass exampleprogram{ public static void main(stringargs[ ]){ System.out.println(“HailThe World !”);}}; Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 15: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

In the above program, the first line declares a class named exampleprogram, where class is the reserve word.Exampleprogram is an identifier as supplied by the programmer.This is followed by brace {, which indicates the beginning of the block of executable statements.The third line : public static void main(string args[ ])defines the function main( ). The declaration of the main() function contains the following keywords:Public, Static and Void.The Public keyword acts as an access specifier which declares the main function to be publically accessible for other classes.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 16: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

The Static definition declares that the very function or method is independent of the entire class and does not figure as a part of any objects of the class. In the above program, the class example program is unchanging or static, as a state. The term static means unchanging and implicitly final.The Void is a data type modifier which defines the main() function of no return type.The argument of the main() function lies within braces eg. (String args[ ]), here String args[ ] declares an argument parameter as argsc ontaining an array of objects of the class type String.It specifies that the main method takes an argument that is an object of string.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 17: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Programming Instructions In JAVAOutput Statements:The println ( ) method or the function is used to display information on the screen. It also provides a line feed i.e. transfers the control to the next line of the output.Example:System.out.println(“Hello”);

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 18: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Remark Statement of JAVAThe Java language offers provision of remark within its program in the following three ways :(i). Using /* and */eg. /* This program prints the sum of two numbers */

(ii). Using // for a single line of comment.eg. // This program prints the sum of two numbers

(iii). Using /** and */eg. /** This program prints the sum of two numbers */

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 19: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Data Types (int)Type Size Min. Range Max.Rangebyte One Byte -128 127short Two Bytes -32,768 32,767int Four Bytes -2,147,483,648 To 2,147,483,647

long Eight Bytes -9,223,372,036,854,775,808 To

9,223,372,036,854,775,807

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 20: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Data Types (float)This type of data includes numbers with decimal part or fractional numbers. It is further classified into two types as float and double with references as :Type Size Min. Range Max. Rangefloat 4 3.4e-038 to 3.4e+038double 8 1.7e-308 to 1.7e+308

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 21: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Escape Sequences in JAVAEscape Meaning\n Newline\t Tab\b Backspace\r Carriage return\f Form feed\\ Backslash\’ Single Code\” Double Code\ddd Octal\xdd Hexadecimal\udddd Unicode CharacterMr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 22: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Declaration of Variables in JAVAA Declaration of a variable refers to the following:(i). Specification of the name of the variable.(ii). Specification of the data type of the variable.(iii). Specification of the scope of usage of the variable.eg.Int age;String name;Boolean isAbsent;

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 23: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Arithmetic OperatorsOperator Meaning Format+ Addition a+b- Substraction a-b* Product a*b/ Division a/b% Modulus a%bwhere a and b are the two numeric constants.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 24: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Assignment OperatorsOperator / Operation /Format / Meaning = Assignment a=10 10 assigned to the

variable a+= Add to the

variable a+=5 a=a+5-= Subtract from

variable a-=5 a=a-5*= Multiply to

variable a*=5 a=a*5/= Divide into a/=5 a=a/5%= Modulus of a%=5 a=a%5

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 25: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Escape Sequences in JAVAEscape Meaning\n Newline\t Tab\b Backspace\r Carriage return\f Form feed\\ Backslash\’ Single Code\” Double Code\ddd Octal\xdd Hexadecimal\udddd Unicode CharacterMr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 26: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Shorthand NotationShorthand Operation Equivalence Operationsum +=10; sum = sum+ 10;sum -=10; sum = sum-10;sum /=10; sum = sum/ 10;sum *=10; sum = sum* 10;sum %=10; sum = sum% 10;(Similarly a variable within an expression on the right can also be used as :)sum +=total; sum = sum+ total;

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 27: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Increment & Decrement OperatorsThe increment operator is indicated by ++ and is used to increment the variable by 1.eg. ++marks;increments the value of the variable marks by 1.It can be used in two ways : as a prefix, (++var), meaning that the operator precedes the variable; and as a postfix, (var++), where the operator follows the variable.In prefix the variable gets incremented first where as in postfix the variable gets incremented after the operation of function of its associate operator.Decrement Operatorsare similar to Increment Operators except the fact for the operation of decrementationby 1. It can also be used both as prefix and postfix forms of its format.eg. --marks;is used to decrement the value of marks by 1Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)

Page 28: Basic Of Java Mr. Dharmesh M Shah, (Asst. Prof) Kalol Inst. Of Management (MCA) MCA Semester- III Subject : Fundamentals Of JAVA Programming (630002) Mr

Ternary Operator of JAVAThis is an excellent substitute to the control statement like IF/THEN/ELSE of BASIC language. It is denoted by the combinational characters ?: and follows the usage with the following format/syntax :Value variable = (test expression) ? expression1 : expression2;which indicates that if the test expression is true then the transfer value for storage into the value variable is expression1 and if the test expression is false the transfer value becomes the expression2. for example:grade = (total > 40) ? ‘P’: ‘F’;In the above example grade refers to the value variable, total>40 is the test expression, ‘P’is the expression1 and ‘F’is the expression2. The ? character follows the true condition and : follows the false condition.

Mr. Dharmesh M Shah, Asst. Prof, Kalol Inst. Of Management (MCA)