Show Mobile Navigation

Webring

Powered by WebRing.
Latest In

Java: a short practical introduction about various varaible types used in Java.

Sachin R Kukale - 02:37
As human language has some specific literals, words, constructors, grammar computer languages also have these defined in it. Compiler (which is different for every language and we cannot execute programs in any language without having specific compiler for it) is responsible for checking whether you are using correct language constructs, grammar, literals etc in that specific language. If you want to know the another language which is unknown to you then you are required to learn the literals of that language, constructs of that language and grammar to construct your sentences along with the words used in that language; likewise to learn any computer language you are required to learn all above particular things and rules to define that language.
Like these other languages Java has some defined set of characters, literal and constant words which are listed below:
1.     Character Set: Character set in Java defines the set of characters that can be use in Java programming language. The allowed set of character that can be used in Java is:
a-z
A-Z
0-9
Unicode characters
2.     Identifiers: Identifiers are the names given to some specific entities of programming language so that these entities can be identified and processed uniquely by the compiler. Identifiers are classified in two types as:
a)     Built-in Identifiers: These are reserved keywords in programming language whose meaning cannot be changed by the programmers. Some built identifiers in Java are:
                                                                    i.            Data Types:
Data types
Required Bytes
Default Value
Min Value
Max Value
Boolean
Not defined
False
True OR
False
Byte
1
0
-128
127
Short
2
0
-32768
32767
Char
2
0
\u0000 0
65535
Int
4
0
-2^31
2^31
Long
8
0
-2^63
2^63-1
Float
4
0.0
1.4E-45
3.4028235E38
Double
8
0.0
4.9E-324
1.7976931348623157E308
b)      User Defined identifiers:  Identifiers which are defined by the user to identify the programming entities are called as user defined identifiers. These identifiers helps programmer to identify the programming entities uniquely.
Rules to define User defined identifiers in Java are:
                                                                     i.            Only letters (a-z), (A-Z), digits (0-9) and underscore (_) can be used to name the identifier.
                                                                   ii.            There is no restriction on the length of name of identifier.
                                                                  iii.            Keywords and built in identifiers can not be used as name for used defined identifiers.
                                                                 iv.            The name of the identifier should not start with digit (0-9).
                                                                   v.            Methods to verify the whether the character can be part of user defined identifier or not are:
boolean isJavaIdentifierPart(char)
boolean isJavaIdenitiferStart(char)
                                Some General Recommendations while defining the name of the identifiers in Java:
Ø  The name of identifiers for a type like class, package, interface, enum and annotations first letter of each word should be written in capital or upper case.
Ø  For method variables first character must be in lower case and first letter of each word should be in upper case (in case of multiple words) All the other letters should be in lower case.
Ø  If you are providing any identifier for constant then all letters must be in uppercase. If multiple words are present then underscore (_) character must be used as separator.
Ex: final flaot PI;
      final float SIMPLE_INTEREST;
3.     Control Statements: if,else,switch,case,default,break,continue,return,goto,while,do,for are some of the general control statements which can be used in Java.
try, catch, finally, throws, assert are the exception handling statements in Java.
ü  Constant and goto can not be used in source file as the meaning of these keywords is not defined in Java.
ü  True, false and null are not keywords and they can be used as values for types.
ü  Every keyword in Java is written in lowercase always. Java is case sensitive programming language.
4.     Data Types: Data types are used indicate the type of data or value that is used. Data types are also used to declare the variable to indicate the type of data that can be hold by the variables in future.
                                i.            Primitive Data types: The primitive data types are used to declare the variables that can hold actual data. There are 8 primitive data types as specified by the Sun Microsystems as: Boolean, byte, short, char, int, long, float, double.
                              ii.            Reference Type: Reference types are used to declare user defined types or arrays. In java user defined types are class, interface, enum, annotations etc. The value for this type will be reference and it is calculated by the JVM, by performing some operations on the actual address. In java actual addresses can not be accessed. For reference type default value is null, byte used is 8 byte.
5.     Variables: Variables are used to indicate the memory locations to access the value present in those particular locations. Variable can hold some value and it indicates to the address of memory where actual data is stored.
                         i.            Instance Variables: A variable which is declared inside the class and outside any member, constructor, initialization block without using static keyword are called as instance variable.
                       ii.            Static variables: A variable which is declared inside the class and outside any member, constructor, initialization block using static keyword are called as instance variable.
                     iii.            Local variables: A variable which is declared inside any member, constructor, and initialization block is called as local variable.
Syntax to declare a variable:

[Modifier(s)]

Syntax to declare and initialize a variable:

[Modifier(s)]=

Syntax to declare and initialize multiple variables:

[Modifier(s)]=,=,=,...,=


