SlideShare una empresa de Scribd logo
1 de 69
05/09/15 www.sunrays.co.in 1
Applied Core Java
JAVA BASICS
SUNRAYS Technologies
ABC square, Sambhaji square, Nigadi Pradhikaran,
PUNE-44, Maharashtra, INDIA Tel +91 77750-85756
212 President Tower, 6/2 South Tukogunj,
INDORE-1, MP, INDIA, Tel +91 731-4249244, Cell : +91-98273 60504
314, 3rd Floor, Saffron Complex, Fatehgunj,
VADODARA-2, Gujarat, INDIA Tel : +91 85111-41515
Email : hrd@sunrays.co.in URL : www.sunrays.co.in,
www.sunraystechnologies.com
Our Books – www.sunilbooks.com
05/09/15 www.sunrays.co.in 2
05/09/15 www.sunrays.co.in 3
PUNE | INDORE | VADODARA | RAIPUR
www.sunrays.co.in
hrd@sunrays.co.in
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
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.
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.
Expert Object
Each Object is an Expert object.
Expert object contains related variables and
functions.
05/09/15 www.sunrays.co.in 7
An Expert never overlaps responsibilities
05/09/15 www.sunrays.co.in 8
Creator
Preserver
Destroyer
Trimurti
Experts bring Modularity and Reusability
05/09/15 www.sunrays.co.in 9
05/09/15 www.sunrays.co.in 10
Object has State & Behavior
Object has state and behavior
State will be changed by behavior
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.
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
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
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.
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
05/09/15 www.sunrays.co.in 16
Program
Program Structure – Primitive Language
int i = 5 //global variable
void main(){
..
a(5);
}
void a(int k){
int j = 0; //local variable
..
}
05/09/15 www.sunrays.co.in 17
Primitive Language Library
Library
Program 1 Program 2
Program 3 Program 4
 Library is made of multiple reusable programs.
05/09/15 www.sunrays.co.in 18
Class
Program Structure - Java
int i = 5 //global variable
void main(){
..
a(5);
}
void a(int k){
int j = 0; //local variable
..
}
05/09/15 www.sunrays.co.in 19
Java Library - API
Package
Class 1 Class 2
Class 3 Class 4
 Package is made of related classes.
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
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.
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.
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.
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”.
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)
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
Java Platform
05/09/15 www.sunrays.co.in 27
JDK
JRE
Development Kit
javac.exe
JVM
(java.exe)
JAVA Class Libraries
AWT, I/O, Net etc.
Compile and Execute
05/09/15 www.sunrays.co.in 28
Hello.java
(text)
JVM (java.exe)
Hello.class
(bytecode)
Compile (javac)
• Compile .java file
• c:testjavac Hello.java
• It will generate file Hello.class
• Run the class by
• c:testjava Hello
Compile once run anywhere
05/09/15 www.sunrays.co.in 29
JVM
Linux
Hello.class
(bytecode)
JVM
MacOS
JVM
Windows
www.sunrays.co.in
Control Statement
if-else
while
for
do-while
GOTO
www.sunrays.co.in
While Loop
www.sunrays.co.in
While Loop
 public class HelloWhile {
 public static void main(String[] args) {
o boolean जबतकहेजान = true;
o int round = 0;
o while (जबतकहेजान ) {
 System.out.println(“मै बसंती नाचूंगी !!!");
 round++;
 if(round>500 )
• जबतकहेजान = false;
 }
 }
 }
www.sunrays.co.in
For Loop 5 shots for 10$
How Much?
Okay!!
 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
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 }
}
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 }
}
}
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);
}
}
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 }
}
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);
}
}
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
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));
05/09/15 www.sunrays.co.in 42
Java.lang.StringBuffer class
 public static void main(String[] args) {
 StringBuffer sb = new StringBuffer("Vijay");
 sb.append(“ Dinanth Chouhan");
 S.o.p("Length : " + sb.length());
 S.o.p("Capacity :" + sb.capacity());
 S.o.p("Char at :" + sb.charAt(1));
 S.o.p("Index Of : " + sb.indexOf("Dinanth"));
 S.o.p("Replace : " + sb.replace(0, 5, "Jay "));
 S.o.p("Reverse : " + sb.reverse());
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.
05/09/15 www.sunrays.co.in 44
java.lang.Math class
public static void main(String[] args) {
S.o.p(“ Mathematics functions");
S.o.p(" Max 2,5 - " + Math.max(2,5));
S.o.p(" Min 2,5 - " + Math.min(2,5));
S.o.p(" Absolute 3.7 - " + Math.abs(3.7));
S.o.p(" Exp 10 - " + Math.exp(10));
S.o.p(" Random Number- " + Math.random());
S.o.p(" Square Root- " + Math.sqrt(4));
}
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));
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.
05/09/15 www.sunrays.co.in 47
Hello <Name>
public class HelloName {
public static void main(String[] args) {
 System.out.println("Hello " + args[0]);
}
}
C:>java HelloName Vijay Dinanth Chohan
 class args[0] args[1] args[2]
C:>java HelloName “Vijay Dinanth” Chohan
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 }
 }
 }
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 }
}
}
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 }
}
}
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
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
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}
}
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
05/09/15 www.sunrays.co.in 55
Return a Value
 public static double getDivision(int a, int b)
