Java
Java
Properties of Java
Simpler ( as pointer is removed)
OOP language
Distributive
Robust
System Secure Language
System Independent
Portability
Interpreted
High Performance
Multithreading
Dynamic
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.
Program 1.
class Test
{
public static void main(String [] args)
{
System.out.println(“Hello user \n”);
System.out.println(“Welcome to Java \n”);
}
}
Eg: java.awt
java.io
java.swing
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();
Eg: age
empName
employee_Net_Sal
PI
MAX_VALUE
Font.BOLD (Font is a class)
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
1. Implicit Conversion
ByteShortCharIntLongFloatDouble
ByteShortCharIntLongFloatDouble
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;
byte a=10;
int b;
b=a;
• Variable1 = <datatype>Variable2;
Eg: int a = 10;
byte = b;
b = (byte) a;
float a;
a = 11.9; // error will occur, as it takes only double
a = (float) 11.9;
or a = 11.9f; // suffix expression
byte a = 10;
char ch;
ch = a;
char ch;
ch = 10; (Constant can be converted into char)
byte a = 10;
byte b = 10’
byte c;
c = a+b;
c = byte(a+b);
Program 2.
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 + + , - -