What is Java
Java is a programming language and a platform.Java is a high level, robust, secured and object-oriented programming language.
Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform.
Java Example
Let's have a quick look at java programming example. A detailed description of hello java example is given in next page.- class Simple{
- public static void main(String args[]){
- System.out.println("Hello Java");
- }
- }
Types of Java Applications
There are mainly 4 type of applications that can be created using java programming:
1) Standalone Application
It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.
Features of Java
The prime reason behind creation of Java was to bring portability and security feature into a computer language. Beside these two major features, there were many other features that played an important role in moulding out the final form of this outstanding language. Those features are :
1) Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.
Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.
2) Object Oriented
In java everything is Object which has some data and behaviour. Java can be easily extended as it is based on Object Model.
3) Robust
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.
4) Platform Independent
Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language.
On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs.
5) Secure
When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.
6) Multi Threading
Java multithreading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.
7) Architectural Neutral
Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.
8) Portable
Java Byte code can be carried to any platform. No implementation dependent features. Everything related to storage is predefined, example: size of primitive data types
9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of just-in-time compiler.
Data Types in Java
Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.
In java, data types are classified into two catagories :
- Primitive Data type
- Non-Primitive Data type
1) Primitive Data type
A primitive data type can be of eight types :Primitive Data types | |||||||
---|---|---|---|---|---|---|---|
char | boolean | byte | short | int | long | float | double |
Integer
This group includesbyte
, short
, int
, long
byte : It is 8 bit integer data type. Value range from -128 to 127. Default value zero. example:
byte b=10;
short : It is 16 bit integer data type. Value range from -32768 to 32767. Default value zero. example:
short s=11;
int : It is 32 bit integer data type. Value range from -2147483648 to 2147483647. Default value zero. example:
int i=10;
long : It is 64 bit integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example:
long l=100012;
Floating-Point Number
This group includesfloat
, double
float : It is 32 bit float data type. Default value 0.0f. example:
float ff=10.3f;
double : It is 64 bit float data type. Default value 0.0d. example:
double db=11.123;
Characters
This group representchar
, which represent symbols in a character set, like letters and numbers.char : It is 16 bit unsigned unicode character. Range 0 to 65,535. example:
char c='a';
Boolean
This group representboolean
, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;
2) Non-Primitive(Reference) Data type
A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change. We will talk a lot more about reference data type later in Classes and Object lesson.Identifiers in Java
All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:- All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.
- After the first character, an identifier can have any combination of characters.
- A Java keyword cannot be used as an identifier.
- Identifiers in Java are case sensitive, foo and Foo are two different identifiers.
Variable
Java Programming language defines mainly three kind of variables.- Instance variables
- Static Variables
- Local Variables
1) Instance variables
Instance variables are variables that are declare inside a class but outside any method,constructor or block. Instance variable are also variable of object commonly known as field or property.class Student
{
String name;
int age;
}
Here name and age are instance variable of Student class.
2) Static variables
Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.class Student
{
String name;
int age;
static int instituteCode=1101;
}
Here instituteCode is a static variable. Each object of Student class will share instituteCode property.
3) Local variables
Local variables are declared in method constructor or blocks. Local variables are initialized when method or constructor block start and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.float getDiscount(int price)
{
float discount;
discount=price*(20/100);
return discount;
}
Here discount is a local variable. Concept of Array in Java
An array is a collection of similar data types. Array is a container object that hold values of homogenous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.
Array Declaration
Syntax :datatype[] identifier;
or
datatype identifier[];
Initialization of Array
new
operator is used to initialize an array.Example :
int[] arr = new int[10]; //10 is the size of array. or int[] arr = {10,20,30,40,50};
Accessing array element
As mention ealier array index starts from 0. To access nth element of an array. Syntaxarrayname[n-1]; Example : To access 4th element of a given array
int[] arr={10,20,30,40};
System.out.println("Element at 4th place"+arr[3]);
The above code will print the 4th element of array arr on console.
foreach or enhanced for loop
J2SE 5 introduces special type of for loop called foreach loop to access elements of array. Using foreach loop you can access complete array sequentially without using index of array. Let us see an exapmle of foreach loop.class Test { public static void main(String[] args)
{ int[] arr={10,20,30,40};
for(int x:arr) { System.out.println(x); } } }
output: 10 20 30 40
Java Operators
Java provides a rich set of operators enviroment. Java operators can be devided into following categories- Arithmetic operators
- Relation operators
- Logical operators
- Bitwise operators
- Assignment operators
- Conditional operators
- Misc operators
Arithmetic operators
Arithmetic operators are used in mathematical expression in the same way that are used in algebra.operator | description |
---|---|
+ | adds two operands |
- | subtract second operands from first |
* | multiply two operand |
/ | divide numerator by denumerator |
% | remainder of division |
++ | Increment operator increases integer value by one |
-- | Decrement operator decreases integer value by one |
Relation operators
The following table shows all relation operators supported by Java.operator | description |
---|---|
== | Check if two operand are equal |
!= | Check if two operand are not equal. |
> | Check if operand on the left is greater than operand on the right |
< | Check operand on the left is smaller than right operand |
>= | check left operand is greater than or equal to right operand |
<= | Check if operand on left is smaller than or equal to right operand |
Logical operators
Java supports following 3 logical operator. Suppose a=1 and b=0;operator | description | example |
---|---|---|
&& | Logical AND | (a && b) is false |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is false |
Bitwise operators
Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byteoperator | description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
<< | left shift |
>> | right shift |
&
, |
and ^
a | b | a & b | a | b | a ^ b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
a = 0001000
b= 2
a << b= 0100000
a >> b= 0000010
Assignment Operators
Assignment operator supported by Java are as followsoperator | description | example |
---|---|---|
= | assigns values from right side operands to left side operand | a=b |
+= | adds right operand to the left operand and assign the result to left | a+=b is same as a=a+b |
-= | subtracts right operand from the left operand and assign the result to left operand | a-=b is same as a=a-b |
*= | mutiply left operand with the right operand and assign the result to left operand | a*=b is same as a=a*b |
/= | divides left operand with the right operand and assign the result to left operand | a/=b is same as a=a/b |
%= | calculate modulus using two operands and assign the result to left operand | a%=b is same as a=a%b |
Misc operator
There are few other operator supported by java language.Conditional operator
It is also known as ternary operator and used to evaluate Boolean expressionepr1 ? expr2 : expr3
If epr1Condition is true? Then value expr2 : Otherwise value expr3
instanceOf operator
This operator is used for object reference variables. The operator checks whether the object is of particular type (class type or interface type)
No comments:
Post a Comment