Documente Academic
Documente Profesional
Documente Cultură
Java Data types – Class – Object – I / O Streams – File Handling concepts – Threads –
Applets – Swing Framework – Reflection
Output:
hellojavatpoint
Program
class Student
{
int id;
String name;
public static void main(String args[])
{
Student s1=new Student();//creating an object of Student
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Output:
101
Sonoo
4. I/O Stream
• Stream defined as a sequence of data.
– InPutStream − used to read data from a source.
– OutPutStream − used for writing data to a destination.
1. Byte Stream : for handling input and output of byte(8-bit bytes).
2. Character Stream : for handling input and output of characters(16-bit unicode).
Standard Streams
• Standard Input − keyboard -represented as System.in.
• Standard Output − -computer screen - represented as System.out.
• Standard Error − error data -computer - represented as System.err.
DataOutputStream An output stream that contain method for writing java standard data
type
import java.io.FileOutputStream;
public class Example
{
public static void main(String args[])
{
package com.javatpoint;
import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1)
{
System.out.print((char)i);
}
fin.close();
}
}
Output:
Welcome to javaTpoint
In this example, we are reading the data from the text file testout.txt using Java FileReader
class.
package com.javatpoint;
import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
Here, we are assuming that you have following data in "testout.txt" file:
Welcome to javaTpoint.
Output:
Welcome to javaTpoint.
In this example, we are writing the data in the file testout.txt using Java FileWriter class.
package com.javatpoint;
import java.io.FileWriter;
public class FileWriterExample
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("D:\\testout.txt");
fw.write("Welcome to javaTpoint.");
fw.close();
}
catch(Exception e)
{ System.out.println(e); }
System.out.println("Success...");
}
}
Output:
Success...
testout.txt:
Welcome to javaTpoint.
6.Thread
• Thread is a lightweight sub-process, a smallest unit of processing.
• Multithreading in java is a process of executing multiple threads simultaneously.
• used to achieve multitasking.
• threads share a common memory area.
• used in games, animation etc.
Life cycle of a Thread (Thread States)
• life cycle of the thread in java is controlled by JVM.
• Thread States are
1) New
The thread is in new state if you create an instance of Thread class but before the invocation of
start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but the thread scheduler has not
selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.
thread is running...
Thread Priority
• 3 constants defined in Thread class:
public static int MIN_PRIORITY
public static int NORM_PRIORITY
public static int MAX_PRIORITY
}
public static void main(String args[])
{
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
Output:running thread name is:Thread-0
running thread priority is:10
running thread name is:Thread-1
running thread priority is:1
7. Applet
• Applet is a Java program that can be embedded into a web page.
• It runs inside the web browser and works at client side.
• Applet is embedded in a HTML page using the APPLET or OBJECT tag and hosted on a
web server.
• Applets are used to make the web site more dynamic and entertaining.
• All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
• Applets are not stand-alone programs. Instead, they run within either a web browser or an
applet viewer.
• JDK provides a standard applet viewer tool called applet viewer.
• In general, execution of an applet does not begin at main() method.
• Output of an applet window is not performed by System.out.println(). Rather it is handled
with various AWT methods, such as drawString().
Life cycle of an applet :
• public void init(): is used to initialized the Applet. It is invoked only once.
• public void start(): is invoked after the init() method or browser is maximized. It is used
to start the Applet.
• public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc.
• public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
• public void destroy(): is used to destroy the Applet. It is invoked only once.
Features of Applets over HTML
Swing Classes
• JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout,
FlowLayout.
• JFrame : JFrame is Swing's version of Frame and is descended directly
from Frame class.
• JWindow : This is Swing's version of Window and has descended directly
from Window class.
• JLabel : is used to create text labels.
• JButton : JButton class provides the functioning of push button.
– JButton allows an icon, string or both associated with a button.
• JTextField : JTextFields allow editing of a single line of text.
Creating a JFrame
import javax.swing.*;
public class FirstSwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame();//creating instance of JFrame
As you know well that appletviewer tool creates a frame and displays the output of applet in the
frame.You can also create your frame and display the applet output.
Let's see the simple example that works like appletviewer tool. This example displays applet on
the frame.
import java.applet.Applet;
import java.awt.Frame;
import java.awt.Graphics;
Applet a=(Applet)c.newInstance();
a.start();
Graphics g=v.getGraphics();
a.paint(g);
a.stop();
}
//simple program of applet
import java.applet.Applet;
import java.awt.Graphics;