PROGRAMMING USING JAVA
UNIT – I
CONTENTS
• The Genesis (Origin) of Java
• How java changed the internet
• Java‘s magic: Byte Code , JVM
• Java Buzz words - Simple, Object Oriented, Robust, Multi threaded,
Architecture-Neutral, Interpreted and high performance, Distributed,
Dynamic
• Evolution of Java - Introduction to Object Oriented Concepts of Java
• Lab1: Learning to work with Java IDE and Writing Simple Conversion
Programs
• Encapsulation, Polymorphism, Inheritance
• Lexical Issues of Java
• White spaces, Identifiers, Literals Comments, Separators ,Keywords
• Introduction to Datatypes of Java - Byte, short, int, long, float, double, chars, Boolean
• What is variable? Declaring a variable, dynamic initialization of variables - Scope and
life time of variables
• Lab2: Operators
• Introduction to Operators
• Working with Arithmetic, Relational, Logical, Bit wise, Conditional, Assignment
operators
• What is Array?, Initialization of Arrays
• Understanding Types of Arrays
• Introduction to Control Statements - Working with Selection Statements-All forms of if
& Switch
• Introduction to Iterative Statements
• Working with while , do-while, for, for each statements
• Introduction to Jump Statements – Working with break, continue and return
Statements
• Laboratory 3: Arrays, Control Statements
INTRODUCTION TO JAVA PROGRAMMING
Genesis of Java
•Java is an Object Oriented, Internet based ,
Multithreaded Programming language.
•Object Oriented
•Internet based (web enabled)
•Multithreaded
Genesis & History of Java
• Java is a general purpose Object Oriented Programming Lanaguage
• Invented by James Gosling
• Developed by Sun Micro Systems
• Initially called as “Oak” , but renamed as “JAVA” in 1995.
• Initially it was developed as Platform Independent language
• Later it became as a language of Internet reflects portability and
Security.
Source code Byte code Machine code
Impact of JAVA over Internet
• Java is associated with Internet
• Java is used for the web development and also for the world wide
communications
• Java applets are execute using hotjava web browser
• Applet is a small window based application which runs in the web
browser.
• Java is facilitated with inbuilt applet viewer and browser
Evolution of Java
• Java programming supports some concepts of
• C Programming Language
• OOPs concept of C++
Introduction to Object Oriented Concepts
• The Java Programming Language is an Object Oriented and Internet
base language. It supports the OOP concepts.
• OOPS concepts of JAVA are:
1) Class
2) Object
3) Data Abstraction and Encapsulation
3) Polymorphism / Method Over Loading
4) Inheritance
5) Method Overriding
1. Class
• Definition : Example:
• A class is a like a container which contains Member-Variables and class sample
Member-Functions. It encapsulates the members in a single unit
called class. {
• Collection of objects is called class. It is a logical entity.
int a=10;
void display()
• Structure of a Class
{
Class Class-name System.out.println(“A=“ + a);
Data types / }
Member variables }
Member-functions
• General format/ Syntax:
Class Class-Name
{
Data types / Variables
Functions
}
2. Object
• Definition :
• In general, an object is an entity.
• Any entity that has state and behaviour is known as an object.
• For example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical.
* In case of OOP, An object is an Instance.
• It is used to access the members of the class for which an object is created.
• An object can be created using ‘new’ operator / keyword.
• Syntax:
• Class-name Object-name= new Class-name();
• Example:
• Sample S = new Sample();
• Here ‘S’ is an Object for the class “Sample”
3.Inheritance
Syntax:
• Definition Class Super-class
• Inheritance is acquiring the properties of one {
Members1
class to another class. }
• Sharing properties among or more classes. Class Sub-Class extends Super-class
{
• It provides code reusability. Members2
• In Java, the “extends” keyword is used to extend }
the properties from one class to another class.
• Types:
1) Single Inheritance
2) Multiple Inheritance
3) Multi Level Inheritance
4) Hybrid
Example Program:
import java.io.*; public class Inherit
class Super {
public static void main(String args[])
{
{
void display1() Sub S=new Sub();
{ S.display1();
System.out.println("I am from Super class"); S.display2();
} }
}
}
class Sub extends Super OUTPUT:
{
void display2()
{
System.out.println("I am from Sub class");
}
}
4.Polymorphism / Method Over Loading
• Polymorphism is a process of taking many forms.
Method over loading
• It is also called as Method Over loading. Void display( )
• Method Over loading is same name of a method is Void display(int a)
used for more than one time with different
arguments. Void display(float a)
• Example: Void display(int a, int b)
Class c1 Void display(int a, float b)
{ Void display(int a), int b, int c)
Void display ()
{
System.out.println(“Hi..”); Method overriding
} Void display( )
Void display(int a)
{ Void display( )
int x=a; Void display( )
System.out.println(“X=“, x);
}
Void display(int a)
Void display(int a)
// Method Overloading {
import java.io.*; length=width=a;
class Room int area1=length*width;
{ System.out.println("Area="+area1);
int length,width; }
Room(int x, int y) }
{ class methodoverloading
length=x; {
width=y; public static void main(String args[])
int area=length*width; {
System.out.println("Area="+area); Room r1=new Room(10,5); OUTPUT
} Room r2=new Room(50);
}
Room(int a) }
5. Abstraction
• 5. Abstraction
*Hiding internal details and showing functionality is known as abstraction. For
example phone call, we don't know the internal processing.
6. Encapsulation
• Encapsulation is a process of wrapping up of data and functions together in a
class. It provides an abstract view and security of data.
• A java class is the example of encapsulation. Java bean is the fully
encapsulated class because all the data members are private here.
5.Method Overriding
• Method Over loading is same name of a method is
used for more than one time with same number of
argument and also same data types.
• Example:
Method overriding
Void display( )
Void display( )
Void display(int a)
Void display(int a)
Lab1: Simple java programs
import java.io.*;
class Sample
{
public static void main(String args[])
{
System.out.println("DEPARTMENT OF COMPUTER SCIENCE");
System.out.println(" Hi Welcome to JAVA World");
OUTPUT
}
}
Lexical issues in java / Java Tokens
• The Lexical issues in java are the usage of the statements or instructions
with the combination of the following.
• Whitespace.
• Identifiers.
• Literals.
• Comments.
• Separators.
• Java Tokens
• Java Tokens are the smallest units or components of Java Program.
• A Java program is developed with set of tokens / words / codes.
• The Java compiler breaks the line of code into text (words) is called Java tokens.
• Token = identifier | keyword | separator | operator | literal | comment
• Example of Tokens
public class TokensDemo
{
public static void main(String args[])
{
System.out.println(“Java Programming");
}
}
• In the above example, public, class, Demo, {, static, void, main, (, String,
args, [, ], ), System, ., out, Jav, Programming, etc. are the Java tokens.
Types of Tokens
• Java token includes the following: 01. abstract 02. boolean 03. byte 04. break 05. class
06. case 07. catch 08. char 09. 10. default
a) Keywords continue
b) Identifiers
c) Literals 11. do 12. double 13. else 14. extends 15. final
d) Operators 16. finally 17. float 18. for 19. if 20.
e) Separators implements
f) Comments
21. import 22. 23. int 24. 25. long
a)Keywords: These are the pre-defined reserved instanceof interface
words of any programming language.
Each keyword has a special meaning. It is always 26. native 27. new 28. 29. private 30.
written in lower case. Java provides the package protected
following keywords: 31. public 32. return 33. short 34. static 35. super
36. switch 37. 38. this 39. thro 40. throws
synchroniz
ed
41. 42. try 43. void 44. volatile 45. while
transient
46. assert 47. const 48. enum 49. goto 50. strictfp
• B. Identifier:
• Identifiers are used to name a variable, constant, function, class, and array.
• It is created by the user.
• It can be a combination of alphabets, numbers and under score.
• Rules:
• The first letter of an identifier must be a letter, underscore or a dollar sign.
It cannot start with digits but may contain digits.
• The whitespace cannot be included in the identifier.
• Identifiers are case sensitive.
• Example:
• a,b,c
• a1
• age
• gender
• name
• mark_1
• mark_2
C.Literals:
- Literal is constant / fixed value (constant) in the source code.
- Categories of Literals are :
* integer literal
* string literal
* Boolean literal, etc. It is defined by the programmer. Once it has been defined cannot
be changed.
- Java provides five types of literals are as follows:
• Integer
• Floating Point
• Character Literal Type
• String
• Boolean 23 int
Example: 9.86 double
false, true boolean
'K', '7', '-' char
"javatpoint" String
null any reference type
D.Operators: In programming, operators are the special symbol that
tells the compiler to perform a special operation.
• Java provides different types of operators.
• There are eight types of operators in Java, are as follows:
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Unary Operators
• Logical Operators
• Ternary Operators
• Bitwise Operators
• Shift Operators
Operator Symbols Example
Arithmetic +,-,/,*,% a+b, a-b, a/b ,a*b ,a%2
Unary ++ , - - , ! ++a, --b, !a
Assignment = , += , -= , *= , /= , %= , ^= A=10, a+=10, b-=20, c*=5, etc.
Relational ==, != , < , >, <= , >= a==b, a!=b, a<b, a>b, A<=b, c>=d
Logical && , || Mark1 && mark2 , Mark1 || Mark2
Ternary (Condition) ? (Statement1) : X=(A>B) ? A : B;
(Statement2);
Bitwise &,|,^,~ a&b, a|b a^2
Shift << , >> , >>> A>>
E. Separators:
*The separators in Java is also known as punctuators.
• The separators are:
• Square Brackets []: It is used to define array elements. A pair of square
brackets represents the single-dimensional array, two pairs of square brackets
represent the two-dimensional array.
• Parentheses (): It is used to call the functions and parsing the parameters.
• Curly Braces {}: The curly braces denote the starting and ending of a code
block.
• Comma (,): It is used to separate two values, statements, and parameters.
• Assignment Operator (=): It is used to assign a variable and constant.
• Semicolon (;): It is the symbol that can be found at end of the statements. It
separates the two statements.
• Period (.): It separates the package name form the sub-packages and class. It
also separates a variable or method from a reference variable.
• Comments: Comments allow us to specify information about
the program inside our Java code.
• Java compiler recognizes these comments as tokens but
excludes it from further processing.
• The Java compiler treats comments as whitespaces. Java
provides the following two types of comments:
• Single Comment line: It begins with a pair of forwarding
slashes (//).
• Multiple Comment line:
/* Multiple line are indicated
with this symbol */
Lab 2: Operators System.out.printl(“OPERATORS”);
import java.io.*; System.out.printl(“Addition:” + add);
class operatorsdemo System.out.printl(“Subtraction:” + sub);
{ System.out.printl(“Multiplication:” + mul);
Public static void main(String args[]) System.out.printl(“Division”+ div);
{ }
Int a=100,b=200,add, sub, mul,div; }
System.out.printl(“OPERATORS”);
add=a+b; OUTPUT
sub=a-b;
mul=a*b;
div=a/b;