o {
 double div = a / b;
 return div;
o }
 }
05/09/15 www.sunrays.co.in 56
Command line Menu
 public static void main(String[] args) throws Exception{
 int ch = System.in.read(); //Read data from keyboard
 S.o.p( "Selected char ASCII Code " + ch);
 if (ch == 'A' || ch == 'a') {
 Add.main(args);
o } else if (ch == 'D' || ch == 'd') {
 Division.main(args);
o } else {
 S.o.p("Incorrect Choice ");
o }
o }
 }
05/09/15 www.sunrays.co.in 57
10
One Dimension Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
length
int[] table = new int[10];
int a = table[4];
int a = table[2];
int size = table.length;
05/09/15 www.sunrays.co.in 58
10
Initialize an Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
length
int[] table = new int[10];
table[0] =2;
table[1] =4;
….
Or
int[] table =
{2,4,6,8,10,12,14,16,18,20}
;
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”}
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
05/09/15 www.sunrays.co.in 61
One Dimension Array
int[] table;
table = new
int[10];
table[0] =2;
table[1] =4;
4B
10
[0]
[1]
[9]
length
2
4
20
1000
1000
table
05/09/15 www.sunrays.co.in 62
10length
2D Array
[0]
20
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
…
[0] [1] [2] [7] [8]
9
9
..
9
9
9
9
9
05/09/15 www.sunrays.co.in 63
int[][] table = new int[10][9];
table
1010
1000
1000
1011
1111
1010
1011
1111
05/09/15 www.sunrays.co.in 64
Define an Array
 int[][] table = new int[10][9];
 table[1][5] = 5;
 int size = table.length;
 int size = table[0].length;
 int[][] rows = new int[10][];
 rows[0] = new int[9];
 rows[1] = new int[19];
 rows[2] = new int[29];
 int[][][] xyz = new int[10][9][2];
05/09/15 www.sunrays.co.in 65
3D Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
[0] [1] [2] [8] [9]
20
18
..
10
8
6
4
2
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
20
18
..
10
8
6
4
30
27
..
15
12
9
6
20
18
..
10
8
6
4
2
30
27
..
15
12
9
6
3
40
36
..
20
16
12
8
4
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
90
81
..
45
36
27
18
9
100
90
..
50
40
30
20
10
…
[0]
[1]
[2]
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
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
Example Source Code
05/09/15 www.sunrays.co.in 68
https://github.com/sunilbooks/SelfLearnJava
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”

Más contenido relacionado

La actualidad más candente

Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with javaHawkman Academy
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VIHari Christian
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
Java Programming and J2ME: The Basics
Java Programming and J2ME: The BasicsJava Programming and J2ME: The Basics
Java Programming and J2ME: The Basicstosine
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Java programming basics
Java programming basicsJava programming basics
Java programming basicsHamid Ghorbani
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenGraham Royce
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 

La actualidad más candente (19)

Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with java
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
Java for the Beginners
Java for the BeginnersJava for the Beginners
Java for the Beginners
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Java Basics
Java BasicsJava Basics
Java Basics
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Java Programming and J2ME: The Basics
Java Programming and J2ME: The BasicsJava Programming and J2ME: The Basics
Java Programming and J2ME: The Basics
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 

Destacado

basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 
Array in Java
Array in JavaArray in Java
Array in JavaAli shah
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of javavinay arora
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for DesignersR. Sosa
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basicsmhtspvtltd
 

Destacado (20)

basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Java cheat sheet
Java cheat sheet Java cheat sheet
Java cheat sheet
 
Basic Elements of Java
Basic Elements of JavaBasic Elements of Java
Basic Elements of Java
 
Java cheat sheet
Java cheat sheetJava cheat sheet
Java cheat sheet
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
PALASH SL GUPTA
PALASH SL GUPTAPALASH SL GUPTA
PALASH SL GUPTA
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
 
Java basics
Java basicsJava basics
Java basics
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Java basics
Java basicsJava basics
Java basics
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 

Similar a Java Basics

Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3Sunil OS
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingPurvik Rana
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaVíctor Leonel Orozco López
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Pritom Chaki
 
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguajeKotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguajeVíctor Leonel Orozco López
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
Topic2JavaBasics.ppt
Topic2JavaBasics.pptTopic2JavaBasics.ppt
Topic2JavaBasics.pptMENACE4
 
hallleuah_java.ppt
hallleuah_java.ppthallleuah_java.ppt
hallleuah_java.pptRahul201258
 

Similar a Java Basics (20)

Java Basics
Java BasicsJava Basics
Java Basics
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
lecture5
lecture5lecture5
lecture5
 
lecture5
lecture5lecture5
lecture5
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
 
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguajeKotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Topic2JavaBasics.ppt
Topic2JavaBasics.pptTopic2JavaBasics.ppt
Topic2JavaBasics.ppt
 
hallleuah_java.ppt
hallleuah_java.ppthallleuah_java.ppt
hallleuah_java.ppt
 
2.ppt
2.ppt2.ppt
2.ppt
 

Último

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 

Último (20)

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 

