Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Originally developed by James Gosling at Sun Microsystems In 1991 named “OAK” In 1995, this language was renamed as “Java”. (which.

Similar presentations


Presentation on theme: "Introduction to Java Originally developed by James Gosling at Sun Microsystems In 1991 named “OAK” In 1995, this language was renamed as “Java”. (which."— Presentation transcript:

1 Introduction to Java Originally developed by James Gosling at Sun Microsystems In 1991 named “OAK” In 1995, this language was renamed as “Java”. (which is now a subsidiary of Oracle Corporation) Java is a purely an object – oriented language. Java is simple and platform-independent. Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc.

2 JVM To enable Java programs to be executed on multiple CPUs without modifications, java programs are compiled into machine instructions that can be understood and executed by an idealized CPU. Such a CPU is called Java Virtual Machine (JVM).

3 Java – Byte code A Java program is first compiled into the machine code that is understood by JVM. Such a code is called Byte Code. This Byte Code is a platform-independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere(WORA). Although the details of the JVM will differ from platform to platform, they all interpret the Java Byte Code into executable native machine code.

4 Just In Time (JIT) The Just In Time (JIT) compiler compiles the byte code into executable machine code in real time, on a piece-by- piece demand basis.

5 JDK The JIT cannot compile the entire byte code of a Java program into executable machine code all at once, because Java Runtime Environment carries out various run time checks that can be performed only at run time. The collection of the three entities name the Java language, the Java language packages and the set of Java development tools is called the Java Development Kit (JDK)

6 Tool in JDK javac : This tool is Java compiler. It compiles the Java source code into the bytecode, which can be understood by JVM java :This tool is the Java interpreter. It interprets the byte code into machine code and then executes it.

7 Applet is a Java program that is developed exclusively for the Internet.
appletviewer: This tool helps us to execute applets program. Stand-alone applications can be executed without the internet connectivity. These applications are executed

8 Features of Java 1.Simple 2.Object-Oriented 3.Platform independent 4.Secured 5.Robust 6.Architecture neutral 7.Portable 8.Dynamic 9.Interpreted 10.High Performance 11.Multithreaded 12.Distributed

9 Features of Java Object-oriented
Object-oriented programming(OOPs) is a methodology that simplify software development and maintenance by providing some rules. Basic concepts of OOPs are: Encapsulation Abstraction Inheritance Polymorphism Object Class

10 The data and the methods are combined to together to form a class.
Song Name Author getName() getAuthor() getPlayingTime() playSong() The data and the methods are combined to together to form a class. Once we define a class, we can declare a group of objects for that class. Each object in such a group contains the data and member functions that have been defined in that class.

11 Encapsulation Encapsulation in java is a process of wrapping code and data together into a single unit, like capsule. We can create a fully encapsulated class in java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

12 Encapsulation One of the most powerful and important points of OOP is that access to a class is strictly regulated. If we wish to modify or access the data in an object, we have to issue a command to that object, which will use its member function to retrieve the requested data and communicate them. The above mentioned mode of retrieval means that the data are wantonly hidden, so that they are not directly accessible to the user. Here, the data and its related functions are said to be encapsulated into an object

13 Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. The trivial or the non-essentials units are not displayed to the user. eg: A car is viewed as a car rather than its individual components. In java, we use abstract class and interface to achieve abstraction.

14 Advantages of Abstraction
It reduces the complexity of viewing the things. Avoids code duplication and increases reusability. Helps to increase security of an application or program as only important details are provided to the user. Encapsulation is data hiding(information hiding) while Abstraction is implementation hiding. While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing the interface to the user and hiding the details of implementation.

15 Inheritance Inheritance is one the most powerful capabilities of object-oriented programming. Using this concept, a new class of objects can be derived from an old one. This process is called inheritance because the new class inherits the characteristics of the original class. The new class is called a derived class of the original class and the original class is called the base class of the new class.

16 Polymorphism When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc. In java, we use method overloading and method overriding to achieve polymorphism. Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

17

18 Polymorphism We can build up subclasses from a base class by using the concept of inheritance. We can specify a unique behavior to each subclass by using the concept of polymorphism. Thus, we can design the derived classes in such a way that they respond to a message that a base class responds to, but at the same time they can perform different functions by making use of concepts of polymorphism.

19 Class & Object Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. Collection of objects is called class. It is a logical entity.

20 class echo { public static void main(String args[]) { for (int i=0; i < args.length; i++) { System.out.println( args[i] ); } C:\UMBC\331\java>javac echo.java C:\UMBC\331\java>java echo this is pretty silly this is pretty silly

21 Hello.java class Hello { public static void main(String[] args) { System.out.println(“Hello, Welcome to Java Application”); } }

22 As Java is a pure OOP-based language, each simple Java program is written in the form of a class.  In java, a class consists of certain data members and the related methods. The System class, the out object and the println() method have all been predefined by the Java.

23 Java Variables Local Variable A variable which is declared inside the method is called local variable. Instance Variable A variable which is declared inside the class but outside the method, is called instance variable . It is not declared as static. Static variable A variable that is declared as static is called static variable. It cannot be local.

24 Java Example class A { int data=50;//instance variable static int m=100;//static variable void method() { int n=90;//local variable } //end of method. }//end of class

25 Method / functions A method is a self-contained entity that carries out a well-defined task of some kind. For example, a method named getrectArea() may be defined so as to compute the area of rectangle. The methods in class are its basic operation entities. int rectAngleArea() { return length * width; } Methods are also called functions, member functions (in class), routine or procedures.

26 1. access-specifier specifies which other methods can call this method
1. access-specifier specifies which other methods can call this method. Most methods shall be declared as public. 2. Return-type is the type of the value that is returned by the method to its caller. If no value returned by a method, then the keyword void should be specified in the place of type 3. Method-name is the name assigned to the method by the user. It shall be any valid identifier. 4. Parameter-list is a list of parameters. This list, always enclosed in parentheses contains parameter names and their types.

27 Constructors Classes should define one or more methods to create or construct instances of the class Their name is the same as the class name note deviation from convention that methods begin with lower case Constructors are differentiated by the number and types of their arguments An example of overloading If you don’t define a constructor, a default one will be created. Constructors automatically invoke the zero argument constructor of their superclass when they begin (note that this yields a recursive process!)

28 Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

29 Why use inheritance For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. Ex. class Subclass-name extends Superclass-name   {       //methods and fields   }  

30 Types of inheritance in java

31

32 class Animal { void eat() { System. out. println("eating
class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal void bark() { System.out.println("barking..."); } class Cat extends Animal void meow() { System.out.println("meowing..."); }

33 static keyword in java The static keyword in java is used for memory management mainly. It makes your program memory efficient (i.e it saves memory). Java static block The static variable can be used to refer the common property of all objects. The static variable gets memory only once in class area at the time of class loading.

34 Java static method Java static block
A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can access static data member and can change the value of it. Java static block Is used to initialize the static data member. It is executed before main method at the time of class loading.

35 why java main method is static?
because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of memory allocation.

36 this keyword in java this is a reference variable that refers to the current object. Usage of this keyword this can be used to refer current class instance variable. this can be used to invoke current class method (implicitly) this can be passed as an argument in the method call. this can be passed as argument in the constructor call. this can be used to return the current class instance from the method. this() can be used to invoke current class constructor.

37 super keyword in java The super keyword in java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable. Usage of java super Keyword super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.

38 final Keyword In Java Java final variable 2) Java final method
If you make any variable as final, you cannot change the value of final variable(It will be constant). 2) Java final method If you make any method as final, you cannot override it. 3) Java final class If you make any class as final, you cannot extend it.

39 Classpath The classpath is an argument (Environment variable) that tells the Java Virtual Machine where to look for user-defined classes and packages in Java programs. • Setting CLASSPATH from command-line SET CLASSPATH=Dir; • Setting CLASSPATH in Windows via Control- panel

40 Java Packages A Java package is a mechanism for organizing Java classes into namespaces. • A package provides a unique namespace for the types it contains. • Classes in the same package can access each other's protected members. • Packages are usually defined using a hierarchical naming pattern • Example – java.lang.*, com.globallogic.* Move the hello world program in a package com.globallogic.training.java Now compile and execute the program

41 Garbage Collection • Garbage collection (GC) is a form of automatic memory management. • The garbage collector or collector attempts to reclaim garbage, or memory used by objects that will never be accessed the application • The basic principle of how a garbage collector works is: – Determine what data objects in a program will not be accessed in the future – Reclaim the resources used by those objects • A key feature of Java is its garbage-collected heap, which takes care of freeing dynamically allocated memory that is no longer referenced.

42 An array is an object Person mary = new Person ( );
int myArray[ ] = new int[5]; int myArray[ ] = {1, 4, 9, 16, 25}; String languages [ ] = {"Prolog", "Java"}; Since arrays are objects they are allocated dynamically Arrays, like all objects, are subject to garbage collection when no more references remain so fewer memory leaks Java doesn’t have pointers!


Download ppt "Introduction to Java Originally developed by James Gosling at Sun Microsystems In 1991 named “OAK” In 1995, this language was renamed as “Java”. (which."

Similar presentations


Ads by Google