Java
Java
Java
JAVA
• Java is a high-level programming language originally developed by
Sun Microsystems and released in 1995.
• Java runs on a variety of platforms, such as Windows, Mac OS, and
the various versions of UNIX.
• Java is developed by an author called "james gosling"
• Oops concept is a bottom up approach. where the object is created
from there the program execution starts.
• There was a need for a programming language that would run on various
devices.
• Java (first named Oak) was developed for this purpose.
• Java enabled web browser (HotJava) demonstrated at 1995 Sun World
conference.
• Java incorporated into Netscape shortly after.
• Java is “cross platform”, meaning that it can run on various computer
operating systems.
Java Applications and Applets
• Java programs can be of two types:
• Applications
Stand-alone programs that run without the aid of a web browser.
Relaxed security model since the user runs the program locally.
• Applets
Small applications that require the use of a Java enabled web browser to run.
Enhanced security model since the user merely goes to a web page and the applet runs itself.
Programming Languages
• A program is a set of instructions a computer follows in order
to perform a task.
• A programming language is a special language used to write
computer programs.
• A computer program is a set of instructions that enable the
computer to solve a problem or perform a task.
• Collectively, these instructions form an algorithm
The Compiler and the Java Virtual Machine
• A programmer writes Java programming statements for a program.
• These statements are known as source code.
• A text editor is used to edit and save a Java source code file.
• Source code files have a .java file extension.
• A compiler is a program that translates source code into an executable
form.
• A compiler is run using a source code file as input.
• Syntax errors that may be in the program will be discovered during
compilation.
• Syntax errors are mistakes that the programmer has made that violate
the rules of the programming language.
• The compiler creates another file that holds the translated instructions.
• Most compilers translate source code into executable files containing
machine code.
• The Java compiler translates a Java source file into a file that contains
byte code instructions.
• Byte code instructions are the machine language of the Java Virtual
Machine (JVM) and cannot be directly executed directly by the CPU.
• Byte code files end with the .class file extension.
• The JVM is a program that emulates a micro-processor.
• The JVM executes instructions as they are read.
• JVM is often called an interpreter.
• Java is often referred to as an interpreted language.
JIT(just in Time Compiler)
Portability
• Portable means that a program may be written on one type of
computer and then run on a wide variety of computers, with little or no
modification.
• Java byte code runs on the JVM and not on any particular CPU;
therefore, compiled Java programs are highly portable.
• JVMs exist on many platforms:
• With most programming languages, portability is achieved by
compiling a program for each CPU it will run on.
• Java provides an JVM for each platform so that programmers do not
have to recompile for different platforms.
Java Versions
• The software you use to write Java programs is called the Java
Development Kit, or JDK.
• There are different editions of the JDK:
1. Java SE - Java2 Standard Edition.
2. Java EE - Java2 Enterprise Edition.
3. Java ME - Java2 Micro Edition
PRIME CONCEPTS OF OOPS:
• Data Hiding
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
JAVA OFFERS THE FOLLOWING
FEATURES
• Java Is A Pure Object Oriented Programming Language
• Java Is A Compiled And Interpreted Programming Language
• Java Is A Platform Independent Programming Language
• Java Is A Multithreaded Programming Language
• Java Is Used In Internet, Web, wap Applications.
JAVA DOES NOT SUPPORT
FOLLOWING FEATURES
• Pointers
• Multiple Inheritance
• Operator Overloading
• Scope Resolution Operator
Variables
•In this example, the variable hours is created as an integer (more on this later) and
• DATA TYPE:
It Is Used To Represent What Type Of Data That The User Is Going
To Enter.
int, float, char, double, String, boolean, byte
OPERATORS:
• Arithemetic
• Relational
• Logical
• Assignment
• Increment And Decrement
• Ternary Or Conditional
• Bitwise ----------> & , | , ^ , << , >>
• Comma
TERNARY OPERTOR:
CASE 1
If(condition)
Statement;
CASE 2:
If(condition)
Statement;
Else
Statement;
CASE 3:
IF(CONDITION)
STATEMENT;
ELSEIF CONDITION
STATEMENT;
ELSE
STATEMENT;
SWITCH CASE STATEMENT:
1. for loop
2. while loop
3. do while loop
for loop
• It Is Used To Execute The Statements For A Fixed Number Of Times.
SYNTAX:
for(initialization;condition;updation)
• String literals in Java are specified like they are in most other
languages by enclosing a sequence of characters between a pair of
double quotes. Examples of string literals are:
Java Access Modifiers:
• Java provides a number of access modifiers to set access levels for
classes, variables, methods and constructors. The four access levels
are:
1. Visible to the package. the default. No modifiers are needed.
2. Visible to the class only (private).
3. Visible to the world (public).
4. Visible to the package and all subclasses (protected).
private:
• The Members (Data Member Or Function) Declared In This Category
Cannot Be Accessed Outside The Class.
protected:
• The Members Declared In This Category Can Be Accessed Any Where
With In The Package( To Any Classes With In The Package)
public:
• The Members Declared In This Category Can Be Accessed Any Where
With In The Package And Also Available Outside The Package Too
The break Keyword
• The break keyword is used to stop the entire loop. The break keyword
must be used inside any loop or a switch statement. The break
keyword will stop the execution of the innermost loop and start
executing the next line of code after the block.
The continue Keyword
• The continue keyword can be used in any of the loop control
structures. It causes the loop to immediately jump to the next iteration
of the loop.
The Three OOP Principles
• All object-oriented programming languages provide mechanisms that help you
implement the object-oriented model. They are encapsulation, inheritance, and
polymorphism
• Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse
• Inheritance is the process by which one object acquires the properties of another
object.
• Polymorphism (from Greek, meaning “many forms”) is a feature that allows one
interface to be used for a general class of actions
POLY MEANS MANY AND MORPHISM MEANS FORMS.
AN OBJECT CAN TAKE MANY OR MULTIPLE FORMS.
class
• Class Is An User Defined Data Type, Which Contains Data Members
And Functions.
• Class Is A Template Or Blueprint Or General Represenation Or
Common Representation Or Skeleton Of Application
• Here is a class called Box that defines three instance variables: width,
height, and depth. Currently, Box does not contain any methods
object
• It is important to remember that a class declaration only creates a
template; it does not create an actual object.
• Thus, the preceding code does not cause any objects of type Box to
come into existence.
• To actually create a Box object, you will use a statement like the
following:
Box mybox = new Box(); // create a Box object called mybox
CONSTRUCTOR
• It is a special method which is called automatically as soon as the
object or instance is created.
• It is used to initialize the data members of the class and allocate the
memory.
• A constructor initializes an object immediately upon creation.
• It has the same name as the class in which it resides and is
syntactically similar to a method.
• Once defined, the constructor is automatically called immediately after
the object is created, before the new operator completes
Constructors
Garbage Collection
• Since objects are dynamically allocated by using the new operator, you
might be wondering how such objects are destroyed and their memory
released for later reallocation.
• In some languages, such as C++, dynamically allocated objects must
be manually released by use of a delete operator.
• Java takes a different approach; it handles deallocation for you
automatically. The technique that accomplishes this is called garbage
collection
The finalize( ) Method
• By using finalization, you can define specific actions that will occur
when an object is just about to be reclaimed by the garbage collector
Recursion
• Java supports recursion.
• Recursion is the process of defining something in terms of itself.
• As it relates to Java programming, recursion is the attribute that
allows a method to call itself.
• A method that calls itself is said to be recursive
static
• There will be times when you will want to define a class member that
will be used independently of any object of that class.
• To create such a member, precede its declaration with the keyword
static.
• When a member is declared static, it can be accessed before any
objects of its class are created, and without reference to any object
• You can declare both methods and variables to be static
final
• A variable can be declared as final. Doing so prevents its contents
from being modified
• final int FILE_NEW = 1;
• Final used for method
final class A
{ // ...
}
// The following class is illegal.
class B extends A
{
// ERROR! Can't subclass A // ...
}
As the comments imply, it is illegal for B to inherit A since A is declared as final.
Inheritance
• To inherit a class, you simply incorporate the definition of one class
into another by using the extends keyword
TYPES OF INHERITANCE
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Hierarchical Inheritance
Why Java Does Not Support Multiple Inheritance:
(Extending More Than One Classes)
• When A Class Extending More Than One Classes, The Object Of The
Sub Class Has To Check The Signatures (Statements) Of Both Super
And Sub Classes , This Increases The Compilation Time.
Abstract Classes
• There are situations in which you will want to define a superclass that
declares the structure of a given abstraction without providing a
complete implementation of every method.
Interfaces
• An interface is defined much like a class
• Interfaces add most of the functionality that is required for many
applications that would normally resort to using multiple inheritance
in a language such as C++.
• it is basically a kind of class, which contains abstract methods
(declaration of the method) and final data fields.
• it uses the keyword "interface"
COMMAND LINE ARGUMENTS:
• THE COMMAND LINE ARGUMENTS ARE THOSE WHICH ARE ENTERED AT
THE TIME OF EXECUTION.
• THE VALUES ARE IN THE FORM OF STRING.
class pro
{
public static void main(String v[ ])
{
String x=v[0];
System.out.println(x);
}
}
javac pro.java
java pro abc
or
java pro 150
PACKAGE
• It is a container of classes and interfaces which are stored some where
else in some other directory.
• Package can be imported by using the keyword "import"
• Package is nothing but a directory.
• The standard built-in packages provided by the java are
util, io, net, awt,applet, sql package
• import java.util.*;
1. What is JAVA?
2. What are the features in JAVA?
3. How does Java enable high performance?
4. Name the Java IDE’s?
5. What do you mean by Constructor?
6. What is meant by the Local variable and the Instance variable?
7. What is a Class?
8. What is an Object?
9. What are the OOPs concepts?
10. What is Inheritance?
11. Give an example of Pointers use in Java class.
1. What is Encapsulation?
2. What is Polymorphism?
3. What is meant by Method Overriding?
4. What is meant by Overloading?
5. What is meant by Interface?
6. What is meant by Abstract class?
7. What are the different access specifiers for Java classes?
8. Explain about Public and Private access specifiers.
9. Difference between Default and Protected access specifiers.
10. What is meant by Exception?
11. What are the types of Exceptions?
1. What is the purpose of static variables and static methods?
2. What is called Loops in Java? What are the three types of loops?
3. What is a singleton class? Give an example
4. Explain the difference between break and continue statement?
5. What is known as infinite loop?
6. Explain the difference between float and double variables in Java?
7. What is known as the base class in Java from which all the classes are derived?
8. Does main() method in java return any data?
9. Are we able to declare a class as Abstract class without having any abstract method?
10. How will we pass an argument to a function through reference rather than pass
value?
11.How can inheritance be restricted to a class so that no class can be inherited from it?
1. Can we state the main method of our class as private?
2. Will a class contain multiple constructors?
3. When is the class constructor invoked?
4. Can default constructor of a class be used even if an explicit constructor is defined?
5. Is it possible for us to override static methods of a class?
6. How can we implement any code before the main method?
7. Will a class be a superclass and a sub-class at the same time?
8. How have the objects of a class been created if no constructor is defined in the class?
9. Can the constructor of a class be called more than once for an object?
10. Can a class have two methods with the same name?
11. What is the advantage of using inheritance?
1. What are the different ways to handle exceptions?
2. What are the advantages of Exception handling?
3. What are the Exception handling keywords in Java?
4. What is the final keyword in Java?
5. What is a Thread?
6. How do you make a thread in Java?
7. What is Multi-threading?
8. Explain the thread life cycle in Java.
9. What is Synchronization?
10. What is the disadvantage of Synchronization?
11. Can goto be used in Java to go to a particular line?
1. Can a class in Java be inherited from more than one class?
2. Can a constructor contain different name than a Class name in Java?
3. Is it possible if a person declares that he successfully compiled a java class without even
containing a main method in it?
4. Is JDK needed on each machine to run a Java program?
5. Is it possible to declare a method in Java class but give it’s implementation in another language
code like C?
6. Can a variable be static and local at the same time?
7. Can an interface contain static methods?
8. How the destructors are defined in Java?
9. Can we change the value of any variable defined in the interface in a class implementing an
interface?
10. Is it correct to say that a Java program never goes out of memory due to garbage collection
feature in Java?
11. Can we use any other return type rather using void for main method?