Sunteți pe pagina 1din 26

Java Tutorial

IT, 5th Semester


Books & References
 Booch, Jacobsobn, Rumbaugh, Object Oriented analysis and
design with application, Pearson.
 Bjarne Stroustrup, The C++ Programming Language, Addison
Wesley
 Robert Lafore, Object-Oriented Programming in C++, Sams
Publishing
 Stanley B. Lippman, Josée Lajoie, Barbara E. Moo C++ Primer,
 E . Balaguruswami, Object Oriented Programming C++. TMH.
 Steve Oualline, Practical C++ Programming O'Reilly &
Associates, Inc.
 Herbert Schildt, Java The Complete Reference, 9th Edition,
McGraw-Hill Osborne Media
Introduction
 C, a structured language, C++ super set of
C, previously named as C with classes.
 Java- true object oriented language
 int x; boolean z; - primitive data type.
 fruit mango; MotorCycle myMotor;
- mango is a variable of datatype fruit,
fruit is user defined data type called class
and mango is the variable called object.
Java…
 A general purpose, object-oriented programming
language, previously named as OAK, developed by
James Gosling and others at Sun Microsystems.
 Java is strongly typed like C++, meaning that a
Java compiler detects type inconsistencies at
compile-time.
 Two types of program styles
 Stand-alone application
 Web applets
 Java is useful mainly for internet programming.
In a Class
 Attributes: Properties of objects of a
class – colour, speed etc of MotorCycle.
 Behavior: Functions within the class
MotorCycle.
 In Java/OOP the functions are termed
as methods, they operate on the
variables of the class where the method
belongs.
Object Oriented Programming
 In OOP main entity is data.
 Programs are divided into objects.
 Objects are the basic runtime entity of
an Object Oriented Programming.
 Objects communicate with each other
without knowing the details of each
others data and code.
Message Communication

obj2
obj1

obj3 obj4

Network of objects communicating between them


Object Oriented Programming
Features
 Data encapsulation and abstraction
 Wrapping up data and method in a single class is
encapsulation, also called data hiding.
 Abstraction refers to the act of representing essential
features without including background details
 Inheritance
 Provides reusability feature
 Polymorphism
 Ability to take more than one form.
 Same external interface having different internal features,
extensively used to implement inheritance.
Java Features
 Compiled & Interpreted
 Platform-independent & Portable
 Object oriented
 Robust & Secure
 Multithreaded
 Distributed
 Dynamic
Compiled & Interpreted

Two stage process: Compiled to bytecode, JVM


generates machine dependent machine code
which executes the program.
Two ways of using Java
Java
source
code

Applet type Java compiler


Application type

Java enabled
Java Interpreter
Web browser

Output Output
Java Data and Variables
 There are 8 primitive data types
primitive type Wrapper type
byte Byte
short Short
int Int
long Long
float Float
double Double
char Character
boolean Boolean
Source Code ( demonstrating
declaration of a variable )
 class example
{
public static void main ( String[] args )
{
long x = 123;
//a declaration of a variable
//named x with a datatype of long

System.out.println("The variable x has value: " + x );


}
}
Data and Variables
 Character type data: 16 bit code, total memory is 216. This
16 bit code is called unicode.
 class X
{
public static void main ( String[] args )
{
char Ch1, Ch2;
Ch1 = 88; //ASCII value of X
Ch2 = ‘Y’;
System.out.println(“Ch1 and Ch2:” );
System.out.println(Ch1+ “ ” +Ch2);
}
}
 O/P Ch1 and Ch2: X Y
Data and Variables
 Java also supports octal and
hexadecimal number.
 01 -> octal no.
 0X/0x -> hexadecimal no.
 1234L -> long type integer
 12.3F -> float data type
Java variables
1.Instance variable: 3.Local variable:
class MotorCycle{ Variables declared within a class.

.
double a = 3.0, b = 4.0;
.
. /// dynamic initialization of c
} double c = Math.sqrt(a*a + b*b);

MotorCycle myMotor;
2.Class variable:
The variable which is common to all
the objects of a class.
Scope and lifetime of a variable

 Java variables can be declared anywhere


in the main method. When a block is a
declared new scope is created.
 Variables will be accessible within that
block where it is declared.
 For nested block variables are accessible
to inner block declared in outer block.
Array
 Group of like type variables referred by same name.
 type variable_name[];

 variable_name = new type[size];

 Example: int month[]; //a variable is created containing null


 month = new int[20]; //memory space is reserved now

. 2
Contains address of . .
1st memory, it acts as .
a pointer .
20
 Memory allocation in single line :
 int month = new int[12];
 int month[] = {31, 28, 30, … , 31};

 Two dimensional array:


 int twoD[][] = new int[4][5];

int twoD[][] = new int[4][5];


for(i = 0; i < 4; i++){
for(j = 0; j < i+1; j++){
twoD[i][j] = k; k++;
}
}
O/P
0
12
345
6789
Operators:
 Arithmetic operators
 Boolean Logical operators
 Bitwise operators
 Relational operators
Arithmetic operators
 op1 + op2 op1 added to op2
 op1 - op2 op2 subtracted from op1
 op1 * op2 op1 multiplied with op2
 op1 / op2 op1 divided by op2
 op1 % op2 Computes the remainder
of dividing op1 by op2
Boolean logical operator
 | the OR operator
 & the AND operator
 ^ the XOR operator
 ! the NOT operator
 || the short-circuit OR operator
 && the short-circuit AND operator
 == the EQUAL TO operator
 != the NOT EQUAL TO operator
 ? : ternary or if then else operator
Bitwise operator
 ~ Bitwise NOT  &= Bitwise AND assignment
 & Bitwise AND  |= Bitwise OR assignment
 | Bitwise OR  ^= Bitwise EXOR
 ^ Bitwise EXOR assignment
 >> shift right  >>== Shift right
 >>> shift right fill zero assignment
 << shift left  <<== Shift left assignment
Relational operator
 == the EQUAL TO operator
 != the NOT EQUAL TO operator
 > greater than
 < less than
 >= greater than or equal to
 <= less than or equal to
Example
int a = 32; a = a >> 2;
int a = -1; a = a>>> 24;
boolean a = true;
boolean b = false;
boolean c = (!a & b) | (a & !b)
boolean d = a ^ b;
Java Command Line Arguments
 Arguments are passed as a String array to the main method of a
class.
public class ReadArgs
{
public static final void main(String args[])
{
for (int i=0;i<args.length;++i)
{
System.out.println( args[i] );
}
}
}

The following command line arguments were passed:


arg[0]: zero
arg[1]: one
arg[2]: two
arg[3]: three

S-ar putea să vă placă și