0% found this document useful (0 votes)
22 views12 pages

Java Notes

Uploaded by

farahsfbn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views12 pages

Java Notes

Uploaded by

farahsfbn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Introduction to Java:

Java was been developed by James Gosling and his colleagues in 1991, initially was called as
oak , in 1995 it was renamed as Java.
Few shortcuts related to java.
JDK - Java Development Kit.
JVM - Java Virtual Machine.
J2SE - Java 2 Standard Edition.
J2EE - Java 2 Enterprise Edition.
Java API – Java Application Programming Interface.
WORA - Write Once Run Anywhere.
IDE- Integrated Development Environment.

Features of java:
Object oriented programming: This model organizes the program around its data, hence
securing data. Everything in Java is in the form of classes and objects.
Platform independent: Platform for any program is mainly the Operating System and the
Processor used on the system where it has to be processed. Program once compiled on one
platform may not work on another platform.
But java makes use of both compiler and interpreter, hence it is platform independent.
Java compiler instead of converting the source code(program) to object code(machine lang)
produces an intermediate code called as byte code.
Once byte code is formed it can be executed on any platform with the help of JVM.

Byte Object
Source code
Java Compiler (javac) code JVM
code

Portable: Since java is platform independent once compiled it can be executed on any machine
hence also called as WORA (write once run anywhere)
Multithreaded: One program can be divided into multi threads(tasks) and can be run
simultaneously.
Robust: Java is Robust(reliable) language. As Java has the feature to handle both runtime error
and compile time error, the programs become more efficient and reliable.
Architectural Neutral: Java is architecture neutral because there are no implementation
dependent features.
Distributed: Java is distributed because it facilitates users to create distributed applications
(Parts of the application are shared among systems connected to the network) in Java. RMI
(Remote Method Invocation)is used for creating distributed applications. This feature of Java
makes us able to access files by calling the methods from any machine on the internet.
Garbage collection: In Java, garbage collection is the process of managing memory,
automatically. It finds the unused objects (that are no longer used by the program) and delete or
remove them to free up the memory.

Other terms related to Java

JVM: Java Virtual Machine is an interface(translator) to convert byte code to object code. JVM
and java API together make the platform for java program. JVM on any machine acts as a virtual
processor hence the name.
Java API:API consists of packages which in turn consists of built in classes and functions that
programmers use in their program.

Java is often referred to as platform as they run on a virtual machine(JVM) provided by java,
instead of interacting with platform of system directly.

Examples of few OOP: C++, Eifel, CLOS, small talk etc….

What is IDE ?
IDE stands for Integrated Development Environment which includes
* Text editor--- to type the program.
* A debugger—to debug the errors
* A viewer--- to view the output (terminal window)
Examples of IDE: BlueJ, Jcreator etc…

What is JDK?
Java Development Kit is a collection of tools that allow us to compile run and debug programs.
BlueJ is an example for IDE it provides an easy- to –use teaching environment for the java
language.

Object Oriented Programming Concepts.

Object: It is a software bundle which consists of its own state(data members) and behaviour
( member function).
Class: Class is a prototype or blueprint which defines what an object consists of i.e., what data
members and methods an object or set of objects of similar type should consists of.
Class is also called as object factory as once a class is defined any number of objects can be
created.
Ex: doll prepared using POP (Plaster Of Paris) is an example for an object. Here the size, shape,
colour makes the state of the object. It moves when it is moved, it breaks into pieces when
thrown down, are some of its behaviour. But to prepare such doll we require a moulder which
specify the size and shape for the doll. Now the moulder is an example for class. Moulder itself
is not an object but can be used to create many objects.
State of object: All the data members of an object together make the state.
Behaviour: All the methods of an object make the behaviour of an object.
State: Data members → attribute → values
Behavior → set of functions → methods

