Introduction to JAVA Introduction about JAVA JAVA was

Introduction to JAVA

Introduction about JAVA Ø JAVA was developed by Sun Microsystems Inc in 1991, later acquired by Oracle Corporation. Ø It helps to create modular programs and reusable code. Ø Compilation of program is done by javac compiler, javac is the primary java compiler included in java development kit (JDK). It takes java program as input and generates java bytecode as output. Ø JVM executes the bytecode generated by compiler.

PLATFORM INDEPENDENT LANGUAGE-JAVA • Each operating system has different JVM, however the output they produce after execution of byte code is same across all operating systems

Steps • • Step 1: Open a text editor, like Notepad on windows Step 2: Save the file with. java extension Step 3: Name the file same as the class name Step 4: Write the Code Step 5: Open command prompt (cmd) on Windows Step 6: Set the complete path of bin directory in cmd window Step 7: Compile the program using the following command javac filename. java • Step 8: Run the program using the following command java filename

Terms • public: This makes the main method public that means that we can call the method from outside the class. • static: We do not need to create object for static methods to run. They can run itself. • void: It does not return anything. • main: It is the method name. This is the entry point method from which the JVM can run your program. • (String[] args): Used for command line arguments that are passed as strings.

Characteristics of java Simple : • Java is Easy to write and more readable and eye catching. • Java has a concise, cohesive set of features that makes it easy to learn and use. • Most of the concepts are drew from C++ thus making Java learning simpler. Secure : • Java program cannot harm other system thus making it secure. • Java provides a secure means of creating Internet applications. • Java provides secure way to access web applications. Portable : • Java programs can execute in any environment for which there is a Java run-time system. (JVM) • Java programs can be run on any platform (Linux, Window, Mac) • Java programs can be transferred over world wide web (e. g applets)

Continues Object-oriented : • Java programming is object-oriented programming language. • Like C++ java provides most of the object oriented features. • Java is pure OOP. Language. (while C++ is semi object oriented) Robust : • Java encourages error-free programming by being strictly typed and performing run-time checks Multithreaded : • Java provides integrated support for multithreaded programming. Architecture-neutral : • Java is not tied to a specific machine or operating system architecture. • Machine Independent i. e Java is independent of hardware.

Continues Interpreted : • Java supports cross-platform code through the use of Java bytecode. • Bytecode can be interpreted on any platform by JVM. High performance : • Bytecodes are highly optimized. • JVM can executed them much faster. Distributed : • Java was designed with the distributed environment. • Java can be transmit, run over internet. Dynamic : • Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time.

List of OOP Concepts in Java There are four main OOP concepts in Java. These are: • Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it. In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times. • Encapsulation. This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself. This way, we can re-use objects like code components or variables without allowing open access to the data system-wide.

Continues • Inheritance. This is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel. • Polymorphism. This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables

How OOP Concepts in Java Work

How Abstraction Works • Abstraction as an OOP concept in Java works by letting programmers create useful, reusable tools. For example, a programmer can create several different types of objects. These can be variables, functions, or data structures. Programmers can also create different classes of objects. These are ways to define the objects. • For instance, a class of variable might be an address. The class might specify that each address object shall have a name, street, city, and zip code. The objects, in this case, might be employee addresses, customer addresses, or supplier addresses.

How Encapsulation Works • Encapsulation lets us re-use functionality without jeopardizing security. It’s a powerful OOP concept in Java because it helps us save a lot of time. For example, we may create a piece of code that calls specific data from a database. It may be useful to reuse that code with other databases or processes. Encapsulation lets us do that while keeping our original data private. It also lets us alter our original code without breaking it for others who have adopted it in the meantime.

How Inheritance Works • Inheritance is another labor-saving Java OOP concept. It works by letting a new class adopt the properties of another. We call the inheriting class a subclass or a child class. The original class is often called the parent. We use the keyword extends to define a new class that inherits properties from an old class.

How Polymorphism Works • Polymorphism in Java works by using a reference to a parent class to affect an object in the child class. We might create a class called “horse” by extending the “animal” class. That class might also implement the “professional racing” class. The “horse” class is “polymorphic, ” since it inherits attributes of both the “animal” and “professional racing” class. • Two more examples of polymorphism in Java are method overriding and method overloading. • In method overriding, the child class can use the OOP polymorphism concept to override a method of its parent class. That allows a programmer to use one method in different ways depending on whether it’s invoked by an object of the parent class or an object of the child class. • In method overloading, a single method may perform different functions depending on the context in which it’s called. That is, a single method name might work in different ways depending on what arguments are passed to it.
![Character • char ch = 'a'; • char[] char. Array = { 'a', 'b', Character • char ch = 'a'; • char[] char. Array = { 'a', 'b',](http://slidetodoc.com/presentation_image_h2/4b4e44e1a3f561240696cd10d3a35285/image-16.jpg)
Character • char ch = 'a'; • char[] char. Array = { 'a', 'b', 'c', 'd', 'e' }; // an array of chars • Character ch = new Character('a'); // Character object with the Character constructor

Methods in Character class Method Description boolean is. Letter(char ch) boolean is. Digit(char ch) Determines whether the specified char value is a letter or a digit, respectively. boolean is. Whitespace(char ch) Determines whether the specified char value is white space. boolean is. Upper. Case(char ch) boolean is. Lower. Case(char ch) Determines whether the specified char value is uppercase or lowercase, respectively. char to. Upper. Case(char ch) char to. Lower. Case(char ch) Returns the uppercase or lowercase form of the specified char value. to. String(char ch) Returns a String object representing the specified character value — that is, a onecharacter string.

Array • An array is a group of like-typed variables that are referred to by a common name. • In Java all arrays are dynamically allocated • Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.
![Continues Declaration type var-name[]; OR type[] var-name; Instantiating an Array in Java var-name = Continues Declaration type var-name[]; OR type[] var-name; Instantiating an Array in Java var-name =](http://slidetodoc.com/presentation_image_h2/4b4e44e1a3f561240696cd10d3a35285/image-19.jpg)
Continues Declaration type var-name[]; OR type[] var-name; Instantiating an Array in Java var-name = new type [size]; Example: int. Array[]; //declaring array int. Array = new int[20]; // allocating memory to array OR int[] int. Array = new int[20]; // combining both statements in one

Array Literal • If the size of the array and values of array are already known, array literals can be used. Example: int[] a= new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; a. length returns 10

For-each • For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java 5. • It starts with the keyword for like a normal for-loop. • Instead of declaring and initializing a loop counter variable, A variable can be declared as a loop counter, followed by a colon, which is then followed by the array name. • In the loop body, the loop variable can e incremented by default it will be incremented by 1

Syntax: for (type var : array) { statements using var; }

Limitations • For-each loops do not keep track of index. • For-each only iterates forward over the array in single steps • For-each cannot process two decision making statements
- Slides: 23