6.     Constants: Constants are always declared using the keyword ‘final’. Final variable i.e. constants can not be reassigned. Once constants are initialized before they are used. Constants will not be initialized by JVM the programmer is responsible for initializing the constants. Constants also do not have any default value.

Java Native Interface: a short and useful notes in JNI.

Sachin R Kukale - 02:24
Java native interface is very important concept in Java using which you can implement the features in other programming languages which are platform independent like C or C++. It gives additional strength to the Java language.

Steps to install Borland C++ on your computer:
        i.            Install Borland C++.
      ii.            Provide the path for ‘bin’ directory of C++ in the path environment variable as you set path for JDK.
    iii.            Create two configuration files ‘bcc32.cfg’ and ‘ilink.cfg’ in the ‘bin’ directory of Borland C++.
     iv.            Write following content in ‘bcc32.cfg’:
-I “C:\Borland\Bcc5\include”
-L “C:\Borland\bcc\lib”
       v.            Write following content in ‘ilink32.cfg’ file:
-L “C:\Borland\Bcc5.5\lib”
     vi.            Write sample ‘C’ programs and try to compile and execute to check the you have done configuration correctly or not.
   vii.            Copy all the header files present in JDK include directory to include directory of Borland C++ 5.5 (including the files present inside the folder present in include directory.)
 viii.            Write Java class with native method and compile the java source file to generate ‘.class’  file.
     ix.            Use ‘javah’ command to generate header file corresponding to native implementation.
Syntax:
javah
Ex:
javah Service
       x.            Copy generated header file to the include directory of Borland C++.
     xi.            Write C implementation with include statements i.e.
#include
#include
Where Service.h is header file generated by the javah command.
   xii.            Method name written in C implementation must be same as specified in generated header file.
 xiii.            Compile the C implementation and generate the ‘.dll’ file. To generate the ‘.dll’ file you must use –WD option as:
bcc32 –WD ServiceImple.c
 xiv.            Write java class and load all file in static block as:
static{
System.loadLibrary(“ServiceImple”);
}
   xv.            Write implementation to call the native method.
 xvi.            Compile and execute java class.


Installing Java Development kit and setting up complete java executable environment n your PC.

Sachin R Kukale - 02:22
Installing JDK: JDK can be downloaded from the official site of vendor i.e. Oracle Corporation. As this JDK is free don’t need to pay any money or charges before downloading or installing the JKD in your machine.
But remember JDK is not platform independent and thus you need to choose the correct version of your OS, to download the compatible version of JDK.
Once you complete the download, open the destination folder and double click on .exe file to start the installation of the program. While installing the JDK always keep in mind that:
        i.            You can install more than one versions of JDK at the same time. For ex. You can install jdk 1.4 and jdk7 at the same time and you can choose which version should be used as per the requirements.
      ii.            You can change the destination folder to install the jdk. It is not mandatory to install the jdk in C:\ drive or along with the windows.
Installing jdk is not a difficult process and you are not required to have any special skills. After installing the jdk you can not directly execute java programs you have to set up the environment by following the steps given below.
Setting the path environment variable in Windows permanently in system:
1.      You can setup the environment variable in two ways. We will first see how to set the path for javac compiler in your computer’s environment variable list.
2.      Click on ‘Start’->the Right click on ‘Computer’ -> From the drop down menu select the ‘Properties’ option.
3.      You might get different types of windows depending upon the version of the OS you are using. If you are using Windows 7 or higher then you have to click on ‘Advanced System settings’ option in the left pane of the system properties window.
4.      Then under the ‘Advanced’ tab you will get option ‘Environment variables’.
5.      Click on the option ‘Environment Variable’.
6.      Go to the installation directory of your jdk. Navigate to the ‘bin’ folder of the jdk copy the address in the address bar of the window.
7.      Now after choosing the environment variable option click on ‘Edit’ tab. A small window will be opened with values for ‘Path’ at the end of this current path add ‘;’ (semi-colon) and after that paste the path to bin directory of JDK where it has been installed on your system.
8.      Then click on ‘OK’ and apply the settings.
9.      To view the whether you have set the path correctly go to command prompt by typing ‘cmd’ in run menu and type ‘javac’ at the command prompt. If command is executing successfully, displaying various options to execute the Java program; your path and environment to create and execute java program is set up successfully.
10.   If you are getting message like ‘command ‘javac’ is not found or not valid’ it means that you have not set path correctly for the ‘bin’ directory of JDK where it is installed.
Why it is required to set the path environment variable to execute java programs successfully?
Java programs are executed through the usage of Java compiler and we invoke it by executing ‘javac’ command. Now when you type ‘javac’ at the command prompt the operating system looks for the executable ‘javac’ file in the already defined path environment variable. If system founds then executable ‘javac’ file at the specified location then it executes the command successfully and if not found it returns the error. This is the reason behind setting up the path environment variable to execute the java programs successfully on your computer.