Object is an instance of a class, as once a class is defined you can create any number of object
within instance using new keyword.
Message: Objects interact each other through messages. Methods of one object make a call of
another method which may present in the same object or another object, this is called as
messaging. Some times message can contain parameters or values such messages are called as
data messages.
Class variables: data members of a class if defined as static then they are called as class
variables these variables will be common to all objects of the class.
Class methods: methods defined as static are called as class methods. Common to all objects,
none of the objects of this class contains this method. To call this function creation of object is
not required.
Instance variable: non static data members are called as instance variable. Even though they
are declared in a class, they are created(stored in computer memory) when an object is created.
They can be accessed using object name.
Instance method: non static methods can be called only when an object is created. To call these
functions we have to mark use of the object name and the dot separator.
Abstraction: showing of only what user needs and hiding of all the complexity of working of an
object is known as abstraction.
Encapsulation: wrapping up of code and data together is known as encapsulation. By doing so,
some data is secured as the data can be accessed only through the code of the object. This leads
to information hiding. The source code for an object can be written and maintained
independently from other objects hence making it modular.
Inheritance: process of accessing the properties of one object into another object is called as
inheritance. In java property of one class can be inherited by other object using “extends”
keyword.
class A
{
int a;
}
class B extends A
{
int B;
}
Now objects of class B can have both A and B variables.

Polymorphism: When two or more objects responds to the same message differently with
respect to the values given, this process is known as polymorphism. Function overloading is one
form of polymorphism.

Lexical issues of program:


Token: Each and every part of a program is termed as token. In other words whatever is typed in
the text editor is termed as token. Each part in a statement, each statement in a program should
be written using proper syntax.
Syntax of a program or a statement is the set of rules, how it has to be written and used.
White space: White space is the space left between two or more tokens. It can be a single space,
double or more spaces, a tab or even new line character. Java is a free form language as it
doesn’t restrict exact space to be left between tokens.
Keywords: keywords are the reserved words that specify meaning to the compiler. Eg: new, if,
static, while, this, etc…..
Identifier: are the names given by the programmer to various items like variables, function,
class or object. Identifier can be of any name but should follow some rules.
Rules to name an identifier:

• Should not be a keyword, but keyword can be a part of it.eg: else(invalid), someone
else(valid).
• Case sensitive: once used in some case should be used in the same case when ever used.
• No special characters are used except for ‘$’ and ‘_‘ characters.
• Numbers can be a part of variable name but should always start with an alphabet or ‘$’ or
‘_‘.
• No space should be left in between.

Constants or literals:
Are the data values which does not change during the execution of a program.
Integer constant: ‘,’ , ’*’ , ‘a’ , ‘1’ , ‘\n’.
String constant: “this” , “ hello”, “***” ,” ???”.
Character set: Java uses the Unicode character set. Unicode stands for Universal Code, is a 2
byte character set which consists of most human written characters including English, Arabic,
Chinese, etc. The first 128 characters are similar to ASCII (American Standard Code for
Information Interchange) character set.

Escape Sequence: Non graphic characters that cannot be typed directly from keyboard such as
new line characters, backspace can be represented by using escape sequences. An escape
sequence is always preceded by ‘\’ followed by one or more characters.
Eg: ‘\b’ – backspace , ‘\n’ - new line, ‘\t’ – tab space, ‘\\’ – backslash, ‘ \’ ’ – single quote, ‘ \” ‘ –
double quotes.

Variables: Variables are the memory location reserved to store some values. These values can
be changed during the execution of the program.

Separators: Separators are also called as Punctuators. They are used to separate two tokens. -
, --- used to separate two variables while declaration.
. --- used to separate object or class name from its members.
; --- used to separate two statements.
( ) --- used to separate function name from its parameters.
[ ] --- used to separate array name from its subscript (index).
{ } --- used to separate block of statements, group array elements.

Operators: are the symbols used to perform some specified action on the operands. Depending
on number of operands used, operators can be classified as:
1. Unary : ++ --- increment, - - --- decrement, - --- negates the value.
2. Binary: +, -, *, /,>,&& are few examples of binary.
3. Ternary: ? : --- is a ternary operator.
Unary --- ++ used to increment the operand by one. Can be used as prefix (++a) or postfix (a++)
notation.
-- used to decrement the operand by one.
Prefix notation: When an operator is placed before the operand.

Postfix notation: When an operator is placed after the operand.

Ternary - ? : also called as conditional operator


Syntax: expression1 ? expression2 : expression3
Usually expression1 is a condition. If the condition is true, then the operand 2 is executed else
operand 3 is executed.
Eg: a=5,b=10 c=a<b ?a:b;
Here as a<b i.e., 5<10 is true, hence a value 5 is assigned to c.

Depending upon functionality operators are classified as:


