Java
Properties of Java
Simpler ( as pointer is removed)
OOP language
Distributive
Robust
System Secure Language
System Independent
Portability
Interpreted
High Performance
Multithreading
Dynamic
Difference between Java & C++
Java is also a Object Oriented Language, but some differences are there.
C++ Java
C++ is not pure OOP language, But Java is purely OOP language,
because in C++, the program is because in java without classes &
written without classes & objects. objects, program cannot be written.
In C++, pointer is available. But in Java, there is no pointer.
Its programmer’s responsibility to But in Java, allocation & deallocation
allocate & deallocate the memory. of memory is done by JVM(Java
Virtual Machine).
In C++ multiple inheritance is But in Java, Multiple inheritance is not
available. available, but its achieved in another
way.
Operator overloading is there. No operator overloading available.
In C++, 3 access specifiers are there, In Java, 4 access specifiers are there,
public, private, protected. private, public, protected, default.
In C++, there are constructor & But in Java only constructor is
destructor. available, but destructor is not.
Various Editions in Java
• Java SE (Standard Edition)
• Java ME (Mobile Edition)
• Java EE (Enterprise Edition)
Editor in java
cd\java\bin\Edit
Setting path in Java
Right click My Computer Properties Advance Tab Environment
Variables System Variable.
New Class path
Variable Value C:\java\j2sdk1.6\lib;
Path .;C:/java/j2sdk1.6/bin;
(.;) it stores the old(previous) path also.
Program 1.
class Test
{
public static void main(String [] args)
{
System.out.println(“Hello user \n”);
System.out.println(“Welcome to Java \n”);
}
}
Naming Convention in Java
Packages are in small letters.
Eg: java.awt
java.io
java.swing
Classes & interfaces names starts with capital letters
Eg: String
System
ActionListener
The first word of method name is in small letters, then from second
word onwards, each new word will start with capital letter.
println();
readLine();
getNumberInstance();
Its known as camel notation
The naming convention for variable is same as method
Eg: age
empName
employee_Net_Sal
Constraints represents fixed values that can’t be altered, such
constants are represented by using capital letters.
PI
MAX_VALUE
Font.BOLD (Font is a class)
All keywords should be written by using all small letters
Eg: public, static, void
Data types in Java
1. Primitive – Built in
(i) Numeric
(a) Integer
• Byte
• Short
• Int
• Long
(b)Real
• Double
• Float
(ii) Non-Numeric
(a) Char
(b)Boolean
2. Non-Primitive – user Defined
(i) Classes
(ii) Array
(iii) Interfaces
Ranges
Datatype Size in byte Range
Byte 1 -128 to 127
Short 2 -32768 to 32767
Int 4 -2147483648 to 2147483647
Long 8 -9223,372,036,854,775,808 to
9223,372,036,854,775,807
Float 4 -3.4x1038 to 3.4x1038
Double 8 -1.7x10308 to 1.7x10308
Char 2 0 to 65535
Boolean 1 True or False
Type Conversion is done by complier
&
Type Casting is done by programmer
Type Conversion
1. Implicit Conversion
ByteShortCharIntLongFloatDouble
2. Explicit Conversion or typecasting
ByteShortCharIntLongFloatDouble
Rule for implicit conversion
1. Value must belong to the same family, one value can be countable to
the others.
boolean a; or int x;
a=1; x=true;
2. Value on the right handside of an assignment operator, must be larger
or equal to variable on left handside of assignment operator.
byte a=10;
int b;
b=a;
Explicit type conversion syntax
• Variable1 = <datatype>Variable2;
Eg: int a = 10;
byte = b;
b = (byte) a;
3. Every real constant (decimal) is double by default
float a;
a = 11.9; // error will occur, as it takes only double
a = (float) 11.9;
or a = 11.9f; // suffix expression
Byte to Char conversion is possible
byte a = 10;
char ch;
ch = a;
Only byte is automatically converted into char, no other datatype can be
automatically converted into character.
short a = 10; int a;
char ch; char ch;
ch = a; ch = a;
ch = (char) a; ch = (char) a;
char ch;
ch = 10; (Constant can be converted into char)
Type conversion in expression
byte a = 10;
byte b = 10’
byte c;
c = a+b;
c = byte(a+b);
Program 2.
WAP to print the value of variable or expressions ( WAP to add 2 numbers)
class Add
{
public static void main(String [] args)
{
int a =10, b = 5, c;
c = a+b;
System.out.println(“answer is = “+c);
}
}
Program 3.
WAP to calculate area & circumference of circle, given its radius, assume
that radius to be an int variable.
class Circle
{
public static void main(String [] args)
{
int r = 5, a, c
a = Math.PI * Math.pow(r,2);
c = 2*Math.PI*r;
System.out.println(“area = “+a);
System.out.println(“circumference = ”+c);
}
}
Operators
1. Shorthand += , - = , *= , /= , %=
2. Arithmetic + , - , * , / , %
3. Relational > , < , >= , <= , == , !=
4. Logical && , || , 1 , 0
5. Increment or Decrement + + , - -