Thursday, 8 November 2012

Convert Amount In Words

A simple problem is to convert the given amount in words for receipt or other bills related usage.


Thursday, 25 October 2012

Environment Variables In Java


Environment Variables  are variables that are added in any Operating system that can be accessed from anywhere in OS without using the complete(Real) location.

In a java Application System.getenv(); is used to find out the system environment variables

Example:-

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

The Security Manager

The security manager is an object that is used to check whether the operation we want to perform is not violating security policy of the operation system.
Sometime we want to perform any operation and got a exception


Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.io.FilePermission characteroutput.txt write)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
        at java.security.AccessController.checkPermission(AccessController.java:546)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
        at java.lang.SecurityManager.checkWrite(SecurityManager.java:962)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
        at java.io.FileWriter.<init>(FileWriter.java:46)
...

To Prevent from this exception we can use SecurityManager class to check for the permission before performing operation.

The security manager is an object of type SecurityManager; to obtain a reference to this object, invoke System.getSecurityManager.

SecurityManager appsm = System.getSecurityManager();

If there is no security manager, this method returns null.


The SecurityManager class defines many other methods used to verify other kinds of operations. For example, SecurityManager.checkAccess verifies thread accesses, and SecurityManager.checkPropertyAccess verifies access to the specified property. Each operation or group of operations has its owncheckXXX() method.
In addition, the set of checkXXX() methods represents the set of operations that are already subject to the protection of the security manager. Typically, an application does not have to directly invoke any checkXXX() methods.

System Properties in java

There are various system properties that we need to use during our programs to perform some action or access some file.
There are some commonly used properties.


KeyMeaning
"file.separator"Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
"java.class.path"Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home"Installation directory for Java Runtime Environment (JRE)
"java.vendor"JRE vendor name
"java.vendor.url"JRE vendor URL
"java.version"JRE version number
"line.separator"Sequence used by operating system to separate lines in text files
"os.arch"Operating system architecture
"os.name"Operating system name
"os.version"Operating system version
"path.separator"Path separator character used in java.class.path
"user.dir"User working directory
"user.home"User home directory
"user.name"User account name

These are following keys that can be used to find out the property.
"System"  class has a properties Object associated with it that is used to find out those properties.

Example:-


Monday, 30 July 2012

strictfp Keyword in java

In java  strictfp   is used to strict all the calculations of  to strictly follow the strict floating point calculations in all platforms.

It can be used with class , interfaces and non-abstract methods. If used with the method then it ensures all the calculations inside that method must follow floating point math. When used with the class then it ensures all calculations inside that class must follow floating point math.

Compile-time constant expressions must always use strict floating-point behavior

It providestotal platform-independency and  take advantage of the speed and precision of the extended precision floating-point operations supported by x86 CPUs 

JAVA Standard Edition 5.0


JAVA 5.0 ( also known as Standard Edition 5.0 or J2SE 5 or J2SE 1.5) :-

Codename:- TIGER

1. Initially released in September 2004.

2. Has 3200+ classes and interfaces.

3. Introduced several updates and improvements

Annotations:- Anotations wrere used to provide meta-data for the programs so that it can be easily understand for metadata-aware programs.

Generics:-Generics are used to provide types of object belonging to collections. So that type safety cab be gauranteed at compile time.

Autoboxing :- Autoboxing is used to automatically convert from primitive types to wrapper clsses.

Improved syntax for looping :-Improved syntax of loops was introduced like for each loop

Varargs :- Provide flexibility to pass variable no of arguments.
                 Prefer Post

Friday, 13 July 2012

Jagged Array in Java

Java support jagged array. U can have different no of length for each column do don't need to have same no of column for all rows.




Example:-



package javaTest;


public class JaggedArray {
public static void main(String ar[])
{
int[][] a=new int[2][];
a[0]=new int[2];
a[1]=new int[4];
a[0][0]=1;
a[0][1]=12;
a[1][0]=10;
a[1][1]=3;
a[1][2]=12;
a[1][3]=13;
System.out.println("length-"+a.length);
for (int i = 0; i < a.length; i++) {
for (int k = 0; k < a[i].length; k++) {
System.out.print(a[i][k]+" , ");
}
System.out.println("");
}


}


}



Output:-



length-2
1 , 12 , 
10 , 3 , 12 , 13 , 


Varargs in java (Passing variable no of arguments in java program)

After J2SE 5.0 java supports passing of variable arguments in a method that u don't need to fix the no of arguments at the time of defining the method .
Compiler automatically converts the arguments in the form of array so u can access all the arguments at run time.





Example:-


public class Varargs
{

    public static void main(String[] args)
    {

        Varargs var = new Varargs();

        var.show("Ankit", "Singh", "Katiyar");

        var.show("facebook.com\"", "ankitkatiyar91");

    }

    public void show(String... arg)

    {

        for (int i = 0; i < arg.length; i++)

        {

            System.out.println(arg[i]);

        }

    }

}



Note:- it   works only it version J2SE 5.0 or above.

You can use any data type for variable arguments.

Example:-

package javaTest;

public class Varargs {

public static void main(String[] args) {
// TODO Auto-generated method stub
Varargs var=new Varargs();
var.show(1,2,3);
var.show(9,7);

}
public void show(int... arg)
{
for(int i=0;i<arg.length;i++) 
{
System.out.println(arg[i]);
}
}

}

Rules :-
* For any method it must be the last argument.

Friday, 4 May 2012

External garbage collection in java

In java garbage collection is internally implemented. For it JVM(Java Virtual Machine) uses a Class
java.lang.Runtime  .

The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.


You can also externally  set the max heap size for your JVM by the VM command
java -Xms .

If u   want to implement garbage collection in your program u can use the method of Runtime  class there is a native method defined in java that is used to call a garbage collector.


Runtime r = Runtime.getRuntime();
r.gc();

Tuesday, 10 April 2012

Microsoft Windows 7 64bit ODBC type 1 driver DSN problem



In windows7 if you are using type 1 driver for your JDBC the u face a problem of creating DSN from control panel administrative tool then try this location file
this will allow you to create a DSN as u desire.
I tried this working.

c:\windows\syswow64\odbcad32.exe

Enjoy :(

Friday, 6 April 2012

SPEEDUP YOUR COMPUTER BY CLEANING YOUR RAM USING NOTEPAD


Does your computer boot slowly or shuts down slowly? Then this tutorial is for you.. One of the major problem of computer coming up slow is
1. UNUSED JUNK IN RAM.
2. TOO MUCH PROGRAM ON THE COMPUTER DESKTOP( ICONS,)
4. VIRUSES ETC

CLEAN UP YOUR RAM
STEPS:-----------------------------
1- Open Notepad
2- Copy and paste these codes into notepad FreeMem=Space(64000000) .
3- Now Save it as CleanRAM.vbs.
NB:- IN THE SAVE AS TYPE DROP DOWN MENU, CHOOSE ALL FILES
4-Now Open it and your RAM will be free from unused junks!

Friday, 3 February 2012

Jar Files are not working properly


Problem - Jar (.jar) files are not working as it should be they are opening in Nokia Pc Suite or in other program.
Solution – Easiest way to solve this problem
1.    Uninstall the program in which they are opening.
2.    Now change the open with setting of .jar file
a.     Set it to Java(TM) SE Platform.
b.    Mark Always open in this program
3.    Now again install the program
4.    This time jar files will work properly.