Java 1
Java 1
Introduction
Basics of class, objects, methods,
constructors
Assembly Programming
Send code instead of binary code.
Need assembler to convert to binary
Structured/Procedural
Use Subroutine/Function
Improving the clarity, quality, and development time
C, PASCAL
Historically, a program has been viewed as a logical procedure that takes input
data, processes it, and produces output data.
Java, C++, C#
Introduction to Java
L 1.5
According to Sun, 3 billion devices run java. There are
many devices where Java is currently used. Some of them
are as follows:
L 1.9
Java Platform Independent
Java is platform independent because it is different
from other languages like C, C++ etc. which are
compiled into platform specific machines while Java is
a write once, run anywhere language.
This code can be executed on any system that implements the Java
Virtual Machine
Using JDK you can compile and run java program from command line.
# c:>java HelloWorld
It runs java byte code on native machine
AN EXAMPLE HELLOWORLD
Names of constants
All are capital letters and separated by underscore.
Example: NAMES_OF_CONSTANTS
JAVA IDENTIFIERS RULES
Identifier is a name given to a variable, class, or method.
Java identifier
Can contain letter, number, underscore (_), or dollar sign ($).
Cannot start with number.
Identifiers are case sensitive
Have no maximum length.
Can not be a keyword, but it can contain a keyword as part of its name.
Data Types
L 1.14
Array Declaration
L 2.8
Array Creation
After declaration, no array actually exists.
In order to create an array, we use the new
operator:
type array-variable[];
array-variable = new type[size];
This creates a new array to hold size elements of
type type, which reference will be kept in the
variable array-variable.
L 2.9
Array Indexing
L 2.10
Array Initialization
L 2.11
Multidimensional Arrays
Multidimensional arrays are arrays of arrays:
1) declaration: int array[][];
2) creation: int array = new int[3][3];
3) initialization
int array[][] = {{1,2,3},{2,4,5},{4,4,5}};
L 2.12
Type Casting
L 3.9
CONTROL STATEMENT
if –else
switch
Loop
for
while
do-while
CONTROL STATEMENT
“Enhance for” or “for-each”
automatically cycles through an array in sequence from the
lowest index to the highest.
Syntax : for(type itr-var : collection) statement-block
Example:
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums)
sum += x;
Advantage: Avoid boundary error
CONTROL STATEMENT
break
Exits out of a loop or switch statement
Unlabeled break exits out of the innermost loop or switch
Use labeled break to exit out of nested loops or switch or block.
JUMP STATEMENT
public class BreakExample {
public static void main( String args[] ) {
for ( int row = 0; row < 5; row++ ) {
System.out.println("Outer loop: " + row);
for ( int column = 0; column < 4 ; column++ ) {
System.out.print(column +" " );
if ( ((row + column) % 2 ) == 0 ) {
System.out.println("Break " );
break;
}
}
}
}
}
JUMP STATEMENT
continue
A continue statement skips to the end of the current
loop's body.
The loop's boolean expression is then evaluated.
REFERENCE
Java- The Complete Reference Chapter 1-5
Simple Java Program
A class to display a simple message:
class MyProgram
{
public static void main(String[] args)
{
System.out.println(“First Java program.");
}
}
4.1
CLASSES
• Class is blue print or an idea of an Object
• From One class any number of Instances can be
created
• It is an encapsulation of attributes and methods
class
FIGURE
Ob1 Ob3
L 3.1
Class
A basis for the Java language.
Each concept we wish to describe in Java must be
included inside a class.
A class defines a new data type, whose values are
objects:
A class is a template for objects
An object is an instance of a class
L 4.6
syntax of CLASS
class <ClassName>
{
Attributes/Variables;
Constructors();
Methods();
}
L 3.2
INSTANCE
• Instance is an Object of a class which is an entity with
its own attribute values and methods.
• Creating an Instance
ClassName refVariable;
refVariable = new Constructor();
or
ClassName refVariable = new Constructor();
L 3.3
Class Definition
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
} L 4.8
}
Constructor
A constructor initializes the instance variables of an object.
It is called immediately after the object is created but before
the new operator completes.
1) it is syntactically similar to a method:
2) it has the same name as the name of its class
3) it is written without return type; the default
return type of a class
constructor is the same class
When the class has no constructor, the default constructor
automatically initializes all its instance variables with zero.
L 5.1
Example: Constructor
class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume() {
return width * height * depth;
}
}
L 5.2
Parameterized Constructor
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
} L 5.3
Methods
L 6.5
Keyword this
L 7.1
Example: Overloading
class OverloadDemo {
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a: " + a);
}
double test(double a)
{
System.out.println("double a: " + a); return a*a;
}
L 7.2
}
Constructor Overloading
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
Box() {
width = -1; height = -1; depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() { return width * height * depth; }
}
L 7.3