Java_Note_3
Java_Note_3
Introduction to Java
Shivneet Tripathi
Department of Computer Application
UIET , CSJM University,
Kanpur
String
• String is a class not a data type in java.There are two
ways to create a string object.
• 1- Implicit
• 2 – Explicit
• Implicit– When you use a string literal , like “Hello
world” , java automatically create a string object .
• Ex - String s = “Hello world”;
• Explicit - When you use the new operator to initiate a
string object.
• Ex String s = new String (“Hello world”);
Example
• Import java.lang.*;
• public class String1{
public static void main(String args[]){
String s1="java";//creating string by java string litera
l
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to s
tring
String s3=new String("example");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
Package
• A java package is a group of similar types of
classes, interfaces and sub-packages.
• Package in java can be categorized in two form,
built-in package and user-defined package.
• There are many built-in packages such as java,
lang, awt, javax, swing, net, io, util, sql etc.
Advantage of Java Package
• 1) Java package is used to categorize the classes
and interfaces so that they can be easily
maintained.
• 2) Java package provides access protection.
• 3) Java package removes naming collision.
Example
• package pack;
• public class A{
• public void msg(){System.out.println("Hello");}
• }
• import pack.*;
• class B{
• public static void main(String args[]){
• A obj = new A();
• obj.msg();
• }
• }
Java Applet
• Applets are small Java programs that are embedded in
Web pages.
• They can be transported over the Internet from one
computer (web server) to another (client computers).
• They transform web into rich media and support the
delivery of applications via the Internet.
• Advantage of Applet
• There are many advantages of applet. They are as
follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many
plateforms, including Linux, Windows, Mac Os etc.
How Applets Differ from
Applications
• Although both the Applets and stand-alone applications are
Java programs, there are certain restrictions are imposed on
Applets due to security concerns:
– Applets don’t use the main() method, but when they are load,
automatically call certain methods (init, start, paint, stop, destroy).
– They are embedded inside a web page and executed in browsers.
– They cannot read from or write to the files on local computer.
– They cannot communicate with other servers on the network.
– They cannot run any programs from the local computer.
– They are restricted from using libraries from other languages.
• The above restrictions ensures that an Applet cannot do any
damage to the local system.
Lifecycle of Java Applet
init()
Begin Bo
rn
stop()
start()
Ru
Idl
nni
e
ng
destroy()
paint() start()
De
ad End
Applet Life Cycle
• Every applet inherits a set of default
behaviours from the Applet class. As a result,
when an applet is loaded, it undergoes a
series of changes in its state. The applet
states include:
– Initialisation – invokes init()
– Running – invokes start()
– Display – invokes paint()
– Idle – invokes stop()
– Dead/Destroyed State – invokes destroy()
Applet States
• Initialisation – invokes init() – only once
– Invoked when applet is first loaded.
• Running – invokes start() – more than once
– For the first time, it is called automatically by the system after init() method
execution.
– It is also invoked when applet moves from idle/stop() state to active state.
For example, when we return back to the Web page after temporary
visiting other pages.
• Display – invokes paint() - more than once
– It happens immediately after the applet enters into the running state. It is
responsible for displaying output.
• Idle – invokes stop() - more than once
– It is invoked when the applet is stopped from running. For example, it
occurs when we leave a web page.
• Dead/Destroyed State – invokes destroy() - only once
– This occurs automatically by invoking destroy() method when we quite the
browser.
Building Applet Code: An Example
//HelloWorldApplet.java
import java.applet.Applet;
import java.awt.*;