Arithmetic, logical,relational operators.
Arithmetic: + add, - sub, * mul , / quotient, % remainder.
Relational: are used to compare two operands and always return Boolean value.

< lesser than


> Greater than
<= lesser than or equal

>= greater than or


equal

== it is equal to

!= not equal to

Logical: used to compare two conditions or Boolean values.

&& ---- returns true only when both the operands are true.
|| ---- logical or ----returns true when any one of the operand is true.
! ---- logical not--- returns true when operand is false.
& ---- similar to logical AND but checks or valuates both the conditions.
| ---- similar to logical OR but evaluates both the condition.
^(XOR) ---- exclusive OR returns true when any one of the operand is true but returns
false when both the operands are true or when both are false.

Difference between & and &&:


&
o Evaluates both the expression
o Even when first condition is false it checks the second expression.
&&
o Conditionally evaluates the second expression.
o When the first condition is false, it never checks the second condition.

Difference between | and ||


|
Evaluates both the expression.
Even when the first condition is true, it checks the second expression.
||
Conditionally evaluates the second expression.
When the first expression is true, it never check the second expression.

Assignment operator:
= is an assignment operator.

Shorthand assignment operators:


All arithmetic operator can be combined with assignment operator to make the shorthand
assignment operators.

Mantissa and exponent part of a double value. The base part is called mantissa and exponent part
is called exponent.
Eg: 3.4*1038 3.4 is mantissa and 38 is exponent part.
Type conversion: the process of converting a value from one data type to another data type is
called type conversion.

Precedence of Operators: The order in which the operators are evaluated in an expression is
called as precedence of operators.
Associativity: When 2 or more operators have the same precedence in an expression, then
associativity specify the direction (left to right or right to left) in which the operators are
evaluated.

Operator Associtivity
() Parenthesis Left to right
[]
++, -- Unary operators Left to right
*, /, % Multiplication, division, modulus Left to right
+, - Addition, subtraction Left to right
<, <= Less than, less than equals Left to right
>, >= Greater than, greater than equals Left to right
==, != Equals, not equals Left to right
&& Logical And Left to right
|| Logical Or Left to right
?: Ternary Operator Right to left
= Assignment Right to left
+=, -=, *=, /+, %= Compound Assignment or Right to Left
Shorthand assignment

Two types of conversion are:


Implicit conversion: also known as automatic conversion or cohesion.
Is the conversion done by the compiler without programmer’s intervention. This is usually done
in the mixed expression when two operands of different type are used in an exprtession. It is
always converted to the largest data type so that information is accurate.
Eg: int a=10;
byte b=15;
float c=35.6;
(a+b)/c. the result will be in float type.
Explicit conversion:
Also called as type casting, is performed by the programmer by forcing an expression to be of
specific type. (type) expression.
Ex: int a=97;
Char x=(char) c;
Float a=2.3, b=3.6;
int x=(int)(a+b);
+ = ------ op1 + = op2 here op1 is added with op2 and the result is stored is op1.
--- =, *=, /= ,%= are other shorthand operators.
Expression--- combination of operators and operands make an expression.
Ex: c=++a – b*d;

Types of expression:
Pure expression: where all the operands of the expression are of same type.
Ex: int a=5, b=10;
System. out.println(a/b);
Here as both the operands of integer type, the return type will also be integer, hence the result is
0.
Mixed expression( Impure Expression): where the operands are of different dat type. Return
value will be of greater data type. Ex: int a=5, float b=100;
System.out.println(a/b); the result will be 0.5.

Difference between Unicode and ASCII:


ASCII:
1. ASCII: is 8 bit character set can store upto 256 characters.

2. Unicode : is 16 bit character set can store upto 65,536 characters including ascii values and
other characters like Arabic, Chinese, hindi etc…

Data type : before declaring any variables we have to specify what type of value we are going
to store in it. This is called as data type.

Types of data type:

Primitive data type: basic data type provided by java are called primitive data type.
Reference data type: the user defined types or composite type are called as reference data
type. These type are constructed from primitive dat types. A reference datatype is used to store
the memory addresses of a class or an array. The variables storing memory addresses are known
as reference data type.

Classess arrays are ex: for reference datatype.


Primitive data type: java supports 8 basic data types.
Integer--- byte, int, short, long.
Float--- float, double.
char. Char
boolean. true/false.

