Interface MenuBarPeer in Java

January 10, 2019
Interface MenuBarPeer in Java
Interface MenuBarPeer in Java

Interface MenuBarPeer in Java:

The MenuBarPeer interface specifies the methods that all implementation of Abstract Window Toolkit menubar must define.

The interface structure of MenuBarPeer is given by:

public interface java.awt.peer.MenuBarPeer extends java.awt.peer.ManuComponentPeer{
//methods
public abstract void addHelpManu(Menu m);
//sets the help menu on the menu bar to be the specified menu
public abstract void addMenu(Menu m);
//adds the specified menu to the menu bar.
public abstract void delMenu(int index);
//remove the menu located at the specified index from the menu bar.
}
 
Interface MenuBarPeer in Java Interface MenuBarPeer in Java Reviewed by Animesh Chatterjee on January 10, 2019 Rating: 5

Empty Statements in Java

January 09, 2019
Empty Statements in Java
Empty Statements in Java

Empty Statements in Java:

An empty statement in java consists of only a semicolon(;) It tells JVM not to do anything.
It is particularly useful while implementing delays in the code.

Few Examples of Empty Statements:


if(x<0){
x=-x;
};
//it is legal in java but JVM will consider this an empty statement
 

another example:


if(done)
;//empty statement
else
System.out.println("Oh no");
// it is equivalent to
if(done)
{}//empty statement
else
System.out.println("Oh no");

 

Interesting one:


for(int i=0;i<10;i++);
System.out.println("Hello");
//it will print Hello only once.
 
As we have put the semicolon at the end of the for loop. JVM will consider this as the empty statement and it is equivalent to:

for(int i=0;i<10;i++){
; //empty statement
}
System.out.println("Hello");
 

for creating delay:


for(int i=0;i<1000;i++){
    for(int j=0;i<10000;i++){
    ;
}
}
 
Empty Statements in Java Empty Statements in Java Reviewed by Animesh Chatterjee on January 09, 2019 Rating: 5

Streams in Java

January 09, 2019
Stream Concept in Java
Streams in Java

Streams in Java:

In fie processing,input refers to the flow of data into a program and output means the flow of data out of a program.Input to a program may come from various input devices like keyboard,mouse,memory,disk or any other program. Output from a program may go to screen,printer,memory,disk,network to other systems.
These devices look very different at the hardware level but they share certain common characteristics such as:

  1. Unidirectional flow of data
  2. Treating of data as a sequence of bytes or characters.
  3. Support to the sequential access of data.


IO devices works as below:


Source flow Mediator Destination
keyboard
Mouse
Memory
Disk
Network
Another program



-------------->



Java code



-------------->
Screen
Printer
Memory
Disk
Network
Another program
Java uses the concept of Streams to represent the ordered sequence of data,a common characteristics shared by all input/output devices.A Stream presents an uniform ,easy to use ,object oriented interface between the program and input/output devices.
A Stream in java is a path along which data flows(like pipeline of a water supply).It has a source of data and a destination of the same data.The source and destination may be physical devices or code or other streams in the program.

The data flow via Streams:


Source flow code Program code Destination
Source data -------------->
input Stream
Java code
read operation
Process data
-------------->
Java code
write operation
------------->
Output Stream

Basic Classification of Streams:

Java streams are classified into two basic types:

  1. InputStreams-reads the data from a source and send it to program for processing
  2. OutputStreams-write processed data to a Stream and send it over other devices.
The program connects and opens an InputStream on the data source and then read the data serially or sequentially.Similarly the program connects and opens an OutputStream to the destination place of data and writes data our serially or sequentially.
The program may not know the source or destination of the data at this point.

Ways to read from Stream in Java:

Java supports reading in the following ways:
  1. Sequential
  2. Random access
  3. binary
  4. Characters
  5. Line y line
  6. by words 
  7. by tokens

Further Classification of Stream classes:

Stream classes may be further divided into 
  1. ByteStream- These classes helps to handle I/O operation on bytes
  2. CharacterStream- These classes helps to handle I/O operations on Characters.

