Author: Developer Gun

Good Morning SMS

  1. Don’t Dwell in the past,
    Don’t dream of the future,
    concentrate the wind on the present,
    Hi Good Morning..!
  2. Next,

  3. Say
    “e”
    “ee”
    “eee”
    “eeee”
    “eeeee”
    “eeeeeee”
    “eeeeeeeee”
    “eeeeeeeeeee”
    Very Good,
    Just Taught U How To Smile Wide
    Now Go & Brush Your Teeth.. 😉
    Good Morning
Advertisements

Packages in Java, Usage of Packages – Part 2 of Parts 2

java-logo

Naming conventions:

Companies use their reversed Internet domain name in their package names, like this:

com.company.packageName

How to Use a Package:

Calling it by its full name:

For example,

Com.package1.package2 myNewClass = new com.package1.package2();

This type of calling is very time consuming as it is long. It is recommended to use the second method.

Calling with import:

Use “import” keyword to access packages. In program when you write import com.package1.package2, then every time we can type only “package2″, then compiler can understand that we are calling it as com.package1.package2.
Example illustration is best to understand:

import com.package1.package2;
class PackageDemo {
package2 myNewClass= new package2 ();



}

Ways of importing a package:

We can import a package in two ways in to our program when we are coding.
That is, One way is to Package Importing only a single member.
//here ‘subclass’ is a java file in myPackage2

import com.package1.package2.childClass;
class PackageDemo {
childClass myNewClass= new childClass();



}

Second way is, Importing all members of a package.
import com.package1.*;
import java.sql.* ;
When we use *, only the classes in the package referred are imported, and not the classes in the sub package.
For every execution of the Java program, JRE automatically imports two entire packages by default:

The java.lang package java library and the current user defined package by default.

Points to remember:

Sometimes class name conflict may occur. For example:

There are two packages package1 and package2. Assume that both of these packages contains a class “PackageDemo” with the same name, let it be PackageDemo.java. Now both this packages are imported by some other class.

import package1.*;

import package2.*;

The above statements will cause compilation error, because compiler will be in confusion bcz there are two classes with same in two different packages which are imported in the current program. To avoid these naming conflicts in such a situation, we have to be more specific and use the member’s qualified name to indicate exactly which PackageDemo.java class we want:

package1.PackageDemo myNewClass1 = new package1.PackageDemo();

Package2.PackageDemo myNewClass2 = new package2.PackageDemo();

While creating a package, which needs some other packages to be imported, the package statement should be the first statement of the program, followed by the import statement.

Compiling package:

The java compiler can place the byte codes in a directory that corresponds to the package declaration of the compilation unit. The java byte code for all the classes(and interfaces) specified in the source files PackageDemo1.java and PackageDemo2.java will be placed in the directory named package1/package2 , as these sources have the following package declaration

package  package1.package2;

The absolute path of the package1/package2 directory is specified by using the –d (destination directory) option when compiling with the javac compiler.

Assume that the current directory is /packages/project and all the source files are to be found here,the command,

javac –d .file1.java file2.java

Issued in the working directory will create  ./ package1/package2(and any sub directories required) under the current directory, and place the java byte code for all the classes(and interfaces) in the directories corresponding to the package names. The dot (.) after the –d option denotes the current directory. Without the –d option, the default behaviors of  the java compiler is to place all the class files in the current directory rather than the appropriate sub directories.

 

Run the Program with Packages:

Since the current directory is /packages/project and we want to run file1.java,the fully qualified name of the file1 class must be specified in the java command,

java package1.package2.file1

Classpath :

It is a environmental variable, which contains the path for the default-working directory (.).

The specific location that java compiler will consider, as the root of any package hierarchy is, controlled by Classpath

Compile java package

If you are not using any IDE, you need to follow the syntax given below:

javac -d directory javafilename

For example

javac -d . Simple.java

The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use . (dot).

How to run java package program

You need to use fully qualified name e.g. mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java

To Run: java mypack.Simple

Output:Welcome to package

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.

Packages in Java, Introduction – Part 1 or Parts 2

java-logo

Packages

  1. A java package is a group of similar types of classes, interfaces and sub-packages.
  2. A sub-package is again group of similar type of classes, interfaces and sub-packages.
  3. Pakcage in java can be categorized in two types,
    1. Built-in package
    2. and User-defined package

Advantages of Java Package

  1. Easy to Maintain: Java package is used to categorize the classes and interfaces so that they can be easily maintained.
  2. Easy to organize: It is easy to organize class files into packages.
  3. Provide Protection: Java package provides access protection.
  4. Avoid Collision: Java package removes naming collision.
  5. Avoid Conflicts: Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
  6. Reusability: most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be resued by any number of programs wishing to incorporate the class in that particular program.

Types of package:

User defined package:

User-defined packages are created by user as project demands based on its requirements. The package we create is called user-defined package.

Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.

There are many built-in packages in Java JDK such as java, lang, awt, javax, swing, net, io, util, sql etc

Some of the existing packages in Java are::

  • java.lang – bundles the fundamental classes
  • java.io – classes for input , output functions are bundled in this package
  • Java.util.

Where do we include Package in a typical Java program:

This statement should be used in the beginning of the program to include that program in that particular package.

