TechAlpine – The Technology world

10 Java Tips – Series I

Java Tips

10 Java Tips – You must know

How memory is managed in java?

In Java manual memory allocation and de-allocation has been eliminated. Memory in Java is automatically garbage collected so you never have to worry about memory corruption. Java memory is managed by the memory model. The Java Memory model explains synchronization techniques to make sure data corruption does not take place. Synchronization can also help to avoid deadlocks between threads and run programs smoothly. The following diagram gives an overview on java memory management.

Memory management in Java

Image 1: Memory management in Java

Are multiple inheritances possible in java?

No, multiple inheritance is not possible in java. Interface has been introduced to serve the purpose and it has been derived from Objective C.

The reasons for omitting multiple inheritances from the Java language mostly stem from the “simple, object oriented and familiar” goal. As a simple language, Java’s creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++’s unnecessary complexity (simple).

In the designers’ opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritances from the language (just as they cut operator overloading). The designers’ extensive C++ experience taught them that multiple inheritances just weren’t worth the headache.

Instead, Java’s designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C’s protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.







What are the eleven design goals of java?

Following are the eleven design goals of java programming language. Java designers have accomplished the task by keeping these goals in mind.

  • Simple
  • Portable
  • Object Oriented
  • Interpreted
  • Distributed
  • High Performance
  • Robust
  • Multithreaded
  • Secure
  • Dynamic
  • Architecture Neutral

 Why java is called distributed?

Java has an extensive library of routines for handling TCP/IP protocols like HTTP and FTP. So Java applications can open and access objects across the internet via URLs. The network programming using java is strong and easy to use. So java is called distributed.

The distributed nature of Java really shines when combined with its dynamic class loading capabilities. Together, these features make it possible for a Java interpreter to download and run code from across the Internet. This is what happens when a Web browser downloads and runs a Java applet, for example. Scenarios can be more complicated than this, however. Imagine a multi-media word processor written in Java. When this program is asked to display some type of data that it has never encountered before, it might dynamically download a class from the network that can parse the data, and then dynamically download another class (probably a Java “bean”) that can display the data within a compound document. A program like this uses distributed resources on the network to dynamically grow and adapt to the needs of its user.

Why java is called architecture neutral?

The Java compiler generates a bytecode instruction which is independent of computer architecture. The bytecode is designed to be both easy to interpret on any machine and easily translated into native machine code on the fly. The following diagram shows the details how it works.

Java architecture neutral

Image 2: Java architecture neutral

Why java enabled browser is needed to run an applet?

Applet is a java program that works on a web page. So the browser should have the capability to interpret the bytecode and make it work. That is why java enabled browser is required to run an applet embedded in a web page. Actually the browser contains the JVM inbuilt which helps to interpret the byte code and show the output.

What do you mean by ‘Java is a strongly typed language’?

Java is strongly typed means that every variable in java must have a declared type. There are eight primitive types in Java. Four of them are integer types and two are floating-point number types. One is the character type char, used for characters in the Unicode encoding and one is a Boolean type for truth values.







How I/O is managed in java?

In java I/O represents Input and Output streams. Streams are used to read from or write to devices such as file or network or console. Java.io package provides I/O classes to manipulate streams. This package supports two types of streams – binary streams which handle binary data and character streams which handle character data. InputStream and OutputStream are high level interfaces for manipulating binary streams. Reader and Writer are high level interfaces for manipulating character streams.

The following figure shows the relationship of different IO classes addressed in this section

Different I/O classes

Image 3: Different I/O classes

How does buffered stream improve performance in java?

The default behavior of a stream is to read or write one byte at a time. This causes poor I/O performance because it takes lot of time to read/write byte by byte when dealing with large amounts of data. Java I/O provides Buffered streams to override these byte by byte default behaviors. You need to use Buffered streams (BufferedInputStream and BufferedOutputStream) to buffer the data and then read/write which gives good performance. You need to understand the method’s default behavior and act upon that.

The following figure shows how buffered streams divert the data flow.

Buffered streams

Image 4: Buffered streams

How do you measure performance of default behavior and buffering for file reading?

The following code snippet will take similar files to read and write using default reading and buffered reading. And then it will display the time taken by both of them. For testing the code keep the files in local file system as shown below.

Listing 1: Buffering for file reading

package com.performance.io;

import java.io.*;

public class IOTest {

public static void main(String[] args){

IOTest io = new IOTest();

try{

long startTime = System.currentTimeMillis();

io.readWrite(“c:/temp/test-origin.html”,”c:/temp/test-destination.html”);

long endTime = System.currentTimeMillis();

System.out.println(“Time taken for reading and writing using default behaviour : ”

+ (endTime – startTime) + ” milli seconds” );

long startTime1 = System.currentTimeMillis();

io.readWriteBuffer(“c:/temp/test-origin.html”,”c:/temp/test-destination.html”);

long endTime1 = System.currentTimeMillis();

System.out.println(“Time taken for reading and writing using buffered streams : ”

+ (endTime1 – startTime1) + ” milli seconds” );

}catch(IOException e){ e.printStackTrace();}

}

public static void readWrite(String fileFrom, String fileTo) throws IOException{

InputStream in = null;

OutputStream out = null;

try{

in = new FileInputStream(fileFrom);

out = new FileOutputStream(fileTo);

while(true){

int bytedata = in.read();

if(bytedata == -1)

break;

out.write(bytedata);

}

}

catch(Exception e)

{

e.printStackTrace();

}

finally{

if(in != null)

in.close();

if(out !=null)

out.close();

}

}

public static void readWriteBuffer(String fileFrom, String fileTo) throws IOException{

InputStream inBuffer = null;

OutputStream outBuffer = null;

try{

InputStream in = new FileInputStream(fileFrom);

inBuffer = new BufferedInputStream(in);

OutputStream out = new FileOutputStream(fileTo);

outBuffer = new BufferedOutputStream(out);

while(true){

int bytedata = inBuffer.read();

if(bytedata == -1)

break;

out.write(bytedata);

}

}

catch(Exception e)

{

e.printStackTrace();

}

finally{

if(inBuffer != null)

inBuffer.close();

if(outBuffer !=null)

outBuffer.close();

}

}

)







Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *


5 × = 25

============================================= ============================================== Buy TechAlpine Books on Amazon
============================================== ----------------------------------------------------------------

Enjoy this blog? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+
http://techalpine.com/10-java-tips-series-i">
LinkedIn