Java Stream class Classification Classes Destination
ByteStream Classes InputStream FileInputStream
PipedInputStream
ByteArrayInputStream
SequentialInputStream
ObjectInputStream
StringBufferInputStream
FileterInputSream
|__BufferedInputStream
|__ PushbackInputStream
|__ LineNumberInputStream
|__ DataInputStream
|--> DataInput
Memory
File
Pipe
ByteStream Classes OutputStream FileOutputStream
PipedOutputStream
ByteArrayOutputStream
SequentialOutputStream
ObjectOutputStream
FileterOutputSream
|__BufferedOutputStream
|__ PushbackOutputStream
|__ DataOutputStream
|-->DataOutput
Memory
File
Pipe
CharacterStream Classes Reader Classes Reader Classes Memory
File
Pipe
CharacterStream Classes Writer Classes BufferedWriter
CharArrayWriter
FilterWriter
PrintWriter
StringWriter
PipeWriter
OutputStreamWriter
       |__FileWriter
Memory
File
Pipe

ByteStream Classes:

ByteStream classes have been designed to provide functional feature for creating and manipulating Streams and files for reading and writing bytes.As the Streams are unidirectional , they can transmit bytes in only one direction and therefore java provides two kinds of byte stream classes:

Classification of ByteStream:

  1. InputStream
  2. OutputStream

InputStream Classes:

InputStream classes that are used to read 8-bit bytes includes a super class known as InputStream and a number of sub classes for supporting different input related functionalities.By inheritance all classes derived from InputStream have basic methods called read() for reading a single byte or array of bytes.
The Super class InputStream is an abstract class,we can not create instance of this class.We need to use subclasses that inherit from this class. The inputStream class defines methods for performing the below operations:
  1. Reading bytes
  2. Closing Streams
  3. Marking position in Stream
  4. Shipping ahead in a Stream
  5. Finding the number of bytes in Stream
Class DataInputStream extends to FilterInputStream and implements the interface DataInput. Hence the DataInputStream class implements the methods described in the DataInput apart from its own methods.

OutputStream Classes:

OutputStream classes are derived from the abstract base class OutputStream. All classes derived from OutputStream have basic method write() for writting a single byte or array of bytes.We can not instantiate the OutputStream class. The OutputStream class does the below operations:
  1. Writing bytes
  2. Closing streams
  3. Flushing streams
The WriterStream classes are designed to perform all output related operations on files.While OutputStream classes are used to write bytes the WriterStream classes are used to write characters.The Writer class is an abstract class which acts as a base class for all the other WriterStream classes.The base class provides support for all output operations by defining the methods that are identical to OutputStream class.

The class StreamTokenizer a subclass of Object can be used to break up a Stream of text from file into meaningful pieces called tokens.It is Similar to StringTokenizer class that breaks a String into its component String Tokens.
Streams in Java Streams in Java Reviewed by Animesh Chatterjee on January 09, 2019 Rating: 5

Dangling else problem in Java

January 08, 2019
Dangling else problem in Java
Dangling else problem in Java

Dangling else problem in Java:

In my previous post on Execution control in java- here I have discussed the if else to control of an execution. In this post we will check how else placement can go wrong. So if we misplaced else- it becomes dangling else.

In if statement is in particular, it is assumed that statement-1 or statement-2 will get executed.The problem arises if statement-1 is in an if statement that has no else.This is forbidden in java:
In the below example our objective is to check if the x value is greater than 5 and y value is greater than 10.For the given input of x=10 and x=4

public class HelloWorld{

     public static void main(String []args){
         int x=10;
         int y=20;
         
         if(x>5)
           if(y>10)
           System.out.println("First case");
           else
           System.out.println("Second case");
     }
}
 
x=10
The output of the code:
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
First case
x=4
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld

It is printing only First case or no output at all. This is because of the dangling else problem.As the else part is closer to the second if,the JVM will treat this as part of the inner if. not the outer if.However we can force the JVM to treat it differently by using a bracket.

public class HelloWorld{

     public static void main(String []args){
         int x=5;
         int y=20;
         
         if(x>15){
           if(y>10)
           System.out.println("First case");
            }
          else
           System.out.println("Second case");
     }
}
 
Now the output of the code is:
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
Second case

if else if construction:

if the code is like below:

if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
     else
     statement-3
 
the JVM will treat this as:

if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
else
     statement-3
 
It is kind of 3 way branching.
JVM will execute the first boolean-expression.If it is true,it will execute statement-1 and skips the remaining if conditions and go out of the if block.If the first expression is false it will execute the second boolean-expression and decide the statement-2 and statement-3.
The above could be modified as:

