Professional Documents
Culture Documents
Core Java
Core Java
Core Java
Java is a high level programming language as well as a powerful software platform. Programming Language Software Platform Different editions of java JVM Java Standard Edition Java API Java Enterprise Edition Java Micro Edition
Overview
Object Oriented Concepts. History & Evolution. Features. Data type, variables and array. Operators. Control Statements. Classes. Methods. Inheritance. Access Specifiers Interfaces and Packages Exception Handling Threading File Handling
Features.
Java is a high level programming language. It is a pure object oriented language. Java offers Platform independence Security Multi threading Automatic garbage collection.
Variable
Variable serves as place holders in memory for data. Different type of variables are used to store data in different format. e.g.: int type can store integer values.
Data types
Data types are of two type. 1. Primitive data types 2. Derived data types.
class array
Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created.
Each item in an array is called an element, and each element is accessed by its numerical index.
Arrays
Declaration Syntax <data_type> []<array_name> <data_type> <array_name>[] Initializing at the time of declaration Eg: int marks[]={40,45,34}; Eg: int []marks; int marks[];
Arrays
Operators
Unary Operators Assignment Operators Arithmetic Operators Equality and Relational Operators Conditional Operators Type Comparison Operator
Operators
Unary Operators + Unary Plus Operator(indicates +ve) Unary Minus ++ Increment Operator -Decrement Operator ! Logical Complement Operator
Operators
Assignment Operators = Simple Assignment Operator Arithmetic Operators + Additive Operator (used for String Concatenation) Substraction Operator * Multiplication Operator / Division Operator % Modulus Operator
Operators
Equality and Relational Operators == Equal to != Not Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to
Operators
Control Statements
Statements that control the flow of a program . if-else switch break continue
Loops
Loops are programming constructs which enables repeated execution of a specific code. while do-while for
Access specifier
Access specifiers are used to specify the visibility and accessibility of a class, member variables and methods. Java provides access specifiers such as:
public: can be accessed from anywhere . private: can be accessed only within the same class. protected: This modifier makes a member of the class available to all classes in the same package and all the sub classes of the class.
Methods
Methods are the functions built into a class. Method declaration includes 1)Modifiers such as public, private etc. 2)Return types the data type of the value returned by the method or void if the method does not return a value 3)Method name 4)Parameter list comma delimited list of input parameters, preceded by their data types enclosed by parenthesis 5)An exception list 6)The method body enclosed btw braces
Methods
Method declaration eg public double calculateAmount(int x, int y) { } Method Signature comprises method s name and parameter type Eg calculateAmount(int, int)
Interfaces
In Java interface is nothing but the collection of methods with empty implementations and constant variables ( variables with static and final declarations ). All the methods in an interface are "public and abstract" by default. Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces.
Interfaces
In Java interface is nothing but the collection of methods with empty implementations and constant variables ( variables with static and final declarations ). All the methods in an interface are "public and abstract" by default. Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces.
Packages
In java we use packages to organize related classes. A package contains only classes and interfaces. A package have a hierarchical structure similar to a directory structure.
Creating a package
To create a package put a package statement with a name at the top of source file . Eg package myPackage;
Packages
Using a package
We can use an already created package by importing it. It is possible to import the whole package or a specific member. Syntax import package_name.*; or import package_name.class_name;
Exception Handling
The Java programming language uses exceptions to handle errors and other exceptional events. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Java uses five keywords for exception handling. Try, catch, throw, throws and finally.
Exception Handling
try Program statements that we want to monitor for exceptions are contained within try block. throw to manually throw an exception throws any exception that is thrown out of a method must be Specified by a throws clause finally Any code that absolutely must be executed before a method returns is put in a finally block.
Try block
Catch block
try
Each catch block is an exception handler and handles the type of exception
indicated by its argument.
Throw statement
throw someThrowableObject; All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class.
Error class:- When a dynamic linking failure or other hard failure in the Java virtual
machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.
Exception Class:-An Exception indicates that a problem occurred, but it is not a serious
system problem. The Java platform defines the many descendants of the Exception class. These descendants indicate various types of exceptions that can occur.
Throws statement
modifiers> <return type> <method name>(<parameter list>) throws <list of Throwables> { /* Java statements */ } To specify what exceptions a method throws. Every method that throws exceptions should declare what exceptions it throws in the method declaration. <list of Throwables> is a space-separated list of the class Throwable and its subclasses.
Threading
A thread can be defined as a single sequential flow of control within a program. Threads are sometimes called lightweight processes. Threads share the process's resources, including memory and open files. Each thread is associated with an instance of the class Thread.
Thread
Thread (class)
Runnable (interface)
Run() method
public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
Synchronization of Threads
y Every object in Java code has one lock, which is useful for ensuring that y y y y
only one thread accesses critical code in the object at a time. This synchronization helps prevent the object's state from getting corrupted. If a thread has obtained the lock, no other thread can enter the synchronized code until the lock is released. When the thread holding the lock exits the synchronized code, the lock is released. Now another thread can get the object's lock and execute the synchronized code.
Introduction to Streams
A Stream is a sequence of bytes travelling from a source to a destination over a communication path. When a stream of data is being sent, it is said to be written and when a stream of data is being received it is said to be read. Data in a stream can flow in one direction only. So we use separate streams for reading and writing data. Java offers 1. java.io.InputStream class to read bytes of data from a source(keyboard). 2. java.io.OutputStream class to write bytes of data to a destination.
Node Streams
Input and Output streams that read from or write to a specific location such as a disk file or memory are called a Node Stream. Eg: FileInputStream, FileOutputStream.
Filter Streams
Filter streams are used to read data from one stream and write it to another. Eg: PrintStream
Byte Streams
Byte streams work on eight bits of data.
Character Streams
Streams that work on 16-bit Unicode characters are called character streams. Eg:InputStreamReader,OutputStreamReader
Java Applets
Applets are web based programs that can be downloaded and can execute on a computer. Applets run in a java enabled browser such as Internet Explorer, Netscape navigator etc. These are said to be java enabled because they have a built in java platform(JVM and Java API).
init() Method
is called the first time when an applet is loaded in the memory.
Using this can initialize variables and add components.
start()Method
is called immediately after the init() and every time an applet receives focus. use this method to restart a process.
stop() Method
is called every time an applet loses its focus. can use this method to reset variables and stop the threads that are running.
destroy() Method
Is called when a user moves to another page. Can use this method to perform clean up operations like closing a file.
AWT Components
Buttons Canvas Checkbox Checkbox Group Choice Label List Scrollbar Text Area Text Field
Layout Managers
Are special objects that determine how the components of a container are organized. Java automatically creates and assigns a default layout manager to every container.
Event
An event is an object that describes a state change in a source.
Components of an Event
An event object comprises of three components. 1. Event object An event object contains the information related to the event such as category of the event, component that generated the event, and the time when the event occurred. 2. Event Source 3. Event Handler
Event Listeners
A listener is an object that is notified when an event occurs. It has two major requirements. It must be registered with one or more sources It must implement methods to receive and process the notification The methods that receive and process events are defined in a set of interfaces found in java.awt.event
Event Classes
Java.util.EventObject Java.awt.AWTEvent
Item Event
Adjustment Event
Component Event
Action Event
Text Event
Focus Event
Container Event
Input Event
Window Event
Key Event
Adapter classes
Adapter classes help us in avoiding the implementation of the empty method bodies. Generally an adapter class is there for each listener interface having more than one method. Event package provides seven adapter classes.
Adapter classes
Adapter classes help us in avoiding the implementation of the empty method bodies. Generally an adapter class is there for each listener interface having more than one method. Event package provides seven adapter classes.