Java Unit-1
Java Unit-1
[Link]. II Semester IV
Paper - I
UNIT - 1
1.1 JAVA
Java is a high-level, third generation programming language, like C, FORTRAN, Smalltalk, Perl etc.
Java is a general-purpose, object-oriented programming language developed by Sun Microsystems of
USA in [Link] name was “Oak “. In 1995, this language was renamed as “JAVA” due to legal
problem. Inventor of the language are James Gosling, Pattrick Naughton, Arthur Van and others. Java
was invented for the development of software for cunsumer electronic devices like TVs, tosters, etc.
3. Object- oriented
Java is truly object-oriented language. In Java, almost everything is an Object. All program code and
data exist in objects and classes. Java comes with an extensive set of classes; organize in packages that
can be used in program by Inheritance.
5. Distributed
Java is called as Distributed language for applications on networks which can contribute both data and
programs. Java applications can open and access remote objects on Internet easily. That means multiple
programmers at multiple remote locations to work together on single task.
8. High performance
Java performance is very extraordinary for an interpreted language, majorly due to the use of
intermediate byte code. Java architecture is also designed to reduce overheads during runtime. The
incorporation of multithreading improves the execution speed of program.
Java C++
1. Java does not support operator C++ supports operator overloading.
overloading.
2 Java does not support multiple C++ supports multiple inheritance of
inheritance of classes.
classes but it supports interface.
3 Java does not support global variable. C++ support global variable.
Every
variable should declare in class.
4 Java does not use pointer. C++ uses pointer
5 There are no header files in Java. We have to use header file in C++.
The Java Virtual Machine (JVM) is a platform-independent engine that does not exist, used to run Java
applets and applications. The JVM understands the particular file format of the platform and
implementation independent class file produced by a Java compiler. Therefore, class files produced by a
Java compiler on one system can execute without change on any system that can invoke a Java Virtual
Machine. Java compiler convert the source code into intermediate code is called as bytecode. The
bytecode is not machine specific. The machine specific code is generated by Java interpreter by acting
as an intermediary between the virtual machine and real machines
Java environment includes a number of development tools, classes and methods. The development tools
are part of the system known as Java Development Kit (JDK). The classes and methods are part of the
Java Standard Library (JSL), also known as the Application Programming Interface (API).
The line “public static void main (String[] args )” shows where the program will start running.
The word main means that this is the main method – The JVM starts running any program by
executing this method first.
The main method in “[Link]” consists of a single statement
[Link]("Welcome");
The statement outputs the character between quotes to the console.
For compiling and running the program we have to use following
commands:
a) javac (Java compiler)
Java compiler convert the java program into bytecode. It converts “.java” file in “.class” file.
Syntax:
C:\javac [Link];
It creates [Link] file.
b) java(Java Interpreter)
Java interpreter interprets bytecode file on JVM , generate machine specific code and execute it.
Syntax:
C:\java abc
It interprets [Link] file and execute it.
1.9.2 Identifiers:
Identifiers are programmer-created tokens. They are used for naming classes, methods, variables,
objects, labels, packages and interfaces in a program. Java identifiers follow the following rules:
1. They can have alphabets, digits, and the underscore and dollar sign characters.
2. They must not start with a digit.
3. Uppercase and lowercase letters are individual.
4. They can be of any length.
Shivaji Science College, Nagpur
BSC –II Semester-IV
Names of all public methods and instance variables start with leading lowercase letter. Examples :
calculate, total, name etc.
When more than one words are used in names , second and subsequent words are marked a leading
uppercase letters. Example : addTotal, myHeight etc.
All private and local variables use only lowercase letters combined with underscores. Examples :
length, batch_strength etc
All classes and interfaces start with leading uppercase letter. Example : Student, MotorCycle etc.
Variables that represent constant values use all uppercase letters and underscores between words.
Example : SIZE, MAX_VALUE
1.9.3 Keywords:
Java language has reserved 50 keywords. Keywords have specific meaning in Java. We cannot use
them as variable, classes and method. Examples : abstract, char, catch, boolean, default, finally, do,
implements, if, long, throw etc.
1.9.4 Operator:
An operator is symbols that specify operation to be performed may be mathematical and logical
operation. Operators are used in programs to operate data and variables. These are part of mathematical
or logical expressions.
Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operators
1.9.5 Separator:
Separators are symbols. It shows the separated code.
Name Use
() Parameter in method definition, containing statements for conditions etc.
{} It is used for define a code for method and classes
[] It is used for declaration of array
; It is used to show the separate statement
, It is used to show the separation in identifier in variable declarations
. It is used to show the separate package name from subpackages and classes,
separate variable and method from reference variable.
Following table shows the data types with their size and ranges.
1.12 VARIABLES:
A variable is an identifier that denotes storage location. A variable can be used to store a value of any
data type. A variable must be declare in the program before using it. A variable may take different values
at different times during the execution of program.
Rules of Variable names :
A variable name may consists of
• Use alphabets, digits, underscore( _ ), and dollar character( $)
• A name cannot include the space character.
• Do not begin with a digit.
• A name can be of any realistic length.
• Upper and lower case count as different characters.
• A name cannot be a reserved word (keyword).
Example :
int count=10;
float tax=1.7f;
2. Class variables : Class variables are declared in inside class. These are global to a class and belong to
entire set of objects. Only one memory location is created for each class variable.
3. Local variables : Variables declared inside methods are called local variables. These are not available
outside the method. Local variables can also be declared inside program blocks (opening and closing brace).
These variables are visible only in the program block. When the program control leaves the block, all the
variables in the block will erase.
1.13 Constant:
Constant means fixed value which is not change at the time of execution of program. Java supports
different types of constant as follows:
Syntax:
final type Symbolic_name=value;
For example:
final float PI=3.1459;
Constant Meaning
‘\n’ New line
‘\f’ Form feed
‘\b’ Back space
‘\t’ Horizontal tab
‘\r’ Carriage return
‘\’’ Single quote
‘\”’ Double quote
‘\\’ backslash
1.14 Comments:
A comment is a remark in a program. It is not executable statement.
Single line comment : // This is my first program
Multiple line comment : /*
Fdhdfhh
Cvnxcvncn
Shivaji Science College, Nagpur
BSC –II Semester-IV
*/
1.15 Command line arguments:
Command line arguments are parameters that are supplied to the application program at the time of
execution following the file name.
In the main () method, the args is confirmed as an array of string known as string objects. Any argument
provided in the command line at the time of program execution, are accepted to the array args as its
elements. Using index or subscripted entry can access the individual elements of an array.
The number of element in the array args can be getting with the length parameter.
For example:
class Add
{
public static void main(String args[])
{
int a=[Link](args[0]);
int b=[Link](args[1]);
int c=a+b;
[Link](―Addition is=‖+c);
}
}
output:
c:\javac [Link]
c:\java Add 5 2
1.16 Operator:
An operator is symbols that specify operation to be performed may be certain mathematical and logical
operation. Operators are used in programs to operate data and variables. These are a part of mathematical or
logical expressions.
Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
6. Increment and decrement operators
7. Bit wise operators
8 Instanceof Operator
9 Dot Operator
% Modulo Division(Remainder)
Operator Meaning
|| Logical - OR
&& Logical - AND
! Logical - NOT
Example : a>b && z==9
Operator Meaning
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
C=(A<B)?A:B;
C=(3<4)?3:4;
C=4
1.16.8 Instanceof Operator : It is an object reference operator and returns true if the object on the left
hand side is instance of the class given on the right hand side. This operator allows us to find whether the
object belongs to a particular class or not.
Example : account instanceof BankAccount is true if the object account belongs to the class BankAccount
otherwise it is false.
1.16.9 Dot Operator : It is used to access the instance variables and methods of class objects.
Example : [Link]
[Link]()
1.17 Separator:
Separators are symbols. It shows the separated [Link] describe function of our code.
Name use
() Parameter in method definition, containing statements for conditions,etc.
{} It is used for define a code for method and classes
[] It is used for declaration of array
; It is used to show the separate statement
, It is used to show the separation in identifier in variable declarartion
. It is used to show the separate package name from sub-packages and classes, separate
variable and method from reference variable.
First pass:
Z=10-(13/3) + (3*3)-1
Z=10-4+3-1
Second pass:
Z=6+3-1
Z=7
Answer is=7
Following table shows associativity of operators.
* Multiplication
/ Division
+ Addition Left to right 4
- Subtraction
>> Right shift Left to right 5
<< Left shift
>>> Right shift with zero fill
< Less than Left to right 6
<= Less than or equal to
> Greater than
>= Greater than or equal to
Instance of Type comparison
== Equality Left to right 7
!= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional Operator Left to right 13
= Assignment operator Left to right 14
The name that is keyed in by the user is received by the system and stored in the buffer [Link] in
terms of bytes.
In the second statement , the bytes in the buffer [Link] are converted into characters and stored in the
object reader. An inputStreamReader object reader can read characters but can not read the whole string
at a time.
To overcome this, in third statement, inputStreamReader data transferred into buffered object in.
In the fourth statement , the readLine() method reads the data and store in string variable text.
Shivaji Science College, Nagpur
BSC –II Semester-IV
The parseInt method of the class Integer, take out the integer from string text and store it in variable
basicSalary.
Simple Program
Import [Link].*;
class HelloDemo
{
public static void main( String args[]) throws IOException
{
[Link](“Hello, What is your Name ? “);
InputStreamReader reader= new InputStreamReader([Link]);
BufferedReader in =new BufferedReader(reader);
String name =[Link]();
[Link]( "My name is "+ name);
}
}
1.20 Type Casting : The process of converting one data type to another is called casting.
Example :
Int j=20;
Byte k=(byte) j;
Long x= (long)j;
Integer type can be cast to any other type except Boolean. Casting in to smaller type may result in loss
of data. Float and Double can be cast to any other type except Boolean. Casting a floating point to an
integer in a loss of fractional part.
From To
char Int, long , float,Double
byte Short, char, int, long, float, double
short Int, Long, float, double
int Long, float, double
long Float, double
float double