Example:

package tools;

public class Employee {
public void empName ()
{
System.out.println (“Hello there”);
}
}
**Note:

  1. At most one package declaration can appear in a source file.
  2. The package declaration must be the first statement in the unit.

Why Java is “Secured” ?

java-logo

Secure

In realworld applications whenever we develop any project  in any of the language, it is mandatory for the programmers to write security programs.
As on today non-java programmers programmatically has to develop security programs by implementing the alogrithms of network security which is a complex process. It indicates other than Java programming language, there are no readily available security programes.
In Java programming SUN Micro system has provided the complete Implementation for all the algorithms of the network security and provided in Java API.
Java contains readily available security programs. Hence, SUN Microsystem has populated Java is one of the highly secured language

How Java is “Dynamic” ?

java-logo

Dynamic

  1. waste of memory space
  2. loss of data
  3. overlapping of existing data

In any programming language we have 2 types of memory allocations.
They are:

  1. Static Memory allocation
  2. Dynamic Memory allocation

A Static memory allocation is one in which memory will be allocated at compile time. Because of static memory allocation we get the following disadvantages.

  1. Waste of memory space[ no of values we entered at runtime are less than the size of the array]
  2. Loss of data [ no of values we entered at runtime are more than the size of the array]
  3. Overllapping of existing data.

To eliminate the above drawbacks we use dynamic memory allocation technique.
A dynamic memory alocation technique is one which memory will be allocated at runtime.
Java programming never follows static memory allocation but it will always follows dynamic memory allocation by using ‘new’ operator.
‘New’ operator is known as dynamic memory allocation operator.
Therefore, based on the memory allocation, SUN Microsystem has populated JAVA is one of the dynamic programming language.

How Java is “Robust”(Strong) ?

java-logo

Robust

In the programming languages like C, C++, PASCAL etc there is no facility for addressing runtime errors. Hence, C, C++, PASCAL, COBOL etc languages are treated as weak programming languages.
The language Java contains a distinct facility called exception handling, which will address runtime errors effectively.
Hence, Java language is populated as one of the strong programming language.

NOTE:
Compile-time errors are those which are listed/resulted provided the programmer is not following syntaxes of the language.
Runtime errors are those which are listed/resulted when the normal user enters invalid input.

How Java is “Interpreted” ?

java-logo

Interpreted

In the earlier version of Java, Compilation phase is very fast and implementation phase is very slow. This is one of the complaint received by SUN Microsystem from the Industry experts.
To speed up the Interpretation phase, SUN Microsystem has developed a program called JIT ( Just in Time compiler and added as a part of JVM)
In the current versions of the JAVA, Interpretation phase is so faster than compilation phase. Hence, SUN Micro system has populated Java is one of the Highly interpreted programming language compared to all the programming languages available in the Realworld.

Define JIT?
JIT is the program developed by SUN micro system and added as a part of the JVM to speedup the Interpretation phase by reading the entire section of the bytecode and converting into native understanding form of OS.

How Java is rated as “High Performance” ?

java-logo

High Performance (Because Java is “Simple”)

Java is one of the high performance programming language because of the following points.

  1. Magic of bytecode
  2. automatic memory management ( Garbage collector program)
  3. Java programming environment is totally free from a complex concept called pointers. So that performance of the Java programmers is improved.

How Java is “Distributed” ?

java-logo

Distributed

Software Engineer — Java

  • Centralised Application
  • Distributed Application

Centralised:

According to the Industry standards, Java applications classifed into 2 types.

They are :

  1. Centralised Application
  2. Distributed Application

Centralised Applications are those which are running in the context of single server and whose results are sharable across the Globe.

The diagrammatic representation of Centralised Application is shown below.
SBI Application
|
Server
|
C1 C2 C3 …..   Cn

This centralised and Distributed library’s are available only in J2EE and not in J2SE. All the centralised application accessed by authorised people but not all the Real world users. All the centralised application will have private URL but not public URL.
The basic limitation of centralised application is that once the server is down, clients are unable to communicate with the server unless and until server is uploaded again.
In otherwords centralised applications always gives less availability of data.
A distributed application is one which is running in the context of multiple servers and whose results can be accessed across the globe.

In Realworld development process for centralised application and distributed application is same but the way which they are running are quite different. And Java library contains perfect features for development of centralised and distributed applications.

How Java is “Networked” ?

java-logo

Network

Collection of Interconnected non-autonomous/autonomous computers connected with a server is known as Network.

The basic aim of networking is that to share the data between multiple machines which are located either in same network or in different network.

According to Industry standards, networks are classified into two types.

  • Untrusted Network
  • Trusted Network

An Untrusted network is one which there exists “collection of Interconnected non-autonomus computers connected with Server”.

Using this network architecture one can develop the Intranet applications by making use of library of (network programming – java.net.*)

Untrusted network architecture prefered by those organizations whose business operations are restricted to certain place.

A Trusted network one can develop internet applications by making use of J2EE technologies (servlets, JSP, EJB, etc)

This architecture prefered by those organizations whose business operations spread across the globe.

Hence, the libraries of J2SE and J2EE makes us to develop the networking applications like Intranet applications and internet applications respectively.

So that JAVA is one of the Networked programmed language.