Ads 468x60px

Pages

Wednesday, 18 July 2012

new operator in java

In Java in order to create an object ,we must follow dynamic memory allocation by using new operator.
new operator is known as dynamic memory allocation operator.

operations of 'new':
1. It allocates sufficient amount of memory space for the data members of the class.
2. It takes an address of created memory space or address of the class and placed into object name.

Syntax:


1. <class_name> object_name=new <class_name()>;
2. <class_name> object_name; //declaration
    object_name=new <class_name()>;


Here,
class_name is the name of the class.
object_name represents a valid variable name of java.

Whenever an object is declared whose default value is 'null'.Any attempt to use object_name,will result in compile time error.

Whenever an object is referenced whose default value is not 'null'.It simply holds the memory address of the actual object_name.






Tuesday, 17 July 2012

Final variable in java



Any variable either member variable or local variable (declared inside method or block) modified by final keyword is called final variable. Final variables are often declare with static keyword in java and treated as constant.

In Java to make any thing thing as constant we use a keyword final.
The final keyword is placing an important role in three levels.They are


1. At variable level

syntax:    final datatype variable_name=variable_value;
example: final int a=10;

Therefore final variable values can not be modified.

2. At method level


Final keyword in java can also be applied to methods. A java method with final keyword is called final method and it can not be overridden in sub-class. You should make a method final in java if you think it’s complete and its behavior should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded on compile time.
syntax: final return type method_name(list_of_parameters)
{
//body
}

Once the method is final which can not be overridden.

3. At class level

Java class with final modifier is called final class in Java. Final class is complete in nature and can not be sub-classed or inherited. Several classes in Java are final e.g. String, Integer and other wrapper classes.
syntax: final class <class_name>
{

//.................................
}


final classes never participates in inheritance process.




Features of java

Features of any Programming Language is nothing nut the list of services provided by the language to the industry programmers.
Java language provides 13 features.They are


1.Simple
2.platform independent
3.Architecture Neutral
4.Portable
5.Multithreaded
6.Interpreted
7.Object Oriented
8.Robust
9.Distributed
10.Dynamic
11.Secure
12.Performance
13.Networked


The concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key feature of java language that makes java as the most powerful language.

1. SimpleThere are various features that makes the java as a simple language. Programs are easy to write and debug because java does not use the pointers explicitly. It is much harder to write the java programs that can crash the system but we can not say about the other programming languages. Java provides the bug free system due to the strong memory management. It also has the automatic memory allocation and deallocation system.The java programming environment contains in build garbage collector for improving the performance of the java applications by collecting unreferenced memory space.

2. Platform Independent
The concept of Write-once-run-anywhere (known as the Platform independent) is one of the important key feature of java language that makes java as the most powerful language. Not even a single language is idle to this feature but java is more closer to this feature. The programs written on one platform can run on any platform provided the platform must have the JVM. 

3. Architecture Neutral
Java is an architectural neutral language as well. The growing popularity of networks makes developers think distributed. In the world of network it is essential that the applications must be able to migrate easily to different computer systems. Not only to computer systems but to a wide variety of hardware architecture and Operating system architectures as well.  The Java compiler does this by generating byte code instructions, to be easily interpreted on any machine and to be easily translated into native machine code on the fly. The compiler generates an architecture-neutral object file format to enable a Java application to execute anywhere on the network and then the compiled code is executed on many processors, given the presence of the Java runtime system. Hence Java was designed to support applications on network.


4. Portable
A portable application is one which runs on all operating systems and on all available processors without considering their vendors and architectures.

portability=platform independent + architectural neutral

5.  Multithreaded
Java is also a Multithreaded programming language. Multithreading means a single program having different threads executing independently at the same time. Multiple threads execute instructions according to the program code in a process or a program. Multithreading works the similar way as multiple processes run on one computer.
Multithreading programming is a very interesting concept in Java. In multithreaded programs not even a single thread disturbs the execution of other thread. Threads are obtained from the pool of available ready to run threads and they run on the system CPUs. This is how Multithreading works in Java which you will soon come to know in details in later chapters.


6. Interpreted
We all know that Java is an interpreted language as well. With an interpreted language such as Java, programs run directly from the source code.
The interpreter program reads the source code and translates it on the fly into computations. Thus, Java as an interpreted language depends on an interpreter program.
Advantage of Java as an interpreted language is its error debugging quality. Due to this any error occurring in the program gets traced. This is how it is different to work with Java.


7. Object Oriented
To be an Object Oriented language, any language must follow at least the four characteristics.
  • Inheritance   :   It is the process of creating the new classes and using the behavior of the existing classes by extending them just to reuse  the existing code and adding the additional features as needed.
  • Encapsulation  :   It is the mechanism of combining the information and providing the abstraction.
  • Polymorphism   :   As the name suggest one name multiple form, Polymorphism is the way of providing the different functionality by the functions  having the same name based on the signatures of the methods.
  • Dynamic binding  :   Sometimes we don't have the knowledge of objects about their specific types while writing our code. It is the way of providing the maximum functionality to a program about the specific type at runtime.  
Java, it is a fully Object Oriented language because object is at the outer most level of data structure in java. No stand alone methods, constants, and variables are there in java. Everything in java is object even the primitive data types can also be converted into object by using the wrapper class.


 8. Robust
Java has the strong memory allocation and automatic garbage collection mechanism. It provides the powerful exception handling and type checking mechanism as compare to other programming languages. Compiler checks the program whether there any error and interpreter checks any run time error and makes the system secure from crash. All of the above features makes the java language robust.

9. DistributedThe widely used protocols like HTTP and FTP are developed in java. Internet programmers can call functions on these protocols and can get access the files from any remote machine on the internet rather than writing codes on their local system.

10. Dynamic
While executing the java program the user can get the required files dynamically from a local drive or from a computer thousands of miles away from the user just by connecting with the Internet.
The language java always follows dynamic memory allocation only.
Dynamic memory allocation is one in which memory will be allocated at run time.

11. SecureJava does not use memory pointers explicitly. All the programs in java are run under an area known as the sand box. Security manager determines the accessibility options of a class like reading and writing a file to the local disk. Java uses the public key encryption system to allow the java applications to transmit over the internet in the secure encrypted form. The bytecode Verifier checks the classes after loading. 

12. PerformanceJava uses native code usage, and lightweight process called  threads. In the beginning interpretation of bytecode resulted the performance slow but the advance version of JVM uses the adaptive and just in time compilation technique that improves the performance. 

13. Networked
The basic aim of networking is to share the data between multiple machines either locally or globally.we can develop internet applications by making use of J2EE and also we can develop intranet application by making use of J2SE.