Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Java Native modifier, transient Modifier , volatile Modifier, synchronized Modifier
Previous Home Next

In java applications, sometimes we will want to use a method that exists outside the JVM. In this scenario, the native modifier can help, native modifier can only apply to a method. Just as the abstract keyword, the native keyword indicates that the implementation of the method exists elsewhere.

In case of abstract, the implementation of a method may exist in a subclass of the class in which the abstract method is declared.

But in the case of native, the implementation of the method exists in a library outside the JVM. The native method is usually implemented in a non-Java language such as C or C++. Before a native method can be invoked, a library that contains the method must be loaded and that library is loaded by making the following system call:

System.loadLibrary("libraryName");

To declare a native method, precede the method with the native modifier, but do not define any body for the method.

Example


public native  void Nativemethod() ;

After you declare a native method, you must write the native method and follow a complex series of steps to link it with your Java code. For example, the following code fragment presents an example of loading a library named NativeMethodDef which contains a method named NativeMethod():


Java Tutorialslass NativeModifierExample
	{
       native void NativeMethod();
       static 
		   {
            System.loadLibrary("NativeMethodDef");
           }
     }

Note:The library is loaded in a static code block which suggest that the library is loaded at the class load time, so it is there when a call to the native method is made. One can use the native method in the same way as we use a non-native method. For example, the following two lines of code would invoke the native method,


NativeModifierExample obj = new NativeModifierExample();
obj.NativeMethod();

The Transient Modifier

In an java based application, while is in running mode, the objects are put in the random access memory (RAM) of the computer.That determines the scope and life of the object. However, if an object may be stored in persistent storage outside the JVM, for later use by the same application or by some another application.

The process of storing an object is called serialization. For an object to be serializable, the corresponding class must implement the interface Serializable. Thus, the transient modifier is related to storing an object in persistant storage. Such storage is called the object’s persistent state.

A variable declared transient is not stored, and hence does not become part of the object’s persistent state.The transient modifier can be used to prevent a security-sensitive piece of data from copying to a source where there is no security mechanism exists.

The transient modifier can only be applied to instance variables. When you are declaring an instance variable transient, you are telling the Java Virtual Machine not to store this variable when the object, in which it is declared, is serialized. Thus, when an instance variable is declared as transient, then its value need not persist when an object is stored.

Example


Java Tutorialslass TransientModifier 
	{
     transient int num1;   
	 // variable will not persist
	 int num2;            
	 // variable will persist
	}

The volatile Modifier

In java the volatile modifier only applies to instance variables just like the transient modifier. The variables declared volatile are subject to asynchronous modifications. Thus we can say that, declaring a variable volatile tells the compiler that this variable might be changed unexpectedly by other parts of the program.

So, the compiler takes some special precautions to keep this variable properly updated, volatile variables are generally used in multithreaded or multiprocessor environments. The volatile modifier tells the accessing thread that it should synchronize its private copy of the variable with the original copy in the memory. Consider the following example for the better understanding,

Example


Java Tutorialslass VolatileModifier extends Thread
  {
    private volatile static int someVal;   
	// declared a volatile variable
    VolatileModifier(String str)
      {
       super(str);
      }
    public void run()               
		// implementing the run method for threads
     {
      for (int j=0; j<7; j++)
        {
          try
            {
             System.out.println(getName() + "->"+i);
             if(getName().equals("r4r thread1"))
               {
                someVal=7;
               }
             if(getName().equals("r4r thread2"))
               {
  System.out.println("The value of volatile variable is :"+someVal);
               }
      Thread.sleep(2000);
            }
			Java Tutorialsatch(InterruptedException e)
             {
              e.printStackTrace();
             }
		}
}
Java Tutorialslass VolatileModifierExample
{
public static void main(String args[])
{
Thread t=new VolatileModifier("r4r thread1");     
// creating a thread object
Thread t1=new VolatileModifier("r4r thread2");
t.start();                                        
// starting the execution of thread
t1.start();
}
}

The synchronized Modifier

The synchronized modifier is used in multithreaded programming to control access to critical sections in the program. The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier is generally used mainly in threading environment. We will discuss about it in detail in threading. Its declarative syntax is,


public synchronized void Anymethod()
	{
     // some logic
    } 

Previous Home Next