Java Basics

  • 1. 05/09/15 www.sunrays.co.in 1 Applied Core Java JAVA BASICS SUNRAYS Technologies ABC square, Sambhaji square, Nigadi Pradhikaran, PUNE-44, Maharashtra, INDIA Tel +91 77750-85756 212 President Tower, 6/2 South Tukogunj, INDORE-1, MP, INDIA, Tel +91 731-4249244, Cell : +91-98273 60504 314, 3rd Floor, Saffron Complex, Fatehgunj, VADODARA-2, Gujarat, INDIA Tel : +91 85111-41515 Email : hrd@sunrays.co.in URL : www.sunrays.co.in, www.sunraystechnologies.com
  • 2. Our Books – www.sunilbooks.com 05/09/15 www.sunrays.co.in 2
  • 3. 05/09/15 www.sunrays.co.in 3 PUNE | INDORE | VADODARA | RAIPUR www.sunrays.co.in hrd@sunrays.co.in
  • 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
  • 9. Experts bring Modularity and Reusability 05/09/15 www.sunrays.co.in 9
  • 10. 05/09/15 www.sunrays.co.in 10 Object has State & Behavior Object has state and behavior State will be changed by behavior
  • 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
  • 16. 05/09/15 www.sunrays.co.in 16 Program Program Structure – Primitive Language int i = 5 //global variable void main(){ .. a(5); } void a(int k){ int j = 0; //local variable .. }
  • 17. 05/09/15 www.sunrays.co.in 17 Primitive Language Library Library Program 1 Program 2 Program 3 Program 4  Library is made of multiple reusable programs.
  • 18. 05/09/15 www.sunrays.co.in 18 Class Program Structure - Java int i = 5 //global variable void main(){ .. a(5); } void a(int k){ int j = 0; //local variable .. }
  • 19. 05/09/15 www.sunrays.co.in 19 Java Library - API Package Class 1 Class 2 Class 3 Class 4  Package is made of related classes.
  • 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
  • 27. Java Platform 05/09/15 www.sunrays.co.in 27 JDK JRE Development Kit javac.exe JVM (java.exe) JAVA Class Libraries AWT, I/O, Net etc.
  • 28. Compile and Execute 05/09/15 www.sunrays.co.in 28 Hello.java (text) JVM (java.exe) Hello.class (bytecode) Compile (javac) • Compile .java file • c:testjavac Hello.java • It will generate file Hello.class • Run the class by • c:testjava Hello
  • 29. Compile once run anywhere 05/09/15 www.sunrays.co.in 29 JVM Linux Hello.class (bytecode) JVM MacOS JVM Windows
  • 32. www.sunrays.co.in While Loop  public class HelloWhile {  public static void main(String[] args) { o boolean जबतकहेजान = true; o int round = 0; o while (जबतकहेजान ) {  System.out.println(“मै बसंती नाचूंगी !!!");  round++;  if(round>500 ) • जबतकहेजान = false;  }  }  }
  • 33. www.sunrays.co.in For Loop 5 shots for 10$ How Much? Okay!!
  • 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));
  • 42. 05/09/15 www.sunrays.co.in 42 Java.lang.StringBuffer class  public static void main(String[] args) {  StringBuffer sb = new StringBuffer("Vijay");  sb.append(“ Dinanth Chouhan");  S.o.p("Length : " + sb.length());  S.o.p("Capacity :" + sb.capacity());  S.o.p("Char at :" + sb.charAt(1));  S.o.p("Index Of : " + sb.indexOf("Dinanth"));  S.o.p("Replace : " + sb.replace(0, 5, "Jay "));  S.o.p("Reverse : " + sb.reverse());
  • 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.
  • 44. 05/09/15 www.sunrays.co.in 44 java.lang.Math class public static void main(String[] args) { S.o.p(“ Mathematics functions"); S.o.p(" Max 2,5 - " + Math.max(2,5)); S.o.p(" Min 2,5 - " + Math.min(2,5)); S.o.p(" Absolute 3.7 - " + Math.abs(3.7)); S.o.p(" Exp 10 - " + Math.exp(10)); S.o.p(" Random Number- " + Math.random()); S.o.p(" Square Root- " + Math.sqrt(4)); }
  • 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.
  • 47. 05/09/15 www.sunrays.co.in 47 Hello <Name> public class HelloName { public static void main(String[] args) {  System.out.println("Hello " + args[0]); } } C:>java HelloName Vijay Dinanth Chohan  class args[0] args[1] args[2] C:>java HelloName “Vijay Dinanth” Chohan
  • 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
  • 55. 05/09/15 www.sunrays.co.in 55 Return a Value  public static double getDivision(int a, int b) o {  double div = a / b;  return div; o }  }
  • 56. 05/09/15 www.sunrays.co.in 56 Command line Menu  public static void main(String[] args) throws Exception{  int ch = System.in.read(); //Read data from keyboard  S.o.p( "Selected char ASCII Code " + ch);  if (ch == 'A' || ch == 'a') {  Add.main(args); o } else if (ch == 'D' || ch == 'd') {  Division.main(args); o } else {  S.o.p("Incorrect Choice "); o } o }  }
  • 57. 05/09/15 www.sunrays.co.in 57 10 One Dimension Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] length int[] table = new int[10]; int a = table[4]; int a = table[2]; int size = table.length;
  • 58. 05/09/15 www.sunrays.co.in 58 10 Initialize an Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] length int[] table = new int[10]; table[0] =2; table[1] =4; …. Or int[] table = {2,4,6,8,10,12,14,16,18,20} ;
  • 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
  • 61. 05/09/15 www.sunrays.co.in 61 One Dimension Array int[] table; table = new int[10]; table[0] =2; table[1] =4; 4B 10 [0] [1] [9] length 2 4 20 1000 1000 table
  • 62. 05/09/15 www.sunrays.co.in 62 10length 2D Array [0] 20 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 … [0] [1] [2] [7] [8] 9 9 .. 9 9 9 9 9
  • 63. 05/09/15 www.sunrays.co.in 63 int[][] table = new int[10][9]; table 1010 1000 1000 1011 1111 1010 1011 1111
  • 64. 05/09/15 www.sunrays.co.in 64 Define an Array  int[][] table = new int[10][9];  table[1][5] = 5;  int size = table.length;  int size = table[0].length;  int[][] rows = new int[10][];  rows[0] = new int[9];  rows[1] = new int[19];  rows[2] = new int[29];  int[][][] xyz = new int[10][9][2];
  • 65. 05/09/15 www.sunrays.co.in 65 3D Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 [0] [1] [2] [8] [9] 20 18 .. 10 8 6 4 2 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 20 18 .. 10 8 6 4 30 27 .. 15 12 9 6 20 18 .. 10 8 6 4 2 30 27 .. 15 12 9 6 3 40 36 .. 20 16 12 8 4 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 90 81 .. 45 36 27 18 9 100 90 .. 50 40 30 20 10 … [0] [1] [2]
  • 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
  • 68. Example Source Code 05/09/15 www.sunrays.co.in 68 https://github.com/sunilbooks/SelfLearnJava
  • 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”