SYBCA - JavaNotes Unit 1 5
SYBCA - JavaNotes Unit 1 5
This language was developed to solve the 1998 Sun MS released SDK 1.2
challenges of the languages existing at that 1999 Sun MS released J2EE
time in terms of :
2000 J2SE with SDK1.3 was released
o Simpliciy,
2002 J2SE with SDK1.4 was released
o Reliability and
2004 J2SE with JDK5.0 was released
o Portability
By this time, java established in market in
Java developed from its pre-decessor
following editions:-
languages C & C++, by using their many
features. o Standard Edition
The historical milestones of java language o Enterprise Edition
development are:
o Micro Edition
1990 Team headed by James Gosling formed to
decide software language for consumer Java Features
electronic device.
Modern programming languages (C++, VB,
1991 Announce of Ook etc) were having many problems.
1992 “Green Project” team demonstrated To overcome these James Gosling & his team
application using this new language for provided solution as Java language.
home appliances, hand-held devides and
While developing this language, C & C++
touch sensitive screen.
languages were used as predecessor.
1993 For www, “Green Proj Team” suggested
They added many new features and many
useless features were removed.
Due ot this, java can be used to develop 4] Robust & Secure –
application and also web applets.
- Java code is said to be robust (avoid possible
The different features supported are:- runtime error) and also secure due to following
reasons:
1) Compiled and Interpreter based
- strict compile and run-time checking or
2) Platform Independent
datatypes
3) Object Oriented
- garbage collection for virtual memory
4) Robust and secure management
2. Package statement – It is one optional - A class in java code can be created as:
section in java program. class <ClassName>
{
It is used to inform compiler about the ----
package name to which the current java ----
program belongs. }
For this, java language supports “package” 6. main method defination – this is one compulsory
keyword, that can be used as: part of executable java program.
package <PackageName>; - As like C, C++, a java program execution starts with
main method.
package myproj;
- main method must be defined within main class as:
3. Import statement - It is one optional section
in java program. class <ClassName>
{
It is necessary to include the required java public static void main(String args[ ])
lib/package for current program as like header {
----
files of C & C++. ----
}
}
C++ Vs Java
Once the java prog. is coded, it must be saved
by “ClassName” with “.java” extension. C++ Java
C lang supports storage These are not supported Destructor function is Destructor function is
class keywords like auto, by java. supported replaced by finalize( )
extern,register function.
C supports signed & These are not supported Main method in C++ Main method must be
unsgined types. by java. class program, must be within class
outside the class
C supports pre-processor These are not supported
directives like #include, by java. Class defination must Class defination not
#define, #ifdef ends with ; (semicolon) ended by ; (semicolon)
1. Single line comment – any line in program code ii. It must not contain blank space, reserve character
starting with // can be considered as single line like -, period (.)
comment. iii. It must not be a reserve keyword
2. Multi-line comment – in case if set of lines to be iv. It may contain digits, Underscore as
declared as comment, it is to be enclosed in /*….*/.
int Num1, int roll_no.
e.g.
- Variable name can be of any length but it is
Java Tokens recommended it should be meaningful.
- Every high-level language code first must be 3. Literals – This is one type of token created by java
compiled before execution. compiler from java prog code.
- For this compiler is used which compiles the high- - Literal is sequence of characters (digits, letters, or
level language code. other character) that represent constant values.
- A java prog code also gets compiled into - Such constant value can be stored in java program
intermediate code or byte code stored in .class file. variable.
- Literal not only represents value but also represents: V. Increment & Decrement operator
How any such value is stored? VI. Special operator
}
}
}
e.g.2 reading all characters from file.
import java.io.*
Character Stream Classes
class Fio2
- These classes for File I/O are used to perform read
{
& write opepration on file using sequence of public static void main(String args[])
characters. {
- for this, two most commonly used library classes FileReader Fobj = null;
are: try {
FileReader Fobj = new FileReader("input.txt");
FileWriter int c;
i. FileReader – this lib class belongs to java.io. while ((c = Fobj.read()) != -1)
package. {
- This class can be used to perform read operation on System.out.print((char)c);
given file, for this it’s object can be created as: }
FileReader <Obj>=new FileReader(“path & }catch(IOException ex){}
finally
fileName”);
{
e.g.
try
FileReader fr=new FileReader(“input.txt”); {
- If file is available in current location of java Fobj.close();
program, only file name is sufficient, but if it is in }catch(IOException e){}
} creation of files and directories,
}
file searching,
}
ii. FileWriter - this lib class belongs to java.io. File or Directory rename
package.
To check file / directory properties like read-
- This class can be used to perform write operation on only, hidden, etc
given file, for this it’s object can be created as:
FileWriter <Obj>=new FileWriter(“path & File/directory deletion, etc.
fileName”); The File object represents the actual file/directory on
e.g. the disk. Following is the list of constructors to
FileWriter fr=new FileWriter(“input.txt”); create a File object.
- If file is available in current location of java
The object of this class can be created as:
program, only file name is sufficient, but if it is in
complete path must be given as: File <obj>=new File(String pathname);
FileWriter fr=new FileWriter(“C:/abc/input.txt”); e.g.
- While using FileWriter class, following factors to be
rememberd: File ob=new File(“c:\\abc\\f1.txt”);
1. if file is not existing, it will be created by JVM by File ob=new File(“C:/abc/f1.txt”);
given name in given location.
- This class supports many file and directory
2. if file is existing, by default it will be overwritten.
related built-in methods:
3. to avoid overwritting of file, folllwing constructor
to be used:
getParent() – this method returns the name of
FileWriter fr=new FileWriter(“input.txt”,true); parent directory in which file is present.
- this class provides built-in method write( ) which getPath()- this method returns complete path and
can write one character at time in file. name of file including directory and sub-
e.g. directory.
import java.io.*;
class Fio3 exists() – this method returns true if the file is
{ available by the given name else returns false.
public static void main(String args[])
{ canWrite() - this method returns true if the file is
FileWriter Fobj = null; writable by else returns false.
try {
canRead() - this method returns true if the file is
Fobj = new FileWriter("input.txt",true);
readable by else returns false.
Fobj.write('B');
}catch(IOException ex){} length() – this method returns the length of file as
finally integer i.e. number of characters present in file.
{ import java.io.*;
try
{ class Fio3
{
Fobj.close();
public static void main(String args[])
}catch(IOException e){}
{
} File Fobj= new File("c:\\temp\\input.txt");
} System.out.println("Get Parent=" +
} Fobj.getParent());
File Class System.out.println("Get Path=" +
Java File class represents the files and directory Fobj.getPath());
pathnames in an abstract manner. System.out.println("File is Existing=" +
Fobj.exists());
This class is used for
System.out.println("File is Writable=" +
Fobj.canWrite());
System.out.println("File is Readable="
+Fobj.canRead());
System.out.println("File is Length=" +
Fobj.length());
}
}
Output –
Get Parent=c:\temp
Get Path=c:\temp\input.txt
File is Existing=true
File is Writable=true
File is Readable=true
File is Length=2
Unit -VI - stop( ) – by this, applet exeuction is stopped
temporarily.
- destroy( ) – applet exeution is stopped permenantly.