Powered by Blogger.

/** and /* in Java Commenting Code | How to give comments in Java?

Java programs can have two kinds of comments. implementation comments and documentation comments. Implementation comments are those found in C++, which are delimited by /*...*/, and //. Documentation comments (known as "doc comments") are Java-only, and are delimited by /**...*/. Doc comments can be extracted to HTML files using the javadoc tools.


In Java Technology, we can add comments to Java source code in three ways which are given bellow:

1 public class TestComment{
2 // comment on one line
3 public static void main (String [ ] args){
4 /* comment on one
5 * or more lines
6 */
7 System.out.println(”Comments in Java”);
8 }
9 /** documentation comment
10 * can also span one or more lines
11 */
12 }

Java Source File | Source File Layout Program

A Java technology source file declares firstly package statement (only one package), secondly import statements, and finally classes. Which are following bellow:

1. package <topPackageName> .[< subP ackageName >];
2. import<packageName> .[subP ackageName]*;
3. <classDeclaration>*

The package statement is a way to group related classes. The package statement place at beginning of the source file and only one package statement declaration is permitted. If a Java technology source file does not contain a package declaration then the classes declared in the file belong to the default package. The package names are hierarchical and are separated by dots. The package name to be entirely lower case.

           The import statement is used to make classes in other packages accessible to the current class. Normally, the Java compiler places the .class files in the same directory as the source file present. We can reroute the class file to another directory using the -d option of the javac command (javac -d. ClassName.java).

1 package bankingPrj;
2
3 public class Account{
4 public String accountName = ”Saving Account”;
5
6 public void showAccName( ){
7 System.out.println(accountName);
8 }
9
10 public static void main (String [ ] args ){
11 Account acc = new Account ( );
12 acc.showAccName( );
13 }
14 }

To compile this code, open a command window or terminal and change to the directory where the program is saved and type javac -d. Account.java then a package or folder will be create in the same directory named bankingPrj. Now you can run this Account.class file in bankingPrj package by typing the command java bankingPrj.Account;

1 package bankingPrj;
2
3 import bankingPrj.Account;
4
5 public class Coustomer{
6 public String name = ”James Bond”;
7
8 public static void main (String [ ] args ){
9 Account acc = new Account ( );
10 acc.showAccName ( );
11 Coustomer cous = new Coustomer ( );
12 System.out.println (cous.name);
13 }
14 }

Similarly, we can compile and run this code.

Line 3 we import the Account class from bankingPrj package.

Line 9 we create object of Account class.

Line 10 we call the method of Account class.

What Is an Object?

Object: Instance of class are called object.

Syntax of Object:

public class Tom {
   public Tom(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myTom
      Puppy myTom = new Tom( "nazrul" );
   }
}


Output: 
Passed Name is : nazrul

What is a Class?

Class: Class is a collection of property and method.

Syntax of Class: 

class class_name{
         field;
         method;
}

Sockets or Networking in Java

Socket is the Java programming model to establish the communication link between two processes. A socket can hold input and output stream. A process sends data to another process through the network by writing to the output stream associated with the socket. A process reads data written the another process by reading from the input stream associated with the socket.

Code 5−7: The HostServer.java application

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

public class HostServer{
  public static void main (String [ ] args){
    ServerSocket s = null;
    try{
      s = new ServerSocket(5432);
      Socket s1 = s.accept();
      OutputStream s1out = s1.getOutputStream ();
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter (s1out 12 bw.write (”Hellow Net World”);
      bw.close();
      s1.close();
    }catch (IOException e){
      e.printStackTrac e ();
    }
  }
}

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

public class ClientServer{
  public static void main (String [ ] args){
    try{
       Socket s1 = new Socket(”127.0.0.1”, 5432);
       InputStream is = s1.get InputSt ream();
       DataInputStream dis = new DataInputStream(is);
       System.out.println(dis.readUTF());
       dis.close();
       s1.close();
    }catch (ConnectException e1){
       System.out.println(e1);
    }catch(IOException e2){
       e2.printStackTrace();
    }
  }
}

What is Java?

Java is a computer programming language that is used to build softwares. It is also an application, development, and deployment environment. Java is object oriented programming language to help us visualize the program in real-life terms. The syntax of Java is similar to C and C++ based language. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

Brief History of Java

In 1990, Sun Microsystems began a research project named Green to develop the C and C++ based language. James Gosling, a network software designer was assigned to this project. Gosling’s solution to the problems of C++ was a new language called Oak after an oak tree outside his window at Sun. In May 1995, Sun Microsystems formally announced Java (Oak was renamed Java). Finally, in 1996, Java arrived in the market as a programming language.

With the advent of Java 2 (released initially as J2SE 1.2 in December 1998), new versions had multiple configurations built for different types of platforms. For example, J2EE targeted enterprise applications and the greatly stripped-down version J2ME for mobile applications (Mobile Java). J2SE designated the Standard Edition. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.

On November 13, 2006, Sun released much of Java as free and open source software, (FOSS), under the terms of the GNU General Public License (GPL). On May 8, 2007, Sun finished the process, making all of Java’s core code available under free software/open-source distribution terms.


Table: Major Release Versions of Java.

Version
Date
JDK 1.0
January 23, 1996
JDK 1.1
February 19, 1997
J2SE 1.2
December 8, 1998
J2SE 1.3
May 8, 2000
J2SE 1.4
February 6, 2002
J2SE 5.0
September 30, 2004
Java SE 6
December 11, 2006
Java SE 7
July 28, 2011