JAVA PROGRAMMING Introduction For Beginners -TamilYoungsters
INTRODUCTION:
Java is a product of Sun Microsystem.
Java was previously called ‘Oak’.
The main feature of java is platform independent.
FEATURES:
ü Simple
ü Secure
ü Portable
ü Object-Oriented
ü Robust
ü Multithreaded
ü Architecture-neutral
ü Interpreted
ü High Performance
ü Distributed
ü Dynamic
Simple
All methods are predefined with it is more easy and efficiency.
Secure
It is secure from many types of virus which can affect our computer.
Provide firewall program from other damages
Robust
High speed
Memory management is taken here.
Multithreaded
It can perform multiple task at the same time.
Architectural Neutral
Platform independent
Java program can be easily moved from one computer to another
Speciality “write ones, run any time for ever”.
Interpreted (High performance)
Java programs are interpreted into byte code and execution (class file to text).
Distributed
Java is mainly design to internet program.
HOW TO ENTER JAVA
Open command prompt
Check in which drive java installed by using ,
C:\>dir j* command
C:\>cd j2sdk1.4
C:\J2SDK1.4>cd bin
C:\J2SDK1.4\BIN>edit filename.java
TO COMPILE JAVA PROGRAM
C:\J2SDK1.4\BIN>javac filename.java
TO RUN JAVA PROGRAM
C:\J2SDK1.4\BIN>java filename
DATATYPES
Java defines 8 simple type of data.
1. Byte – 8bits (1 byte) => smallest value
2. Short – 16 bits (2 bytes) => least value
3. Int – 32 bits (4 bytes) => most value
4. Long – 64 bits (8 bytes) => long value
5. Char – single letter
6. String – collection of characters
7. Float – real numbers fractional precision.
8. Double – double precision
9. Boolean – true or false
VARIABLES
A variable is an identifier that denotes a storage location used to store a data value. The value of a variable may be changed the execution.
Rules:
The variable name may consist of alphabetics, digits and underscore sign.
They must not begin with a digit.
Uppercase and lowercase are distinct.
It should not be a keyword.White spaces are not allowed.
Syntax:
To declare the variable,
Datatype variablename[=value]...;
Datatype à anyone datatype or name of the class or interface.
Example:
int a,b,c;
int d=3,e=5;char y=’x’;
DYNAMIC INITIALIZATION:
Java allows variables to be initialized dynamically using any expression.
Example:
import java.io.*;
class test
{
public static void main(String args[])
{
double a=3.0,b=4.0;
double c=Math,sqrt(a*a+b*b);
System.out.println(c);
}
}
Here, the value of c is initialized dynamically.
THE SCOPE AND LIFETIME OF VARIABLE
Java allows variables to be declared within any block. A block defines a scope of variable.
Two categories of scope variables:
i) Global variable:
It having the life time, till to the end of the program.
ii) Local variables:
It having the life time, till to the end of the block or module.
Example:
Class test2
{
public static void main(String args[])
{
int a=10; //Global declaration.
if(a>=10)
{
int b,c; //Local declarations.
b=5;
c=a*b;
System.out.println(“Value of c is “+c);
}
System.out.println(“a value is “+a);
}
}
TYPE CONVERSION
One type of data is assigned to another type of variable. This process is known as conversion.
Two types of conversion variable:
i) Automatic conversion
ii) Casting
Automatic conversion:
To perform automatic type conversion satisfy following rules.
i) Two types are compatible
ii) Destination type is larger than the source type.
iii) This conversion is also known as widening conversion.
Example:
byte b=75;
int a=b;
Casting:
To create a conversion between to incompatible types, we must use a cast. A cast is an explicit type conversion. This conversion is also known as narrow conversion.
(target type) value;
Here, target type specifies the desired type to convert the specified value too.
Example:
double d=10.5;
int i;
i=(int) d;
TYPE PROMOTION IN EXPRESSION
Type promotion converts