JAVA 2.0 Notes
1. INTRODUCTION
Java is an Object-Oriented, multi-threaded programming language developed by Sun Microsystems in 1991. It is designed to be small, simple and portable. The popularity of the Java is due to 3 key elements powerful programming language, applets and rich set of significant object classes.
Adv of Java
Simple
Secure
Portable
Object Oriented
Robust
Multithreaded
Interpreted
High Performance
Distributed
Dynamic Components of JVM
In Java development environment, there are two parts, a Java compiler and Java interpreter.
Java compiler generates byte code and interpreter executes the Java program. Eventhough the bytecode is slow, it gives platform independent application.
Java program is typed in and saved in a file with extension .java. This file is compiled by javac. It creates a file with .class extension. This is executed by Java file without extension. A simple example for a Java program is
Example 1:
class first
{
public static void main (String s[ ] )
{
System.out.println("Welcome to Java ") ;
}
}
It was saved in the file first.java and compiled by java first.java
and executed by java first.
Class declaration is done in the first line of the program by class classname
Every java program has main( ) method, which was first executed.
void keyword indicates that main( ) does not return any value.
static indicates that main( ) is a class method and can be called without creating an object.
public indicates the method is global.
Data types :
Integers :
long 64
int 32
short 16 -32768 to 32767
byte 8 -128 to 127
Float
float 64double 32
Characters
char 16 0 to 63536
String
Boolean :
boolean true or false
Character Escape Sequences
\n \t \f \b \\ \’ \" \ddd \uxxxx
To declare a variable
type variable=value,variable=value,…;
To initialize a variable
int a=10; String name="palani";
float sal=5400.50f;
Arrays:
int arr [12] ;
char carr[ ] = { ‘a’,’b’,’c’,’d’} ;
int a[12][10] ;
char name[10][10][10] ;
Type casting:
When one type of data is assigned to be another type of variable, an automatic type conversion
will take place.
For example
byte a=10; int c = a + 10;
Here, a is converted to int automatically.
To manually convert a type use (type) value
For example float f = (float) a/ float(b) ; if a, b are integers.
Example 2:
class cast
{
public static void main(String args[ ])
{
byte a = 10, b = 20 ;
int c = 15, d = 2 ;
float f1 = c/d; // f1 is 7.0
float f2 = (float) c / (float) d ; // f2 is 7.5
c = b + 10 ; // b is automatically converted to int
/* b = b + 10 ; Error : cannot assign int to byte */
b = (byte) (b + 10);
System.out.println("f1 =" + f1 " + "f2 = " + f2 " + "c = " + c + "b = "+b);
}
}
Operators
1) Arithmetic - + - * / %
2) Relational - < > <= >= !=
3) Logical - && !=
4) Assignment - =
5) Comparision - = =
6) Incre/Decrement - ++ --
7) Bitwise operator - ~ & ^ >> >>> << &= != ^= 8) Conditional - ? : Comments : // Single line comment /* */ Multiline line comment /** */ Document line comment .
No comments:
Post a Comment