TYPE SIZE RANGE


byte 8 bits(1byte) 27 to 27-1 (-128 to
127)
short 16 bits(2bytes) 215 to 215-1(-32768
to 32767)
int 32 bits(4bytes) 231 to 2 31 -1
long 64bits(8 bytes) 263 to 263 -1
float 32 bits(4 bytes) 3.4E +38(-3.4
*1038) to 3.4E +38
double 64bits(8bytes) 1.7E + 308 to 1.7
+ 308
char 16 bits(2bytes) 0 to 65.535
boolean Reserves 1 byte of True or false.
memory but uses 1
bit to store 0 for
false and 1 for true.

Types of Errors:

Syntax Errors: The errors that take place due to the violation of the syntax is called as syntax
errors. These errors can be rectified during the compilation of the program.
Ex: Semicolon missing, Unclosed String literal, etc.

Run Time Errors: Run time errors are also called as Exceptions. These errors occur during the
execution of the program.
Ex: ArrayIndexOutofBoundException, IndexOutofBoundException, NumberFormatException,
ArithmeticException, InputMismatchException, etc.

Logical Errors: The errors that cannot be rectified either during the compilation time or during
the execution are called as logical errors. Even though Java doesn’t display any error, but still
the program produces the wrong output due to mistake in the logic.
Ex: Area_Triangle=1/2.0*b*b; in this statement, instead of using base * height, we have used
base*base, so even though there is no error in this statement, still the output is wrong.

Decision Making / Branching / Selection Statements:


Compound Statements: Group of statements enclosed within curly braces is called as compound
statement.
Ex:
{
Statement1;
Statement 2:
}
Java supports two selection statements if and switch.
if statement is condition branching statement. It either executes the group of statements(if block)
or skips it depending upon the condition.
Syntax: if(condition)
{
If block statements;
}
If the condition is true, if block is executed else if block is skipped.
if – else statement:
if(condition)
{
If block statement;
}
else
{
Else block statement;
}
If the condition is true if block is executed and else block is skipped, if the condition is false if
block is skipped and else block is executed.
if – else – if ladder:
if(conditon1)
{
If block;
}
else if(condition 2)
{
Else if block;
}
else
{
Else block;
}
If – else – if is a multiple branching statement. The statements are executed from top-down. As
soon as one of the conditions controlling the if is true, the statement associated with that if is
executed rest of the blocks are skipped. If none of the statements are true then else block is
executed.
Nested if – An if within an if is called as nested if.
Syntax:
if(condition1)
{
if(condition2)
statement1;
else
statement2;
}
Dangling else: The ambiguity that arises in a nested if due to presence of more number of if then
the number of else is called as dangling else. This problem can be solved by using proper
braces{}.
Ex:
if(a>0)
if(a%2==0)
System.out.println(“Positive even number”);
else
System.out.println(“Negative number”);

After inserting braces


if(a>0)
{
if(a%2==0)
System.out.println(“Positive even number”);
}
else
System.out.println(“Negative number”);

switch block:

Syntax:
switch(variable/expression)
{
case constant1: Statements; break;
case constant2: Statements; break;
:
:
case constantn: Statements; break;
default: Statements; break;

}
break: break keyword is used to terminate the switch block.
default: when none of the case matches default statement is executed.
case: case keyword checks whether the control variable matches with the constant value given
after the case, if it matches the statements after the case are executed.

Iterations
Group of statements executed for number of times is called as iteration.
3 Iteration statements supported by Java are : while(), do..while() and for loop.
Difference between while() and do while()
while do while
While is an entry controlled loop Do while is an exit controlled loop
Condition is checked before the body of the Condition is checked at the end of the body of
loop the loop
Loop is not executed even once if the condition Loop is executed atleast once even if the
is false for the first time condition is false for the first time.

while and for


While For
While loop is better used for indefinite loop, Better used for definite loop i.e., when we
i.e., when number of iteration is not fixed know how many times the loop is going to
execute.
Inititialization, condition and modification are Inititialization, condition and modification are
given at different places given at one place.

infinite loop: The loop which executes for infinite times is called infinite loop.
Empty loop : The loop which repeats for certain number of times but doesn’t contain any
statement in the loop. It is also called as delay loop.

You might also like