Introduction:
Java is an Objected Oriented Programming. As seen in C++ java has all the
compositions of C++ with some additional advances changes. Java was developed for internet
programming through html by Sun Microsystems CEO James Gosling.
Characteristics of Java:
The OOP concept in java is same as C++ with changes in technique and style.
It has object and class
It has Data Abstraction and Encapsulation
It has Inheritance and Polymorphism
It has Dynamic Binding (constructor)
It has Message Communication from one object to other and through applet application
in html.
Java Features:
Platform independent and portable because source code is converted to byte code.
Robust and secure because manages memory well.
Distributed data can be share online through internet and intranet.
Multithread and interactive; can run more than one program or block at a time.
Java Virtual Machine (JVM) help to manage the memory resource effectively and thus
reducing the startup time of loading the class and sharing data among classes.
Body of Java:
class room //Sub class and Base class
{ float area (float l, float b) //Method or function default is friend
{return l*b;}
double area (double l, double b)
{return l*b;}
}
class display // Main class
{public static void main (String[] args)
{
System.out.println("Friends");
}
}
Java has two files source file which has the code, and class file. The source file need hot
have the same name as class file when there is no public class in the source code file. When
complied java creates separate class file for each class from the source code file, the class be
super class, sub class, or main class. However if the source code file has public class the source
code file should be saved in the name of public class. It the sub class and main class are public
then the source code file should be saved in the name main class. Source file and class files are
case sensitive
1
Introduction to Java
Java and C++
Java does not support operator overloading
Java does not support template class.
Java does not support multiple inheritances; it is achieved through interface.
Java does not support global variable.
Java has no pointers.
Java does not support independent function all function must be inside the class. In java
functions are referred as Method.
The main program is also inside a separate class. It is called main class.
The main class must have same name as the file name with .java as file extension.
Java can be developed as stand alone product but it is extensively and exclusively used to
develop internet based software applications.
Java does not use destructor instead finalize( ) function is called to destroy object.
Data Types:
Character, Integer, float, and double.
Boolean True of false default false.
Array, Class, Interface.
Floating point default is double precision to convert single precision use f or F as suffix to
the float point value. When double variable is assigned to float variable, the double variable
value must be suffixed with f or F to store in float variable as single precision. It can also be
converted using type cast (float)x;
Array:
One dimensional Array Declaration
Method 1:
Step1: int x [ ]; This is an array set to null
Step2: x = new int[10]; This an array allocated with 10 elements.
Method 2:
int x = new int[10]; Array of size 10.
2
Introduction to Java
Method 2:
int y[ ][ ] = new int[3][3]; 3x3 Array.
The two dimensional array in Java is different from other programming tool. Java two
one dimensional arrays are arranged in that order. The advantage of have two one dimensional
arrays to form two dimensional array is shown in the following example.
Scope of Variables:
Instance variable created for each instance of object. Each object has separate memory
address to store the data.
Class variable are global to all the objects created from that class. It has only one memory
location, which is for class alone.
Local variable within the braces in the main and in methods.
Constant:
A symbolic constant value is defined using the keyword final. It cannot be defined inside
the method. It can be used as data member in the class.
final int x=100;
Type casting:
This method is adopted only when to assign higher order variable to lower order variable.
Casting higher order data to lower order data may result in loss of data.
int x=10; float y;
x = (int)y;
3
Introduction to Java
Automatic conversion is possible when the lower order variable is assigned to higher order
variable in such case data is not lost and type casting is not required.
y = x;
Java Operators:
1. Arithmetic operators +, -,*,/, and % - % can be used for real number also.
2. Relational operators <, >, <=, >=, = =, and !=.
3. Logical operators &&, ||, and !
4. Assignment Operators. =
5. Shorthand operator x += 1 is same x=x+1
6. Increment and Decrement operator ++ and
7. Conditional Operator: exp1?exp2:exp3;
8. Bitwise operator: to manipulate data at bit level. &, |, ^ ~(once complement), <<,>>,
>>>(right shift with zero fill)
9. Instanceof operator To find the instance of an object.
A a = new A( );
if (a instanceof student) statement
10. Dot operator to access data members in the class.
Initialization:
Float must be initialized with suffix f or F to avoid compiler error.
Ex: float x =23.45 will be error float x=23.45F is right way to initialize.
Arithmetic expressions
Evaluation of Expressions
Precedence of Operators
Control Structures:
If, if else, else if.
Switch
for and while
Note: unreachable error message can be over come using if control structure.
Jumping Loop:
break, continue, return
In programming languages like C, and C++ jumping in and out or loop is achieved
through break and continue statements. But these jump statements are not specific and it works
only for the block of that control structure. In other word a break or continue cannot be
redirected to other inner block or outer block. This may be overcome through goto statement.
Whereas in Java break or continue can be specifically redirected. Since break and
continue statements can be labeled. Which work similar to goto statement. Java does not support
goto statement. Break or continue statement can be with or without label. If it is without label
statement is applied for that block or else it is redirected to label.
4
Introduction to Java
Class and Objects:
All the algorithm or program must be inside a class as method or data.
Even main must be inside a class which is called main class.
Class defined as public should be saved in that file name.
Class not defined as public. The file may be saved as main class name or first sub class
name. It is always preferred to save as main class name.
All other classes are called sub class.
Methods are defined only inside the class.
Method without argument must be empty not even void.
Method cannot have default argument value.
Method can have final argument with out assigning value to the argument.
Constructor has same name, no return type, and declared any where in the class.
Object can be created only by constructor when constructor is defined.
Method finalize( ) work just like destructor but it should he explicitly created in the class
as protected member and called through object like regular method. This is seldom used
because java takes care of memory management of objects.
Static data members use class itself as instance; the static members are referred directly
through classname.member.
All members and its class are friend by default. The friend members and class can be
used only within that package. It is inherited and accessed outside the class.
Public classes and members can be accessed from any package. It is inherited and
accessed outside the class.
Protected members can be accessed by class and derived of same package and derived of
other packages but not by class of other packages. It can be accessed outside the class
and it is inherited.
Private protected is visible only in the derived of the packages.
Private data member can be accessed only by its class within the class and it cannot be
inherited. Where private method can be accessed outside the class and it is cannot be
inherited.
Each variable must have its own access modifier, that access modifier cannot be grouped
to variables like in c++.
The keyword final can be used to make the final data, final method, and final class. The
final data work just like declaring a variable as constant. Whereas final method will not
allow the derived class to override the base class method, similarly the final class will not
allow to inherit further. They are independent.
The opposite to final is abstract this make the derived class and overriding a must.
The class must also be defined as abstract to define abstract method
The base class must have only the body (declaration style) of the method with abstract
keyword.
Abstract class will not allow to create object, object members should be referred only
through derived object class.
5
Introduction to Java
Program 8.18 to understand visibility control of members
class visibility
{ private int pvt=10;
//protected private int pvtprt=20;
int frd; //default is friend
protected int prt;
public int pub;
void displaypvt()
{System.out.println("The private value is :"+pvt);
}
void display(String t, int a)
{System.out.println("The "+t+ " value is :"+a);
}
class visibilitycontrol
{public static void main(String...args)
{int x;
visibility v = new visibility();
v.displaypvt();
x=v.frd=20; //chain assingment
v.display("friend",x);
x=v.prt=30; //chain assingment
v.display("protected", x);
x=v.pub=40; //chain assingment
v.display("public", x);
}
}
6
Introduction to Java
class RectArea //main class
{
public static void main(String args[])
{ int area;
System.out.println("welcome");
//rectangle a = new rectangle(); //Invokes default contructor
rectangle b = new rectangle(5,5); //Invokes constructor with two argument
area=b.area();
System.out.println("Area is "+area);
//a=b;
//area=a.area();
//System.out.println("Area is "+area);
}
}
}
}
7
Introduction to Java
Program 8.9 Understanding static members
public class staticmembers
{ static int sa;
int a;
staticmembers (){}
8
Introduction to Java
Inheritance:
Base is inherited by using key word extends
The inherited base class is not restricted as private, protected, or public as in C++
All members and its class are friend by default. The friend members and class can be
used only within that package. It is inherited and accessed outside the class.
Public classes and members can be accessed from any package. It is inherited and
accessed outside the class.
Protected members can be accessed by class and derived of same package and derived of
other packages but not by class of other packages. It can be accessed outside the class
and it is inherited.
Private protected is visible only in the derived of all the packages.
Private data member can be accessed only by its class within the class and it cannot be
inherited. Whereas private method can be accessed outside the class and it is cannot be
inherited.
Constructor is not inherited.
The base class is called super class in inheritance.
The constructor value to base class is passed through the derived class by calling the
method super in the derived class or sub class constructor.
Method super can be used only in sub class that is derived class.
Method super must be the first statement in the sub class.
Method super must have the same argument as in the base class.
Even when there is more than one constructor to a base class only one constructor of the
derived can be initiated through the method super in the derived class constructor.
Only Multilevel and hierarchical inheritance can be performed using keyword extends.
Multiple inheritances is developed using interface.
Program 8.11 Inheritance & constructor and super method to pass value to base class
class OneD
{double l;
OneD(int a)
{l=a; }
double length()
{return l; }
}
class TwoD extends OneD
{double b;
TwoD(int x, int y)
{ super(x);
l=x; b=y;
}
double area()
{return (l*b); }
}
9
Introduction to Java
class ThreeD extends TwoD
{double h;
ThreeD(int x, int y, int z)
{ super(x,y);
l=x; b=y;h=z;
}
double volume()
{return (l*b*h); }
}
Program 8.12 and 8.13 overriding and final variable, method, class
class base
{ void display(int a)
{System.out.println("Base or super class value is "+a);
}
}
10
Introduction to Java
//The below method is not possible because it was made final in the base class
//void display(int a)
//{System.out.println("Derived B or sub class value is "+a);
//}
}
}
}
}
}
11
Introduction to Java
Program 8.17 Method with variable argument-It must be last argument in the method
class vararg
{void display(Object...args)
{for(Object O:args)
{System.out.println("The object value is :"+O);
}
}
/*
void display(int...args)
{for(int s:args) //for(Object s:args) will also work
{System.out.println("The integer value is :"+s);
}
} */
}
12
Introduction to Java
9.5 Strings
It has two class String class and StringBuffer class. The capacity of String class is same
as the length of the string value. The capacity of StringBuffer class is length of string value and
16 additional bytes which is default size of StringBuffer.
13
Introduction to Java
}
}
import java.util.*;
for(String s:str)
{list.addElement(s); }
int sa = list.size();
String strc[]= new String[sa];
list.copyInto(strc);//All elements in the list should be string values
for(String sc:strc)
{System.out.println(sc); }
list.insertElementAt("Babbage",2); list.insertElementAt(627012,2);
int i[]={11,22,33};
for(Object o:i)
list.addElement(o);
list.addElement(100); list.addElement(11.34);
list.addElement(22.56f); list.addElement('a'); list.addElement("All");
14
Introduction to Java
for(Object so:stro)
{System.out.println(so); }
Interface:
Multiple inheritances is developed using key word interface.
Interface variables (data member) are final and static by default. Only these variables can
be declared.
Variables must be initialized within interface.
Only body of methods is defined in the interface.
The interface is included in the class using the keyword implements.
All the methods of the interface must be defined in the class when implementing
interface. Something similar to abstract method, when the abstract class extends with
abstract method the abstract method must be override.
More than one interface can be included to a class. Thus multiple inheritances are
achieved.
Then the class must define all or required method inside that class. If the class use the
same name as method in the interface the interface method is implemented, or if the name
is different from the interface included then it method of that class alone not a method
from interface.
Interface cannot have private or protected members.
The members must be public when implementing the interface in a class.
Object can be created from interface just like from class, but class object must be
assigned to the interface object before calling the member from interface object.
Class can be created by extending the base and also including the interface this is similar
to hybrid inheritance in C++.
15
Introduction to Java
}
16
Introduction to Java
class extendinterface
{public static void main(String...str)
{base b = new base();
float f;
f=b.square(10.0f);
System.out.println("Square "+f);
f=b.ruler(20f);
System.out.println("Ruler "+f);
}
}
void display()
{System.out.println("Square "+s);
System.out.println("Circle "+c);
17
Introduction to Java
System.out.println("Cube "+cu);
System.out.println("Cylinder "+cy);
System.out.println("Surface area Cylinder "+cya);
}
}
class multipleinheritance
{public static void main(String...str)
{TwoD TD = new TwoD();
TD.square(10.0f, 20.0f);
TD.circle(10f);
TD.circle(10f,29.5f);
TD.cube();
TD.cylinder(10.0f);
TD.display();
}
}
class volume
{float cube(float x)
{return(x*x*x);
};
}
void display()
{System.out.println("Square "+s);
18
Introduction to Java
System.out.println("Circle "+c);
System.out.println("Cylinder "+cy);
}
}
class hybridinheritance
{public static void main(String...str)
{mix hy = new mix();
float c;
hy.square(10.0f, 20.0f);
hy.circle(10f);
c=hy.cube(5f);
hy.cylinder(10f,10f);
System.out.println("Cube "+c);
hy.display();
}
}
Package:
Package is a collection of class. Ex: java.lang.Math java is package which is inside
another package called lang which has class called Math. Each package will have its own
classes. Through those classes methods are accessed, just like regular class as seen
earlier. Example; Math.sqrt(x); Math.sin(x), etc.
Package works like a folder storing different files and sub folders. Similarly a package
can have class or sub package which may its own class.
The keyword package package_name should be used before beginning the class. This
will create the package; if the package already exists it will store the class in that
package.
Each class should be stored as separate file with package name at the beginning
(test.java).
At least one class should be public class; the file should be saved in the name of public
class. Package can be hidden by making it as default class, default class can be accessed
only by the members of same package.
Package can have base class and derived file as single file, but only one class must be
public class preferably derived class so that base can be accessed through derived class.
A derived class can be created in a separate file by including the base class package at
top.
Only public method can be accessed from a class object when placed in a package. No
other methods such as protected or default can be accessed through object when placed in
package.
19
Introduction to Java
Protected member can be accessed from any class from the same package and from
derived class of different package; but not from separate class of different package.
Private protected is only visible in the derived class of any package.
package pkg1;
public class pkg1extendsA extends pkg1classA
{ public void displayExtendA()
{System.out.println("Default "+def);
//System.out.println("Private "+pvt); Cannot access private outside its class
System.out.println("Protected "+prt); //private protected also visible because inherited
System.out.println("Public "+pub);
}
}
package pkg1;
public class pkg1classB
{ pkg1classA a = new pkg1classA();
public void displaypkg1B()
{System.out.println("Default "+a.def);
//System.out.println("Private "+a.pvt); Cannot access private outside its class
System.out.println("Protected "+a.prt); //private protected not visible because cannot
System.out.println("Public "+a.pub);
}
}
20
Introduction to Java
package pkg2;
public class pkg2classB
{ pkg1.pkg1classA a = new pkg1.pkg1classA();
public void displaypkg2B()
{//System.out.println("Default "+a.def); Cannot access default outside its package
//System.out.println("Private "+a.pvt); Cannot access private outside its class
//System.out.println("Protected "+a.prt); Cannot access default outside its package
System.out.println("Public "+a.pub);
}
}
package pkg2;
public class pkg2extendsA extends pkg1.pkg1classA
{ public void displayExtendApkg2()
{//System.out.println("Default "+def); cannot access from another package
//System.out.println("Private "+pvt); Cannot access private outside its class
System.out.println("Protected "+prt); //private protected also visible because inherited
System.out.println("Public "+pub);
}
}
21
Introduction to Java
}
Multithreading:
Operating systems like windows XP, UNIX, Linux, Macintosh, etc can execute or run
more than one program or application at a time this is called Multitasking or Multithreading in
Java. A single processor shares the processing time; which is not possible to visualize during
execution of these application. Thread is a small program.
Method I:
1. Create a class by extending the built-in class Thread
2. Create a method run( ).
3. Create object from Mythread class and call the start( ) method.
Method II:
1. Create a class by implementing the built-in interface Runnable.
2. Create a method run( ).
3. Create object from the above class
4. Create object from the Thread class passing object as argument and call the start( ) method.
22
Introduction to Java
System.out.println("Thread A " +i);
}
System.out.println("Exit Thread A ");
}
}
class ThreadB extends Thread
{public void run()
{ for (int i=0; i<10; ++i)
{System.out.println("Thread B " +i);
if (i==3)
stop();
}
System.out.println("Exit Thread B ");
}
}
class ThreadC extends Thread
{public synchronized void run()
{for (int i=0; i<10; ++i)
{System.out.println("Thread C " +i);
if (i==6)
try{sleep(1000);}
catch(Exception e)
{System.out.println("Slept");}
}
System.out.println("Exit Thread C ");
}
}
class example2
{ public static void main(String args[])
{ThreadA A = new ThreadA();
ThreadB B = new ThreadB();
ThreadC C = new ThreadC();
23
Introduction to Java
}
}
class ThreadB extends Thread
{public void run()
{for (int i=0; i<10; ++i)
System.out.println("Thread B " +i);
System.out.println("Exit Thread B ");
}
}
class example4
{ public static void main(String args[])
{ThreadA ARun = new ThreadA(); //Step 3
Thread A = new Thread(ARun); //Step 4
ThreadB B = new ThreadB();
B.setPriority(Thread.MIN_PRIORITY);
A.setPriority(B.getPriority()+1);
System.out.println("Start Thread A");
A.start();
System.out.println("Start Thread B");
B.start();
Exception Handling:
Errors are three types, syntax error, run-time error, and logical error. Syntax error can be
easily fixed because a program cannot be executed without fixing syntax errors. The run-time
error will not happen always, this error will trigger only at an instance. For example x=y/z; will
continue to execute without any error unless the value of z is zero. Logical error is like
answering the question incorrectly, example x=a+b*c+d is not same as x=(a+b)*(c+d); compiler
will not give any error until the programmer finds it and fix it .
24
Introduction to Java
User-defined exceptions must be thrown exclusively; unlike built-in exception this will
not raise exception automatically.
Exmple1:
class anexception
{void display()
{int x=2,z=0;
int a[]={2,4,6,8};
for(int e=0; e<5;++e)
{z=a[e]/x;
System.out.println("finally " + z);
}
}
}
25
Introduction to Java
Example2:
class error
{
public static void main(String args[])
{ int a=0,y=5,z=0;
try{ if(z==0)
throw new ArithmeticException();
else
a=y/z;
}
catch (ArithmeticException x)
{System.out.println("AE " +x);}
catch (ArrayStoreException x)
{System.out.println("ASE " +x);}
catch (Exception e)
{System.out.println("Error " +e);}
finally
{System.out.println("Finally")};
}
}
Example3:
class exception
{ public static void main (String...s)
{int x=20,y=0;
try
{y=x/y;
}
finally
26
Introduction to Java
{System.out.println("finally" +y);
}
}
}
Example4:
class MyException extends Exception
{private int detail;
MyException(int a)
{detail=a;
}
public String toString()
{return "Userdefined Exception " + detail;
}
}
class MyExceptionA extends Exception
{private int detail;
MyExceptionA(String m)
{ super(m);
}
}
27
Introduction to Java
}
}
}
Java has developed stream classes to handle input and output operation of files as well as
from keyboard, mouse, disk, memory, network, etc. The stream classes are classified into two
groups.
1. Byte stream classes to handle I/O on bytes.
2. Character stream classes to handle I/O on characters.
28
Introduction to Java
4. writeFloat( ) 8. writeBoolean( )
PipedReader PipedInputStream
PipedWriter PipedOutputStream
Note: Some stream classes and their methods should be placed inside the try block.
The class Stream Tokenizer breaks the text to meaningful pieces called tokens. The
StreamTokenizer class is similar to StringTokenizer class which breaks string into tokens.
29
Introduction to Java
Two or more files can be converted to single file using SequenceInputStream class. And
also Graphical Input and Output can be performed by importing java.awt package and extending
class Frame.
System.out.println("Name ");
st = new StringTokenizer(din.readLine());
String name = String.valueOf(st.nextToken());
System.out.println("Total ");
st = new StringTokenizer(din.readLine());
double total = Double.valueOf(st.nextToken());
}
}
30
Introduction to Java
Write and Read to and from file Example 2:
import java.io.*;
import java.util.*; //Tokenizer class
class interactive
{public static void main(String...s) throws IOException
{File interact = new File ("interact.txt");
DataInputStream din = new DataInputStream(System.in); //Keyboard action
StringTokenizer st;
System.out.println("Name ");
st = new StringTokenizer(din.readLine());
String name = String.valueOf(st.nextToken());
System.out.println("Total ");
st = new StringTokenizer(din.readLine());
double total = Double.valueOf(st.nextToken());
dos.writeInt(reg);
dos.writeUTF(name); //String value
dos.writeFloat(mark);
dos.writeDouble(mark+total);
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
System.out.println(dis.readFloat());
System.out.println(dis.readDouble());
fin.close(); dis.close();
}
}
31
Introduction to Java
Read and Write to and from file by bytes Example 3:
import java.io.*;
class BytesIO
{public static void main(String...s)
{File in = new File ("in.txt");
File out = new File ("out.txt");
FileInputStream ins = null;
FileOutputStream outs = null;
catch(IOException e)
{System.out.println(e);
System.exit(-1);
}
finally
{try{ins.close();
outs.close();
}
catch(Exception e){}
}
}
}
32
Introduction to Java
Read and Write to and from file by character Example 4:
import java.io.*;
class CharacterIO
{public static void main(String...s)
{File in = new File ("in.txt");
File out = new File ("outc.txt");
FileReader ins = null;
FileWriter outs = null;
while ((c=ins.read())!=-1)
{outs.write(c); //do not convert to char to write to a file
System.out.print((char)c); //do not convert to char to display on monitor
}
}
catch(IOException e)
{System.out.println(e);
System.exit(-1);
}
finally
{try{ins.close();
outs.close();
}
catch(Exception e){}
}
}
}
33
Introduction to Java
Read from file and display on monitor by bytes Example 5:
import java.io.*;
class FileToMonitor
{public static void main(String...s)
{File in = new File ("in.txt");
FileInputStream ins = null;
try{ins = new FileInputStream(in); //must be in the try block or throws
byte b;
while ((b=(byte)ins.read())!=-1)
{System.out.print((char)b);
}
}
catch(IOException e)
{System.exit(-1);
}
finally
{try{ins.close(); //must be in the try block
System.out.println();
}
catch(Exception e){}
}
}
}
34
Introduction to Java
class MonitorToFile
{public static void main(String...s)
{File out = new File ("mtf.txt");
FileOutputStream outs = null;
byte c[]={'a','b','c', '\n','d','e'};
try{outs = new FileOutputStream(out); //must be in the try block
outs.write(c);
}
catch(IOException e)
{System.exit(-1);
}
finally
{try{outs.close();
}
catch(Exception e){}
}
}
}
random.writeInt(9800);
random.writeDouble(2213.76);
random.writeBoolean(false);
random.writeChar('R');
random.seek(0);
System.out.println(random.readInt());
System.out.println(random.readDouble());
System.out.println(random.readBoolean());
System.out.println(random.readChar());
35
Introduction to Java
random.seek(4); //go to 4th item false
System.out.println(random.readBoolean());
random.close();
}
}
class PrimaryDataType
{public static void main(String...s) throws IOException
{File primary = new File ("primary.txt");
dos.writeUTF("Electronics ");
dos.writeLong(9800);
dos.writeDouble(2213.76);
dos.writeBoolean(true);
dos.writeChar('R');
dos.writeUTF("and Communication ");
dos.writeLong(980105);
dos.writeDouble(22.76);
dos.writeBoolean(false);
dos.writeChar('C');
dos.close(); fos.close();
}
}
36
Introduction to Java
Points to Remember
File name must be same as class name when there is public class.
File must have .java as extension.
javac test.java to compile java program.
java test.java to run java program.
System.out.print function to display output
System.out.println function to display output and goes to next line.
Built in methods must be identified through class. System is a package, out is the class
and println is the method or function.
Variables in the System.out.println can be concacted using + operator; it implicitly
converts all data to string.
Control structure logic has same rule as C and C++.
Identifier includes number, alphabet, _ and $. The first must not be number.
Java does not support unsigned integers. Integer is 4 bytes.
Java does not converts float to int even if the data to be lost like in C++ (pg.42). The
conversion is automatic if smaller type is assigned to larger type (pg.57), in such type
casting not required like in C++. Type cast is used other wise (pg.58).
Char data type with ++/-- can be used to find the next or previous character in java
(pg:48).
Java has boolean as data type like C++ (pg:49).
Java supports string class to instead of character array but character array can also be
used.
Java does not support two variables with same name even in different blocks (pg:56).
Array size is mention only by using memory manipulator new in java, which is similar to
C++, in other word java does allow to create array with static size.
Array length can be found using length method in java.
Java does check for array boundary when it exceeds it generates error.
To initialize array new is not required (pg.63).
Java has new data type called string.
Strings are concataed (appended) with simple + operator instead of strcpy in C, C++.
Java does not support pointer because there is no pointer in java.
Modulus operator % can be used for all numeric data type.
if(!x) is same as if(x==0) but cannot use if(!x) method like in C, C++.
Short Circuit Logical operator
Boolean logical operator
Break, continue, return, goto, try, catch, throw, throws, and finally to control the flow of
execution.
Break, continue can have label like goto (pg.121).
Function is known as Method in Java.
Methods should be inside the class it cannot be defined outside.
Methods are called through the class to which it belong using dot operator.
Method without argument should not have even void as argument.
Method cannot have default value.
Class members are public by default.
37
Introduction to Java
38