The transient and volatile Modifiers

These are the java modifiers which are used to handle some specialized situations.

//example for Transient modifier
 class T 
{ 
transient int a; // will not persist 
int b; // will persist
 }

If the class T is written to a persistent storage area, the contents of a would not be saved, but the contents of b would.

Volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of our program.

It is useful in multi threading where two or more threads want to access the resources then they will store there own copy of values by using this modifier.

Using instanceof

  • This is used to know whether the object is instance of a particular class or not which is useful in threading to know which object reference want the resource.
  • This is useful in casting where some situations child object trying to refer to parent class.
// Demonstrate instanceof operator.
class A {
 int i, j;
}
class B extends A{
 int i, j;
}
class C extends A {
 int k;
}

public class InstanceOf {
 public static void main(String args[]) {
 A a = new A();
 B b = new B();
 C c = new C();

 if(a instanceof A)
 System.out.println("a is instance of A");
 if(b instanceof B)
 System.out.println("b is instance of B");
 if(c instanceof C)
 System.out.println("c is instance of C");
 if(c instanceof A)
 System.out.println("c can be cast to A");
 if(a instanceof C)
 System.out.println("a can be cast to C");
 System.out.println();

 // compare types of derived types
 A ob;
 ob = c; // A reference to c
 System.out.println("ob now refers to c");
 if(ob instanceof C)
 System.out.println("ob is instance of C");
 System.out.println();
 ob = c;// A reference to c
  if(ob instanceof A)
 System.out.println("ob can be cast to A");
 System.out.println();

 if(ob instanceof B)
 System.out.println("ob can be cast to B");
 else
 System.out.println("ob can not cast to B");
 System.out.println();

 // all objects can be cast to Object
 if(a instanceof Object)
 System.out.println("a may be cast to Object");
 if(b instanceof Object)
 System.out.println("b may be cast to Object");
 if(c instanceof Object)
 System.out.println("c may be cast to Object");

 }
}

Output:

a is instance of A

b is instance of B

c is instance of C

c can be cast to A

ob now refers to c

ob is instance of C

ob can be cast to A

ob can not cast to B

a may be cast to Object

b may be cast to Object

c may be cast to Object

strictfp

  • By modifying a class, a method, or interface with strictfp, you ensure that floating-point calculations all the truncations.
  • When we declare a class as strictfp the methods in that class also modified by strictfp automatically.

strictfp class MyClass

{ //...

  • These are not in use as there is no use in more set of programs.

Native Methods

  • These are used to call a subroutine that is written in a language other than Java such a subroutine exists as executable code for the CPU and environment in which you are working—that is, native code.
  • To declare a native method, precede the method with the native modifier, but do not define any body for the method.

public native int meth() ;

// A simple example that uses a native method.
public class NativeDemo {
 int i;
 public static void main(String args[]) {
 NativeDemo ob = new NativeDemo();
 ob.i = 10;
 System.out.println("This is ob.i before the native method:" +
 ob.i);
 ob.test(); // call a native method
 System.out.println("This is ob.i after the native method:" +
 ob.i);
 }
 // declare native method
 public native void test() ;  //no body which have to be loaded from dynamic link library (DLL)
 // load DLL that contains static method
 static 
 {
 System.loadLibrary("NativeDemo");
 }
}

The library is loaded by the loadLibrary( ) method, which is part of the System class.

static void loadLibrary(String filename)

  • we must use javah.exe to produce one file: NativeDemo.h.(javah.exe is included in the JDK.)
  • we will include NativeDemo.h in our implementation of test( ). To produce NativeDemo.h, use the following command:

    javah -jni NativeDemo

  • The following statement defines the prototype for the test( ) function that we will create:

JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *, jobject);

Problems with Native Methods

  • Potential security risk - native code is not confined to the Java execution environment. This could allow a virus infection as it can access the host system.
  • Loss of portability-native code is contained in a DLL, it must be present on the machine that is executing the Java program.

Using assert

This is used to create assertion while developing a program which is a condition that should be true during the execution of the program.

EX: To know whether a function returns positive value or not by checking the condition greater than or equals to if true it will not do anything if false it will throw AssertionError.

The assert keyword has two forms.

  • assert condition;

condition is a boolean expression,If the result is true, then the assertion is true and no other action takes place. If the condition is false, then the assertion fails and a default AssertionError object is thrown.

  • assert condition: expr;

    expr is a value that is passed to the AssertionError constructor. This value is converted to its string format and displayed if an assertion fails.

// Demonstrate assert.
class AssertDemo {
 static int val = 3;
 // Return an integer.

 static int getnum() {
 return val--;
 }
 public static void main(String args[])
 {
 int n;
 for(int i=0; i < 10; i++) {
 n = getnum();
 assert n > 0; // will fail when n is 0
 System.out.println("n is " + n);
 }
 }
}

Output:

n is 3

n is 2

n is 1

Exception in thread "main" java.lang.AssertionError at AssertDemo.main(AssertDemo.java:17)

Assertion Enabling and Disabling Options

While executing a program we can enable or disable assertions in a package called Mypack by using -ea and -da as follows

-ea:MyPack... //enables an assertion

-da:MyPack...//disables an assertion

In a class AssertDemo as follows

-ea:AssertDemo

results matching ""

    No results matching ""