Why Java? Java is easier to learn compared to other object oriented programming languages especially c++.It is platform independent so a java program can be written once and then run on many different devices. JVM Java Virtual Machine is designed by Sun Microsystems to find methods. It is platform dependent. In Java programs JVM finds the “main method” to start execution. In Applet programs JVM finds the “Run() method” to start execution. In servlet programs JVM finds the “Service() method” to start execution. Note: Runtime Environment for java programs is JVM JRE Designing and Implementing JVM is called as Java Runtime Environment (JRE). JDK – Java Developer Kit SDK – Standard Developer Kit To remember: Compiled Java code is referred as bytecode We are not writing java programs for windows, Unix etc. we write java programs only to run on a JVM. Most JVMs are written in C or C++. When you run the java programs, you actually running a JVM and the JVM is interpreting your java code.
Java Editions Sun grouped their major java programming technologies in to three editions. They are 1. J2ME : Java 2 Platform, Micro Edition. – For Electronic Devices 2. J2SE : Java 2 Platform, Standard Edition. – For Core Java 3. J2EE : Java 2 Platform, Enterprise Edition. – Collection of Java Technologies. Core Java Features: 1. 2. 3. 4. Platform Independent. Simple Automatic Memory Management Secured
5. Dynamic 6. Multithreaded 7. Pure OOPS language 1. Platform Independent Write once, run anywhere. The byte code is platform independent hence java is platform independent. 2. Simple No pointers in java like c, c++. It contains many inbuilt APIs 3. Automatic Memory Management In languages without automatic memory management, the programmer must reserve memory when it is required and mark it as free once its contents no longer have effect in the running program. For example, in the C programming language this is achieved by using the functions malloc, calloc, realloc, alloca and free. On the other hand, languages with automatic memory management like java automatically reserve memory when it is required for storing new values and reclaim it (free it) when their contents can no longer affect future computations. It is done by garbage collection in java using System.gc() method. Like Mark and Sweep method. 4. Secure 5. Dynamic It loads the class as and when it is required. 6. Multithreaded 7. Pure OOPS language Without class and object nothing can do in java. Hence it is called as Pure OOPS language.
A Simple Java Program public class Vijay { public static void main (String ar[]) { System.out.println(“Welcome”); }
}
Note: 1. It is necessary to use semicolon to denote the end of all statements in Java. 2. Only one public class should be there in a .java file. The name of the .java files must be same as the name of the public class file. Javac – Java Compiler. It’s a tool which compiles java classes into bytecodes. Java – Java Interpreter. Runs the bytecode Public – The method can be accessible globally / outside the class/package. Static – you need not have an instance of the class to access the method as we are declaring this as static Void – the method does not return any value. Main () – the program starts from this method.
Class and Objects: Class: Objects: Basic runtime entities. It’s a Instance of the class While executing it communicates with the methods and other things in the class. It is an object belongs to user defined data type Class. It is defined using new keyword. Two major components of objects are Attributes and Behaviors. Attributes and Behaviors are referred as fields and methods. Note: Object class is the super class for all classes. Fundamental building blocks of a java program. Blue print of a structure. Skeleton of java programs. It is a User Defined Data type.
OOPs Concept Basic diagram of OOPs concept Package Subclass Inside Outside Package Outside subclass
Public Protected Private default
Types of OOPs concept: 1. Encapsulation 2. Abstraction 3. Polymorphism 4. Inheritance Encapsulation: Wrapping up of data into single unit is called Encapsulation. It is nothing but data hiding. Abstraction: Taking all the common features and binding into the single class. Polymorphism/ Late Binding: Single class act in different ways. Taking more than one form. Method overloading and Method overriding is the polymorphism concepts. Method overloading: Method which contains same name, functions with different signature is called method overloading. Method overriding: Method which contains same name and signature with different functions is called method overriding. Inheritance: The objects of properties of subclass acquires the objects of properties of superclass is called inheritance.
Constructor: It contains the same name as class name. It does not contain return type because it never returns a value. It initializes itself whenever an object is created. The two main keywords using in constructor is this () and super (). If you want to call another constructor in the same class means, you have to use this () keyword. If you want to call the super class constructor from the sub class means, you have to use super () keyword in the first statement itself. Note: Constructor can be over loaded but it cant be override. Abstract class: 1. It can have both abstract and non abstract methods 2. The method which doesn’t have body part is called abstract method. 3. If any method is declared as abstract means the class should declared as abstract. Interface: 1. All the methods in the interface should be abstract. 2. All the variable should be public static final. 3. It can be used in the class using implements keyword. Note: Abstract and static cannot come together. Java doesn’t support multiple interface, it only supports multilevel interface.
EXCEPTION HANDLING: A type of error handling that allows control and information to be passed to an exception handler when an exception occurs. An exception is an abnormal behavior existing during a normal execution of a program. There are two types of exceptions: Checked: those handled by the compiler, and therefore, not a concern of the programmer. Checked exceptions are derived from the Error class. Unchecked: those that the programmer must handle. Unchecked exceptions inherit from the RuntimeException class. Often, unchecked exceptions result from errors in source code that the programmer should not have allowed anyhow.