Monday 6 November 2017

Data Conversion in java

Data Conversion

In java programming we have six data conversion technique they are:.

1 Converting numeric string type data into numerical / fundamental type values

In order to convert numerical string into numerical or fundamental values we use the following generalized predefined method which is present in wrapper classes.
Data Conversion
Here xxx represent any fundamental data type.

Example

 
String  s1="100";
int  x=Integer.parseInt(s1);

Example

 
String  s2="100.75f";
Float  y=Float.parseFloat(s2);

2 Converting numeric / fundamental type values into string type values

In order to convert numeric / fundamental type values into string values, we use the following predefined static overloaded method.
Data Conversion
Here XXX represent any fundamental data type values

Example

int a=10;
Data Conversion

3 Converting fundamental type values into object type values:

In order to convert the fundamental data into equivalent wrapper class object type data we use the following generalized predefined parameterized constructor by taking fundamental data type as a parameter.
Example:
Data Conversion
in JDK 1.4 converting fundamental data type values into wrapper class object is known as boxing. In the case of JDK 1.5 and in higher version it is optional to the java programmer to convert fundamental data type value into equivalent wrapper class object. That is implicitly taken care by JVM and it is known as auto boxing.

Definition of auto boxing

The process of implicitly converting fundamental type value into equivalent wrapper class object is known as auto boxing.

4 Converting object type value into fundamental type value:

In order to convert wrapper class object data into fundamental type data, we use the following predefined instance method present in each and every wrapper class.
Data Conversion
In case of JDK 1.5 and in higher version it is optional to the java programmer to convert object data into fundamental type data and this process is known as auto un-boxing and its takes care by JVM.

Definition of auto un-boxing

In process of implicitly conversion objects type data into fundamental type data is known as auto un-boxing.

5 Converting String type data into object type data

In order to convert String type numeric data into equivalent wrapper class object, we use the following predefined parameterized constructor by each and every wrapper class except character class.
Data Conversion

6 Converting wrapper class object type data into String type data

Data Conversion
To convert wrapper class object type data into string type data we use the following generalized predefined instant method which is present each and every wrapper class.
Data Conversion
Example
int a=10;
  • String is=String.valueOf(a);
  • Integer io=new Integer(is);
  • int x=io.intValue();
  • Integer io=new Integer(n);
  • String so=io.toString();
  • int x=Integer.parseInt(so);

Socket Programming in Java

Socket Programming in Java

Networking is a concept of connecting two or more computing devices together so that we can share resources like printer, scanner, memory.
In Networking application mainly two programs are running one is Client program and another is Server program. In Core java Client program can be design using Socket class and Server program can be design using ServerSocket class.
Both Socket and ServerSocket classes are predefined in java.net package

Advantage of Network Programming

The main advantage of network Programming is sharing of data and resources, some more advantages are;
  • Sharing resources like printer, Scanner.
  • Centralize software management, Software install on only one system and used in multiple system.
  • Sharing of data due to this reduce redundancy of application.
  • Burden on the developer can be reduced.
  • Wastage of memory can be reduced because no need to install same application on every system.
  • Time consuming process to develop application is reduced.

Terms used in Socket Programming

Port number: It is unique identification value represents residing position of a server in the computer. It is four digit +ve number.
Port Name: It is a valid user defined name to know about client system, the default port name for any local computer is localhost.. Port name should be the some value which is given at Server programming.

Socket class

Socket class are used for design a client program, it have some constructor and methods which are used in designing client program.
Constructor: Socket class is having a constructor through this Client program can request to server to get connection.

Syntax to call Socket() Constructor

Socket s=new Socket("localhost", 8080);
// localhost -- port name and 8080 -- port number
Note: If given port name is invalid then UnknownHostException will be raised.
Method of Socket class
  • public InputStream getInputStream()
  • public OutputStream getOutputStream()
  • public synchronized void close()

getInputStream()

This method take the permission to write the data from client program to server program and server program to client program which returns OutputStream class object.

Syntax

Socket s=new Socket("localhost", 8080);
OutputStream os=new s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);

getOutputStream()

This method is used to take the permission to read data from client system by the server or from the server system by the client which returns InputStream class object.

Syntax

Socket s=new Socket("localhost", 8080);
InputStream is=new s.getInputStream();
DataInputStream dis=new DataInputStream(is);

close()

This method is used to request for closing or terminating an object of socket class or it is used to close client request.

Syntax

Socket s=new Socket("localhost", 8080);
s.close();

ServerSocket class

The ServerSocket class can be used to create a server socket. ServerSocket object is used to establish the communication with clients.
ServerSocket class are used for design a server program, it have some constructor and methods which are used in designing server program.
Constructor: ServerSocket class contain a constructor used to create a separate port number to run the server program.

Syntax to call ServerSocket() Constructor

ServerSocket ss=new ServerSocket(8080);
// 8080 -- port number
Method of ServerSocket class
  • public Socket accept()
  • public InputStream getInputStream()
  • public OutputStream getOutputStream()
  • public synchronized void close()
accept(): Used to accept the client request it returns class reference.