Setting the path environment variable in Windows with command prompt temporarily:
1.      You can define the separate path environment variable for each command prompt you start. But this path environment variable is temporary and when you exit the current command prompt it will be deleted. That is path set at command prompt will be local to that command prompt only.
2.      To see the current path environment variable type: ‘path’.
3.      You will see the current path for the system. Now to set path type:
‘set path=%path%;

You are done! Check whether the path environment variable is set correctly or not by typing ‘javac’ at the command prompt. 

Some special and unique features of java.

Sachin R Kukale - 02:19
Every programming language has its own special features and such features are very much dependent on the objectives behind developing the programming languages. In this section we will discuss some special features of the java programming language with history behind implementing the feature and tools that supports such special features in Java:
ü Simple: The syntax of Java programming language is simple and logical. Java derives most of its structure and syntax from C and C++. The reason behind adopting this simple syntax is to help people understand the Java easily and quickly. Moreover if you have knowledge of C or C++; Java will sound familiar to you. This simplicity in structure as well as adaptation of the syntax from previous popular programming languages made java popular and the same made it easier for programmers who know the C or C++.
ü Object Oriented: Java is purely object oriented programming language. As Java was designed independently and designers didn’t have burden to make it compatible with other source-code based language they used this advantage to create simple, powerful and object oriented language. The Java’s object model is simple and powerful and it can balance the two extreme views about object oriented programming.
ü Robust:  The advent of internet and existence of more than one computing platforms demands the programs that can be executed on variety of systems reliably. Java is strictly typed programming language and it checks your code at compile time and also at run-time, to avoid many errors that may be difficult to find out and solve. Also java restricts you in some areas while it gives you full freedom in another. For ex. To avoid the errors that comes from misunderstood the concept of pointers; Java vendors avoided to add pointers feature in Java. Also the two places where mistakes happens while coding are: exceptional conditions that are not handled properly and memory management. Both of these tasks memory management and exception handling are tedious but in Java you need not to worry about that. Java vendors have provided some features like garbage collector which handles such job internally for you.
ü Platform Independent: Java is platform independent programming language; which means that you can get same results from a program developed on Linux and is executed on Windows or other platforms. Java has some special features which makes it platform independent and some driving forces which caused the development of Java as platform independent.
To understand why Java is platform independent we have to understand the environment in which Java programs are run. Every java programs run in an environment called JVM (Java Virtual Machine). Without JVM we cannot run or execute any program or application which is written using Java. Internally this JVM is responsible for handling the entire execution of the program. JVM provides the memory and other resources for execution of the program on behalf of the operating system.
When you write a code and execute it using the javac compiler the compiler produces a highly optimized code called Bytecode. Then this code is used by JVM to execute the program in real time. JVM is interpreter of the Bytecode and thus we can say that Java uses both compiler and interpreter to execute the programs. So if you have the only JVM implemented for each platform then the Bytecode will give same result along all the platforms.
Remember JVM or Java programming language (Java Development Kit) is also not platform independent but only the programs developed using the Java programming language are platform independent due to the concepts of bytecode and JVM.
ü Security and Portability: The same concepts bytecode and JVM are responsible for providing the two other features of the Java Programming language: portability and security. As bytecode can be read only by JVM no one can insert malicious software or code snippet that can harm other parts of computer as that bytecode will never be read by other applications in the computer. As said earlier due the co-existence of the different computing platforms at the same time it was necessary to make the Java portable so that every user on any platform can use the applications of java or can create applications using Java.
ü Multithreaded: To create real time networked applications Java supports the concept of multithreading, which is important concept when it comes to performing many tasks simultaneously. Java allows us to write programs that can perform multiple tasks at the same time. The multithreading feature is important in multi-tasking and multi-user environment.
ü Architectural Neutral: In changing world we cannot guarantee the continuous existence of anything and so we cannot guarantee that the computing platforms and architecture existing today will last forever. Changes are made for achieving the new goals and aims to provide the better solutions to us. So in the due course of time architecture of computer may change but people behind Java wanted to run the same programs on these changing architectures too. Thus Java was made to be architectural neutral and this feature is also achieved by implementation of JVM, which ensures the safe execution of Java programs on any platforms. It also ensures that program written today will run on future machines as long as JVM exists.

So these are some special features of Java and you can now say that Java is truly programmer’s language which provides full support when required and restricts you when you are going to make mistakes. These features of Java are also called as Java Buzzwords. Remember every feature has reason to implement it and every feature has something which provides support for that feature from core of the Java.
Previous
Editor's Choice