Is it possible to call one constructor from another constructor? Saturday, Feb 6 2010 

Java supports this feature using ‘this’ pointer.

Example:

class sample

{

private:

int a, b;

public:

sample(int a)

{

this.a=a;

}

sample(int a, int b)

{

this(a); //calls the single parameter constructor

this.b=b;

}

}

Advertisements

Is constructor really needed for the program? Saturday, Feb 6 2010 

Constructor Requirement…

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit constructor.

Example:

class sample

{

private:

int a, b;

public:

….

}

This sample class doesn’t contain any constructor. But the java compiler will create a default constructor in the byte code with an empty argument… sample() { }

If a class includes one or more explicit constructors like …

class sample

{

private:

int a, b;

public:

sample(int a, int b)

{

this.a=a;

this.b=b;

}

}

then the java compiler does not create the default constructor “sample(){}”.

What is the difference between C++ and Java? Saturday, Feb 6 2010 

C++ Vs Java

Both C++ and Java use similar syntax and are Object Oriented, but:

  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
  • Java does not support destructors but adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework.
  • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
  • C++ requires explicit memory management, while Java includes automatic garbage collection.

UDP Server and Client Thursday, Jan 7 2010 

Program for UDP Server and Client

Server Program:

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

public class Server
{

public static void main(String str1[]) throws Exception
{
DatagramSocket ds;
byte[] buf=new byte[1024];
ds=new DatagramSocket(200);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the String”);
InetAddress ia=InetAddress.getLocalHost();
(more…)

FTP Server and Client Thursday, Jan 7 2010 

FTP Program

Server Program:

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

public class Server
{
public static void main(String asd[]) throws Exception
{
ServerSocket ss=new ServerSocket(1024);
System.out.println(“ServerSocket Generated”);
Socket s=ss.accept();
System.out.println(“ServerSocket Accepted”);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintStream p=new PrintStream(s.getOutputStream());
String fname,str;
(more…)

Next Page »