Syntax

Socket s=new Socket("localhost", 8080);
ServerSocket ss=new ServerSocket(8080);
Socket s=ss.accept();

Rules to design server program

  • Every server program should run accepted port number (any 4 digit +ve numeric value) It can set by relating an object for server socket class (In any used defined java program).

Syntax

ServerSocket ss=new ServerSocket(8080);
  • Accept client request.
  • Read input data from client using InputStream class.
  • Perform valid business logic operation
  • Send response (writing output data) back to client using OutputStream class.
  • close or terminate client request.

Rules to design client program

  • Obtain connection to the server from the client program (any user defined class) by passing port number and port name in the socket class.

Syntax

Socket s=new Socket(8080, "localhost");
// 8080 is port number and localhost is port name
  • Send request (writing input data) to the server using OutputStream class.
  • Read output data from the server using InputStream class.
  • Display output data
Note: close the connection is optional.
networking

Server Code

// Saved by Server.java

import java.net.*;
import java.io.*;

class Server
{
public static void main(String[] args) 
{ 
try
{
int pno=Integer.parseInt(args[0]);
ServerSocket ss=new ServerSocket(pno);
System.out.println("server is ready to accept clint request");
Socket s1=ss.accept();
InputStream is=s1.getInputStream();
DataInputStream dis=new DataInputStream(is);
int n=dis.readInt();
System.out.println("Value from client : "+n);
int res=n*n;
OutputStream os=s1.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(res);
s1.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}

Client Code

// Saved by Client.java

import java.net.*;
import java.io.*;
import java.util.*;

class Client
{
public static void main(String[] args) 
{
try
{
 String pname=args[0];
 int pno=Integer.parseInt(args[1]);
 Socket s=new Socket(pname,pno);
 System.out.println("clint obtailed connection from server");
 System.out.println("Enter a number ");
 Scanner sn=new Scanner(System.in);
 int data=sn.nextInt();
 OutputStream os=s.getOutputStream();
 DataOutputStream dos=new DataOutputStream(os);
 dos.writeInt(data);
 InputStream is=s.getInputStream();
 DataInputStream dis=new DataInputStream(is);
 int res=dis.readInt();
 System.out.println("Result from server : "+res);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Download Code Client Server code

Steps to run above program

  • First open two command prompt window separately.
  • Compile Client program on one command prompt and compile server program on other command prompt.
  • First run Server program and pass valid port number.
  • Second run client program and pass valid port name and post number (which is already passed at server).
  • Finally give the request to server from client (from client command prompt).
  • Now you can get response from server.

Compile and run server program

server

Compile and run client program

client

Limitation of J2SE network programming

  • Using this concept you can share resource locally but not globally.
  • Using this concept you can not develop internet based application.
  • Using this concept you can develop only half-duplex application.
  • Using this concept you can not get service from universal protocols like http, ftp etc...
  • Using this concept you can not get services from universal server software like tomcat, weblogic etc..
To overcome all these limitation use Servlet and JSP technology.

String Handling in Java

String Handling in Java

The basic aim of String Handling concept is storing the string data in the main memory (RAM), manipulating the data of the String, retrieving the part of the String etc. String Handling provides a lot of concepts that can be performed on a string such as concatenation of string, comparison of string, find sub string etc.

Character

It is an identifier enclosed within single quotes (' ').
Example: 'A', '$', 'p'

String:

String is a sequence of characters enclosed within double quotes (" ") is known as String.
Example: "Java Programming".
In java programming to store the character data we have a fundamental datatype called char. Similarly to store the string data and to perform various operation on String data, we have three predefined classes they are:
  • String
  • StringBuffer
  • StringBuilder

Tuesday 31 October 2017

Boxing and Unboxingin Java

Boxing and Unboxingin Java

Definition of Auto Boxing

The process of implicitly converting fundamental type values into the equivalent wrapper class object is known as auto boxing.

Converting fundamental type values into object type values:

In order to convert the fundamental data into equivalent wrapper class object type data we use the following generalized predefined parameterized constructor by taking fundamental data type as a parameter.
Example:
Data Conversion
In JDK 1.4 converting fundamental data type values into wrapper class object is known as boxing. In the case of JDK 1.5 and in higher version it is optional to the Java programmer to convert the fundamental data type value into the equivalent wrapper class object. That is implicitly taken care by JVM and it is known as auto boxing.

Definition of auto Unboxing.

In process of implicitly conversion objects type data into fundamental type data is known as auto un-boxing.

Converting object type value into fundamental type value:

In order to convert wrapper class object data into fundamental type data, we use the following predefined instance method present in each and every wrapper class.
Data Conversion
In case of JDK 1.5 and in higher version it is optional to the Java programmer to convert object data into fundamental type data and this process is known as auto un-boxing and it takes care by the JVM.

Abstract class in Java

Abstract class in Java

We know that every Java program must start with a concept of class that is without the class concept there is no Java program perfect.
In Java programming we have two types of classes they are
  • Concrete class
  • Abstract class

Concrete class in Java

A concrete class is one which is containing fully defined methods or implemented method.

Example

class  Helloworld
{
void  display()
{
System.out.println("Good Morning........");
}
}
Here Helloworld class is containing a defined method and object can be created directly.

Create an object

Helloworld obj=new  Helloworld();
obj.display();
Every concrete class has specific features and these classes are used for specific requirement, but not for common requirement.
If we use concrete classes for fulfill common requirements than such application will get the following limitations.
  • Application will take more amount of memory space (main memory).
  • Application execution time is more.
  • Application performance is decreased.
To overcome above limitation you can use abstract class.

Abstract class in Java

A class that is declared with abstract keyword, is known as abstract class. An abstract class is one which is containing some defined method and some undefined method. In java programming undefined methods are known as un-Implemented, or abstract method.
Abstract meaning

Syntax

abstract class className
{
......
}

Example

abstract class A
{
.....
}
If any class have any abstract method then that class become an abstract class.

Example

class Vachile 
{
abstract void Bike(); 
}
Class Vachile is become an abstract class because it have abstract Bike() method.

Make class as abstract class

To make the class as abstract class, whose definition must be preceded by a abstract keyword.

Example

abstract class Vachile
{
......
}

Abstract method

An abstract method is one which contains only declaration or prototype but it never contains body or definition. In order to make any undefined method as abstract whose declaration is must be predefined by abstract keyword.

Syntax

abstract ReturnType methodName(List of formal parameter)

Example

abstract   void  sum();
abstract  void  diff(int, int);

Example of abstract class

abstract class Vachile
{  
 abstract void speed();  // abstract method
}  
class Bike extends Vachile
{  
void speed()
{
System.out.println("Speed limit is 40 km/hr..");
}
public static void main(String args[])
{
 Vachile obj = new Bike(); //indirect object creation  
 obj.speed();
 }  
} 

Output

Speed limit is 40 km/hr..

Create an Object of abstract class

An object of abstract class cannot be created directly, but it can be created indirectly. It means you can create an object of abstract derived class. You can see in above example

Example

Vachile obj = new Bike(); //indirect object creation 

Important Points about abstract class

  • Abstract class of Java always contains common features.
  • Every abstract class participates in inheritance.
  • Abstract class definitions should not be made as final because abstract classes always participate in inheritance classes.
  • An object of abstract class cannot be created directly, but it can be created indirectly.
  • All the abstract classes of Java makes use of polymorphism along with method overriding for business logic development and makes use of dynamic binding for execution logic.

Advantage of abstract class

  • Less memory space for the application
  • Less execution time
  • More performance

Why abstract class have no abstract static method ?

In abstract classes we have the only abstract instance method, but not containing abstract static methods because every instance method is created for performing repeated operation where as static method is created for performing a one time operations in other word every abstract method is instance but not static.

Abstract base class

An abstract base class is one which is containing physical representation of abstract methods which are inherited by various sub classes.

Abstract derived class

An abstract derived class is one which is containing logic representation of abstract methods which are inherited from abstract base class with respect to both abstract base class and abstract derived class one can not create objects directly, but we can create their objects indirectly both abstract base class and abstract derived class are always reusable by various sub classes.
When the derived class inherits multiple abstract method from abstract base class and if the derived class is not defined at least one abstract method then the derived class is known as abstract derived class and whose definition must be made as abstract by using abstract keyword. (When the derived class becomes an abstract derived class).
If the derived class defined all the abstract methods which are inherited from abstract Base class, then the derived class is known as concrete derived class.
abstract class in java

Example of abstract class having method body

abstract class Vachile
{  
 abstract void speed();  
 void mileage()
{
 System.out.println("Mileage is 60 km/ltr..");
}
}  
class Bike extends Vachile
{  
void speed()
{
System.out.println("Speed limit is 40 km/hr..");
}
public static void main(String args[])
{
 Vachile obj = new Bike();  
 obj.speed();
 obj.mileage();
 }  
} 

Output

Mileage is 60 km/ltr..
Speed limit is 40 km/hr..

Example of abstract class having constructor, data member, methods

abstract class Vachile
{  
 int limit=40;  
 Vachile()
{
System.out.println("constructor is invoked");
}  
 void getDetails()
{
System.out.println("it has two wheels");
} 
 abstract void run();  
}  
  
class Bike extends Vachile
{  
 void run()
{
System.out.println("running safely..");
}  
 public static void main(String args[])
{  
  Vachile obj = new Bike();  
  obj.run();
  obj.getDetails();  
  System.out.println(obj.limit);  
 }  
}  

Output

constructor is invoked
running safely..
it has two wheels
40

Difference Between Abstract class and Concrete class

Concrete classAbstract class
A Concrete class is used for specific requirementAbstract class is used to fulfill a common requirement.
Object of concrete class can create directly.Object of an abstract class can not create directly (can create indirectly).
Concrete class containing fully defined methods or implemented method.Abstract class has both undefined method and defined method.

Data Conversion in java

Data Conversion In java programming we have six data conversion technique they are:. 1 Converting numeric string type data into numeri...