4. 05/09/15 www.sunrays.co.in 4
Java is Programming Language
Java is a programing language.
just like any other primitive language such
as C, C++, Pascal.
It has
o Variables
o Functions
o Data Type
o Control Statement
o Arrays
5. 05/09/15 www.sunrays.co.in 5
Java is OOP
3 Idiot
Java is Object Oriented Programming .
follows OOP methodology.
Java thinks only Objects.
Meri
4 Lakh
ki watch
Just like a Money Oriented Person
who always thinks of Money.
6. 05/09/15 www.sunrays.co.in 6
Basic Unit of Java is Object
Such as program of
o sum of two number is an object
o Fibonacci Series is an object
o SMS Services is an object
o Email Services is an object
o Account Services is an object
Basic unit of Java is an Object.
7. Expert Object
Each Object is an Expert object.
Expert object contains related variables and
functions.
05/09/15 www.sunrays.co.in 7
8. An Expert never overlaps responsibilities
05/09/15 www.sunrays.co.in 8
Creator
Preserver
Destroyer
Trimurti
11. 05/09/15 www.sunrays.co.in 11
Object has State & Behavior
States are stored in memory variables.
Behavior changes states.
Behaviors are implemented by functions;
functions are referred as methods in OOP
Variables and Methods of an object are
defined by Class.
Class is the structure or skeleton of an
Object.
12. Class vs Objects
05/09/15 www.sunrays.co.in 12
Realization
Realization
State/Variables
currentGear
Speed
Color
Methods
changeGear()
Accelerator()
break()
changeColor()
State/Variables
name
address
Methods
changeName()
changeAddress()
Design
Real world entities based
on design
13. Class is the basic building block
The basic building block of Java is a
Class.
Java program is nothing but a class.
Java application is made of Classes.
05/09/15 www.sunrays.co.in 13
14. 05/09/15 www.sunrays.co.in 14
Class
Class contains methods and variables.
Variables contain values of type int,
float, boolean, char and String.
Methods perform operations.
15. Executable Class
An executable Class must have default
method ‘main’ .
Method main() is the entry point of a class.
main() is called by JVM at runtime.
05/09/15 www.sunrays.co.in 15
20. 05/09/15 www.sunrays.co.in 20
JAVA Application
ApplicationApplication
Package 1 Package 2
Package 3 Package 4
Application is made of multiple packages
21. 05/09/15 www.sunrays.co.in 21
Java Program is a Class
public class HelloJava {
…
}
A class may contain multiple variables and
methods.
A Class should have default ‘main’ method that is
called by JVM at the time of execution.
22. 05/09/15 www.sunrays.co.in 22
My First Program - HelloJava
public class HelloJava {
opublic static void main(String[] args) {
o String name =“Vijay”;
o System.out.println("Hello “ + name);
o}
}
public, class, static, and void are keywords.
Keywords are always written in small letters.
23. 05/09/15 www.sunrays.co.in 23
Keywords
class – is used to define a class.
public – Access modifier shows accessibility of a
class or variable or method to other Java classes.
There are 3 access modifiers public, protected and
private.
static – Memory for the static variables is
assigned only once in life. Non-static variables are
called instance variables.
void – is a NULL return type of main method.
24. 05/09/15 www.sunrays.co.in 24
Statements
System.out.println() method is used to write text at
standard output device – Console.
String is a data type.
Two strings are concatenated by + operator
o String name = “Vijay” ;
o “Hello” + name is equal to “Hello Vijay”.
25. 05/09/15 www.sunrays.co.in 25
JAVA_HOME
Java is installed at “C:Program FilesJavajdkx.x.”
It is known as JAVA_HOME
JAVA_HOMEbin folder contains
o javac.exe – Java Compiler
o java.exe – JVM ( Java Virtual Machine)
26. 05/09/15 www.sunrays.co.in 26
Compile Program
Open command prompt
Create c:sunrays
Change directory to c:sunrays
Compile program by
o javac HelloJava.java
o It will create HelloJava.class
Execute class file by
o java HelloJava
34. public class HelloFor {
public static void main(String[] args)
{
o for (int shot=1; shot <= 5; shot++)
o {
System.out.println(i+“Shot Balloon");
o }
o }
}
www.sunrays.co.in
For Loop – Five shots
35. 05/09/15 www.sunrays.co.in 35
Print Hello Java 5 times - for
public class HelloFor {
public static void main(String[] args) {
o for (int i = 0; i < 5; i++) {
System.out.println("Hello Java ");
o }
o }
}
36. 05/09/15 www.sunrays.co.in 36
Print Hello Java 5 times - while
public class HelloWhile {
public static void main(String[] args) {
o int i = 0;
o while (i < 5) {
System.out.println("Hello Java ");
i++; // i = i+1
o }
}
}
37. 05/09/15 www.sunrays.co.in 37
Print Hello Java 5 times – do-while
public class HelloDoWhile {
public static void main(String[] args) {
int i = 0;
o do {
System.out.println( i+ " Hello Java ");
i++;
o } while (i < 5);
}
}
38. 05/09/15 www.sunrays.co.in 38
Foreach statement
public class HelloFor {
public static void main(String[] args) {
o int[] table={ 2, 4, 6, 8, 10};
o for (int v : table) {
System.out.println(“Table “ + v);
o }
o }
}
39. 05/09/15 www.sunrays.co.in 39
Add.java
public class Add {
public static void main(String[] args) {
oint a = 5;
oint b = 10;
oint sum = a + b;
oSystem.out.println("Sum is " + sum);
}
}
40. 05/09/15 www.sunrays.co.in 40
Java Primitive Data Types
Primitive Data Types:
o boolean true or false
o char unicode (16 bits)
o byte signed 8 bit integer
o short signed 16 bit integer
o int signed 32 bit integer
o long signed 64 bit integer
o float,double IEEE 754 floating point
41. 05/09/15 www.sunrays.co.in 41
java.lang.String class
String name = "Vijay Dinanth Chouhan";
S.o.p(" String Length- " + name.length());
S.o.p(" 7 ths caharcter is- " + name.charAt(6));
S.o.p(" Dina index is- " + name.indexOf("Dina"));
S.o.p(" First i Position- " + name.indexOf("i"));
S.o.p(" Last i Position- " + name.lastIndexOf("i"));
S.o.p(" a is replaced by b- " + name.replace("a", "b"));
S.o.p(" All a is replaced by b- “ + name.replaceAll("a", "b"));
S.o.p(" Chota vijay- " + name.toLowerCase());
S.o.p(" Bada vijay- " + name.toUpperCase());
S.o.p(" Starts With Vijay- " + name.startsWith("Vijay"));
S.o.p(" Ends with han- " + name.endsWith("han"));
S.o.p(" Substring- " + name.substring(6));
43. 05/09/15 www.sunrays.co.in 43
String vs StringBuffer
String is immutable
o Memory object can not be changed.
StringBuffer is mutable
o Memory object can be changed.
45. 05/09/15 www.sunrays.co.in 45
Static vs Instance
String name = “Vijay”;
String surname = “Chohan”
S.o.p(name.length());
S.o.p(surname.length());
String.length()
S.o.p(Math.max(2,5));
S.o.p(Math.max(5,10));
46. 05/09/15 www.sunrays.co.in 46
Other Data Types
Reference types (composite)
o objects
o arrays
strings are supported by a built-in class
named String (java.lang.String).
string literals are supported by JAVA as a
special case.
48. 05/09/15 www.sunrays.co.in 48
Hello Name – if <condition>
public class HelloName1 {
public static void main(String[] args) {
o if (args.length == 1) {
System.out.println("Hello " + args[0]);
o } else {
System.out.println(“Parameter name is required");
o }
}
}
49. 05/09/15 www.sunrays.co.in 49
Hello All
public class HelloAll {
public static void main(String[] args) {
o for (int i = 0; i < args.length; i++) {
System.out.println(i + " = Hello " + args[i]);
o }
}
}
50. 05/09/15 www.sunrays.co.in 50
Hello All (Cond)
public static void main(String[] args) {
int size = args.length;
if (size == 0) {
o S.o.p("Usage : java HelloAll n1 n2 n3 .. ");
} else {
o for (int i = 0; i < size; i++) {
o S.o.p ( i+ " = Hello " + args[i]);
o }
}
}
51. 05/09/15 www.sunrays.co.in 51
Hello All - switch
public static void main(String[] args) {
int size = args.length;
switch(size) {
case 0 :S.o.p("Usage : java HelloAll1 n1 n2 n3..");
o break;
case 1 : S.o.p(“Hello “ + args[0]); break;
default :
o for (int i = 0; i < size; i++) {
S.o.p(i + " = Hello " + args[i]);
o }//for
}//switch
}//method
52. 05/09/15 www.sunrays.co.in 52
Add.java – Integer Arguments
public class Add {
public static void main(String[] args) {
oint a = Integer.parseInt(args[0]);
oint b = Integer.parseInt(args[1]);
oint sum = a + b;
oSystem.out.println("Sum is " + sum);
}
}
C:>java Add 10 20
53. 05/09/15 www.sunrays.co.in 53
Division
public class Division {
opublic static void main(String[] args) {
oint a = Integer.parseInt(args[0]);
oint b = Integer.parseInt(args[1]);
odouble div = a/b;
oS.o.p("Division is " + div);
o}
}
54. 05/09/15 www.sunrays.co.in 54
Define a Method
public static void main(String[] args) {
o printAll(args);
}// main
public static void printAll(String[] args) {
o for (int i = 0; i < args.length; i++) {
System.out.println(“Hello " + args[i]);
o }
}//printAll
59. 05/09/15 www.sunrays.co.in 59
Other Data Type Arrays
char[] chList = new char[5];
chList[0] = ‘A’….
o Or
char[] chList = {‘A’,’B’,’C’,’D’,’E’}
String[] strList = new String[5];
strList[0] = “A”
strList[1] = “Bee”
o Or
String[] strList = {“A”,”Bee”,”Cee”,”Dee”,”E”}
60. 05/09/15 www.sunrays.co.in 60
Copy an Array
public static void main(String[] args) {
o char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n',
'a', 't', 'e', 'd' };
o char[] copyTo = new char[7];
o System.arraycopy(copyFrom, 2,
o copyTo, 0, 7);
o S.o.p(new String(copyTo));
}
Start
Index
Start
Index
No Of
Element
66. 05/09/15 www.sunrays.co.in 66
java.util.Date class
import java.util.*;
public class TestDate {
public static void main(String[] args) {
o Date d = new Date();
o S.o.p("Date : " +d);
o S.o.p ("Long Time : " +d.getTime());
}
Output
o Date : Mon Jan 04 00:35:53 IST 2010
o Long Time : 1262545553156
67. 05/09/15 www.sunrays.co.in 67
Format a Date
import java.util.*; import java.text.SimpleDateFormat;
public class TestDateFormat{
public static void main(String[] args) {
o Date d = new Date();
o SimpleDateFormat format= new
SimpleDateFormat("dd/MM/yyyy");
o String str = format.format(d);
o S.o.p("Date : " + str );
o String str1 = "22/03/2009";
o Date d1 = format.parse(str1);
o S.o.p(d1);
}
Output
o String : 04/01/2010
o Sun Mar 22 00:00:00 IST 2009
69. Thank You
05/09/15 www.sunrays.co.in 69
If you have any questions feel free to contact us :
Email : hrd@sunrays.co.in
URL : www.sunrays.co.in
Next topic “Variables and Operators”