if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
else if
     statement-3
 
In this way it will become multiway branching.
Dangling else problem in Java Dangling else problem in Java Reviewed by Animesh Chatterjee on January 08, 2019 Rating: 5

Class FileDialog in Java

January 07, 2019
Class FileDialog in Java
Class FileDialog in Java

Class FileDialog in Java:

The class FileDialog displays a dialog window from which the user can select a file.It is a modal dialog hence once it's show() method is called,it blocks the rest of the application until the user has cancelled the dialog or has chosen a file. The AWT sends the file dialog window over all the mouse ,keyboard and focus event that occurs over it.

The structure of the class FileDialog is given as:


public class java.awt.FileDialog extends java.awt.Dialog{
//member element
public final static int LOAD;
//this constant indicates that the file dialog window is intend to determine
//a file from which to read.
public final static int SAVE;
//this constant indicates that the file dialog window is intend to determine
//a file to which to write.
//constructor
public FileDialog(Frame parent,String title);
//creates a FileDialog window with the specified tile for loading a file. The files
//shown are those present in the current directory.
public FileDialog(Frame parent,String title,int mode);
//creates a FileDialog window with the specified tile for loading or saving a file. The files
//shown are those present in the current directory.The mode argument must have the value LOAD 
to SAVE.The value LOAD indicates that the file dialog is finding a file to read.The value SAVE
indicates that the file dialog is finding a place to write a file.
//methods:
public void addNotify();
public void setDirectory(String dir);
public void setFile(String file);
public void setFilenameFilter(FilenameFilter filter);
public String getDirectory();
public String getFile();
public String paramString();
public int getMode();
public FilenameFilter getFilenameFilter();
}
 

Class FileDialog in Java Class FileDialog in Java Reviewed by Animesh Chatterjee on January 07, 2019 Rating: 5

ActionListener in Java

January 07, 2019
ActionListener in Java
ActionListener in Java

ActionListener in Java:

ActionListener interface defines the method that an object must implement to listen for action events on AWT components.When as ActionEvent occurs ,an AWT component notifies it's registered ActionListener objects by invoking the actionPerformed() methods.

The structure of ActionListener interface is given as:


public abstract interface java.awt.event.ActionListener extends java.util.EventListener{
//method
public abstract void actionPerformed(ActionEvent e);
}
 
ActionListener in Java ActionListener in Java Reviewed by Animesh Chatterjee on January 07, 2019 Rating: 5

ComponentAdapter in Java

January 07, 2019
ComponentAdapter in Java
ComponentAdapter in Java

ComponentAdapter in Java:

ComponentAdapter class is an implementation of ComponentListener interface containing empty bodies of each methods of the ComponentListener interface. If we do not wish to write every methods of ComponentListener interface in our class,ComponentAdapter class helps in that aspect.

The structure of interface ComponentAdapter is given as:


public abstract class java.awt.event.ComponentAdapter  extends java.lang.Object
implements java.awt.event.ContainerListener
implements java.util.EventListener{
//constructor
public ComponentAdapter();
//methods
public void componentHidden(ComponentEvent e);
public void componentMoved(ComponentEvent e);
public void componentResized(ComponentEvent e);
public void componentShown(ComponentEvent e);
}
 
ComponentAdapter in Java ComponentAdapter in Java Reviewed by Animesh Chatterjee on January 07, 2019 Rating: 5

ContainerAdapter in Java

January 07, 2019
ContainerAdapter in Java
ContainerAdapter in Java

ContainerAdapter in Java:

ContainerAdapter class is an implementation of ContainerListener interface containing empty bodies of each methods of the ContainerListener interface. If we do not wish to write every methods of ContainerListener interface in our class,ContainerAdapter class helps in that aspect.

The structure of interface ContainerAdapter is given as:

public abstract class java.awt.event.ContainerAdapter extends java.lang.Object
                                                      implements java.awt.event.ContainerListener {
//constructor
public ContainerAdapter();
//methods
public void componentAdded(ContainerEvent e);
public void componentRemoved(ContainerEvent e);
}
 
ContainerAdapter in Java ContainerAdapter in Java Reviewed by Animesh Chatterjee on January 07, 2019 Rating: 5
Powered by Blogger.