Download What is java - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Java
WORA
What is java ?
1.
2.
3.
Object Oriented Programming
WORA( Write Once Run any where)
The Java language was created by James Gosling in June
1991.
4. Java Provides development and deployment environment.
5. It is similar in syntax to C++
6. It is robust provides exception handling , automatic
garbage collection.
7. Designed for the distributed environment. Supports
Multithreading
8. Secure ( No Pointer and Byte Code Verifier)
9. Architectural Neutral - If a company develops new hardware
, it doesn’t have to make a new software , only JRE needs to
be replace for New platform).
10. Portable (WORA ) Write Once Run any-where
11. High Performance (JITc)
Java Edition
Java History
1.
2.
3.
4.
5.
Green Project (1991) was designed for hand-held devices
and set-top boxes. (Requirement was the hardware in the
consumer devices was always changing but the same code
use on the different hardware systems)
It was originally named as Oak but became unsuccessful, so
in 1995, Sun changed its name to JAVA
James Gosling's goals were to implement a virtual machine
and a language that had a familiar C/C++ style of notation.
The first public implementation was Java 1.0 in 1995. It
promised "Write Once, Run Anywhere" (WORA).
Note : The initial version of Java is so small it
fit in a floppy. Now it of 60+MB.
Java Kick Start
. JDK (Java Development Kit)
JDK
= javac compiler + .jar (libraries) + javadoc + jdb + JRE
JDK – JDK is a software package which is used to develop and
deploy java applications.
JDK consists
a) javac compiler – it is use to convert .java file into .class
file
b) JAR Files - A jar files are java API’s , which contains
number of classes and interfaces. For Example to make a
database connection with oracle we require ojdbc.jar.
c) Javadoc – through this utility we can make Java
documentaion for any java program.
d) Jdb- jdb is a java debugger.
e) JRE – See in the next slide
JAVA KICK START continue…
2. JRE (Java Runtime Environment)- A JRE is required to run Java
applications interpretively on a target hardware and operating
system platform.
JRE=JVM (Java Virtual machine)+ jar files (libraries)
A JVM performs the following tasks.
1.
2.
3.
4.
Class Loader Load all the .class file, necessary for the
execution of a program.
Byte code Verifier It ensures the code does not no
illegal data conversion, no stack over flow, not violate
system integrity.
JIT – (Just In time compiler) JIT work as a cache for the
JVM interpreted code. It memorized the repeated code
instructions (that is interpreted by the JVM) and when
JVM reqiure the same set of instructions so JIT provides
these instructions to the JVM , so JVM does not interpret
the same instructions again
Generate Native Code- The JVM and the Java API are
built for every O/S platform. The Java API calls to the O/S
API.
Java Class loader
1.
2.
Class Loader- The Java Classloader is a part of the Java
Runtime Environment that dynamically loads Java classes
into the Java Virtual Machine.
A software library is a collection of more or less related
object code. In the Java language, libraries are typically
packaged in Jar files. Libraries can contain objects of
different types. The most important type of object
contained in a Jar file is a Java class. A class can be
thought of as a named unit of code. The class loader is
responsible for locating libraries, reading their contents,
and loading the classes contained within the libraries.
This loading is typically done "on demand", in that it
does not occur until the class is actually used by the
program. A class with a given name can only be loaded
once by a given classloader
There are 3 types of Java Class Loaders
JVM
1.
2.
3.
JVM is a software that is plat form specific. For example for
Linux platform
You require a Linux based JVM , for windows platform you
require a windows based JVM.
Now in these days JVM is bundled with Internet explorer
and With other types of browsers.
When the JVM is started, three class loaders are used
a) Bootstrap class loader
b) Extensions class loader
c) System class loader
The bootstrap class loader
The bootstrap class loader loads the core Java libraries
(<JAVA_HOME>/lib directory). This class loader, which is part of
the core JVM, is written in native code
The extensions class loader
extensions directories (<JAVA_HOME>/lib/ext or any other directory
specified by the java.ext.dirs system property). It is implemented by
the sun.misc.Launcher$ExtClassLoader class.
The system class loader
The system class loader loads code found on java.class.path, which
maps to the system CLASSPATH variable. This is implemented by the
sun.misc.Launcher$AppClassLoader class
How Java Works
Writing First Java Program
Step -1 Write Source Code
Example:
class A
{
public static void main(String args[])
{
System.out.println(“Hello Java ! “);
}
}
Step- 2 Save Code with class Name
A.java
Step-3 Compile the code
javac A.java ( when you compile the code it will generate an
intermediate code (Byte code) it is not a machine code.
Step-4 run the Program
java A ( running the program by starting the JVM with the A.class.
The JVM translates the bytecode into something the underlying
platform understands , and runs your program.
Program Observation
When the JVM starts running , it looks for the class that is at the
command line. Then it starts looking for a specially written
method that looks exactly like:
public static void main(String args[])
{
//Your Code Here
}
Note: Every Java application has to have at least one class, and at
least one main method.
You can define main method in the following ways
a) public static void main(String args[])
b) static public void main(String args[])
c) public static void main(String …a) // Allowed in JDK 1.5
or higher
d) static public void main(String …a) //Allowed in jdk1.5
or higher
e) The main method is public , because java is package
centric language.
f) Now JVM has to access your main method from outside
your package.
g) That’s why it set to as public
h) The main method also static , because static is loaded on
the compile
i) Time and not required to instantiate.
j) The main method is void , because it is not required to
return any thing
k) to the JVM.
l) Main is the name of the method
m) String args[] is used for command line arguments , where
String is a Java class.
System.out.println(“Hello Java ! ”);
Where System is a final class, found in java.lang package.
Out is a object of PrintStream class and it declare static in the
System
Class.
Println(String msg) this is a method define in the PrintStream
class.
Used to print on console and print in new line
Identifier
It is a name given to a class, variable, constant or method.
Rules for Declaring a Legal Identifier:
a) Identifier must start with a letter, $, _. Cannot start with
number.
b) After the first letter, contain numeric.
c) No Limit to the number of characters an identifier can
contain.
d) Cannot use keyword in identifier name.
e) Identifiers are case-sensitive.
Variable
A variable is just like a cup or container. It can hold
something. It has a size and a type.
Primitive Type Example
byte x=10; // Take 1 byte in memory
short y=500; // Take 2 bytes in memory
int a=40000; // Take 4 bytes in memory
Constant
As the name implies , constants are fixed values which never
change.
Example:
final int MAX=100;
“final” keyword is used to declare a constant in java.
Keywords
Keywords are reserved which has some special meaning for
the compiler.
For Example : if , while ,int etc.
Java Keywords
Primitive Data Types List
In Java, primitives come in different sizes with different names.
When you declare a variable in Java, you must declare with a
specific type.
Reference type
Data types
Variable (Cup) can be of two type
✔ Primitive Type
✔ Reference Type
Primitive Type – Primitives are like the cups at the CoffeeHouse They come in different sizes and each has name like
small , big, medium.
A Reference variable holds bits that represents a way to access an
object.
It doesn't hold the object itself, but it holds something like a
pointer, or an address.
Using == with primitive type
Example
int a=100;
int b=100;
System.out.println(a==b); //print true
Using == with reference type
Example:
String a=new String(“Hello”);
String b=new String(“Hello”);
System.out.println(a==b); // false But Why ???
Answer is, The reference type checks address in this case , it
return true if both
address or references are same.
So HOW I COMPARE VALUES IN REFERENCE TYPE, BECAUSE ==
CHECKS
ADDRESS NOT VALUE?
Answer is using equals()
System.out.println(a.equals(b)); //return true