SlideShare a Scribd company logo
1 of 67
www.SunilOS.com 1
JAVA BASICS
www.sunilos.com
www.raystec.com
www.SunilOS.com 2
Java is a Programming Language
A programing language.
Write computer programs
Other programming language:
o C/ C++
o Python
o .Net
o PHP etc.
What is programming?
www.SunilOS.com 3
Video
Game
Calculator / Bill Payment
www.SunilOS.com 4
www.SunilOS.com 5
Basic elements of the Language
1. Command: an action
2. Variables/Attributes: memory to remember data
3. Data Type: number, decimal, character
4. Control Statement: conditional action
5. Functions/Methods: a task contains set of commands
www.SunilOS.com 6
Java is OOP
3 Idiot
Java is Object Oriented Programming .
follows OOP methodology.
Java thinks only Objects.
 Just like a Money Oriented Person
who always thinks of Money.
मेरी 4 लाख
की घडी!!
मेरी डेढ़ लाख
की शेरवानी!!
My 300$
shoes
www.SunilOS.com 7
Basic Unit of Java is Object
Program of
1. Print Hello World is an object
2. Sum of two numbers is an object
3. Fibonacci Series is an object
4. SMS Service is an object
5. Email Service is an object
6. Account Service is an object
Basic unit of Java is an Object.
OOP is real world programming.
Class: structure of an object
 What object will do? What are responsibilities of an object?
What tasks are done by Object?
 Class is the structure of an Object
 Class defines Role and Responsibilities of an object
 Ultimately a Program is a Class
 When memory is allocated to class, it will become object
www.SunilOS.com 8
Object Vs Class
www.SunilOS.com 9
State/Variables
• currentGear
• speed
• color
Methods
• changeGear()
• accelerator()
• break()
State
• name
• address
Methods
• changeName()
• earnMoney()
Structure
Real world entities
Object Vs Class
www.SunilOS.com 10
State/Variables
• balance
• owner
• type
Methods
• getBalance()
• fundTransfer ()
• payBill()
Real world entities
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.
www.SunilOS.com 11
www.SunilOS.com 12
Class
Class contains program elements
Class contains methods and variables.
Variables contain data
Types of data are int, float, boolean, char
and String.
Methods perform operations.
Executable Class
A Class is program
A Class can be executed
Executable Class must contain one method, names
‘main’
When you execute a class main() method is executed
www.SunilOS.com 13
www.SunilOS.com 14
HelloWorld – First Java Program
1. public class HelloWorld {
2. public static void main(String[] args) {
3. System.out.println(“Hello World”);
4. }
5. }
public, class, static, and void are keywords.
Keywords are always written in small letters.
www.SunilOS.com 15
Pizza
1. public class Pizza {
2. public static void main(String[] args) {
3. int money = 99;
4. if (money > 100) {
5. System.out.println("Wow!!, I can buy Pizza :)");
6. } else {
7. System.out.println("Oh!, I can not buy Pizza (:");
8. }
9. }
10. }
Sum of Two Numbers
public class Sum {
 public static void main(String[] args) {
 int a = 10;
 int b = 5;
 int c = a + b;
 System.out.println(c);
 }
}
www.SunilOS.com 16
Expert Class
Each class is an Expert class
An expert class contains related attributes and methods
www.SunilOS.com 17
Doctor Advocate Engineer Astronaut
Specialization -Inheritance
www.SunilOS.com 18
Doctor
Heart Specialist ENT Specialist Cancer Specialist
Expert never overlaps other’s responsibilities
www.SunilOS.com 19
Creator Preserver Destroyer
Trimurti
Expert Classes
www.SunilOS.com 20
:Automobile
-color :String
-speed:int
-make:String
+$NO_OF_GEARS
+getColor():String
+setColor()
+getMake():String
+setMake()
+break()
+changeGear()
+accelerator()
+getSpeed():int
:Person
-name:String
-dob : Date
-address:String
+$AVG_AGE
+getName():String
+setName()
+getAdress():String
+setAddress()
+getDob (): Date
+setDob ()
+getAge() : int
:Account
-number:String
-accountType : String
-balance:double
+getNumber():String
+setNumber()
+getAccountType():String
+setAccountType()
+deposit ()
+withdrawal ()
+getBalance():double
+fundTransfer()
+payBill()
Experts bring Modularity and Reusability
www.SunilOS.com 21
www.SunilOS.com 22
Class
Program Structure - Java
int i = 5 //global variable
void main(){
..
a(5);
}
void a(int k){
int j = 0; //local variable
..
}
www.SunilOS.com 23
Package – group of related classes
Package
Class 1 Class 2
Class 3 Class 4
www.SunilOS.com 24
JAVA Application
Application
Package 1 Package 2
Package 3 Package 4
 An application has multiple packages
Java Platform
www.SunilOS.com 25
JDK
JRE
Development Kit
javac.exe
JVM
(java.exe)
JAVA Class Libraries
AWT, I/O, Net etc.
Compile and Execute
www.SunilOS.com 26
Hello.java
(text)
JVM (java.exe)
Hello.class
(bytecode)
Compile (javac)
• Compile .java file
• c:>javac Hello.java
• It will generate file Hello.class
• Run the class by
• c:>java Hello
Compile once run anywhere
www.SunilOS.com 27
JVM
Linux
Hello.class
(bytecode)
JVM
MacOS
JVM
Windows
www.SunilOS.com
Control Statements
if-else
while
for
do-while
GOTO
28
www.SunilOS.com
While Loop
29
www.SunilOS.com
While Loop
 public class HelloWhile {
 public static void main(String[] args) {
o boolean जबतकहेजान = true;
o int round = 0;
o while (जबतकहेजान ) {
 System.out.println(“मै बसंती नाचंगी !!!");
 if(++round > 500 )
• जबतकहेजान = false;
o }
 }
 }
30
www.SunilOS.com
For Loop
₹10 for 5 shots
How Much?
Okay!!
31
 public class HelloFor {
 public static void main(String[] args)
 {
o for (int shot=1; shot <= 5; shot++)
o {
o System.out.println(“Shot Balloon” + i);
o }
o }
 }
www.SunilOS.com
For Loop – Five shots
32
www.SunilOS.com 33
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 }
}
www.SunilOS.com 34
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 }
}
}
www.SunilOS.com 35
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);
}
}
www.SunilOS.com 36
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 }
}
www.SunilOS.com 37
Add.java
public class Add {
public static void main(String[] args) {
o int a = 5;
o int b = 10;
o int sum = a + b;
o System.out.println("Sum is " + sum);
}
}
www.SunilOS.com 38
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
www.SunilOS.com 39
java.lang.String class
 String name = "Vijay Dinanath Chauhan";
 S.o.p(" String Length- " + name.length());
 S.o.p(" 7th character 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(“ Chhota 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));
 Note : S.o.p = System.out.println
www.SunilOS.com 40
Java.lang.StringBuffer class
 public static void main(String[] args) {
 StringBuffer sb = new StringBuffer("Vijay");
 sb.append(“ Dinanath Chauhan");
 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());
www.SunilOS.com 41
String vs StringBuffer
String is immutable
o Memory object can not be changed.
StringBuffer is mutable
o Memory object can be changed.
www.SunilOS.com 42
java.lang.Math class
 public static void main(String[] args) {
o S.o.p(“ Mathematics functions");
o S.o.p(" Max 2,5 - " + Math.max(2,5));
o S.o.p(" Min 2,5 - " + Math.min(2,5));
o S.o.p(" Absolute 3.7 - " + Math.abs(3.7));
o S.o.p(" Exp 10 - " + Math.exp(10));
o S.o.p(" Random Number- " + Math.random());
o S.o.p(" Square Root- " + Math.sqrt(4));
 }
 Note : S.o.p = System.out.println
www.SunilOS.com 43
Static vs Instance
String name = “Vijay”;
String surname = “Chauhan”
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));
www.SunilOS.com 44
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.
www.SunilOS.com 45
Hello <Name>
 public class HelloName {
o public static void main(String[] args) {
o System.out.println("Hello " + args[0]);
o }
 }
 C:>java HelloName Vijay Dinanath Chauhan
 class args[0] args[1] args[2]
 C:>java HelloName “Vijay Dinanath” Chauhan
www.SunilOS.com 46
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 }
 }
 }
www.SunilOS.com 47
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 }
}
}
www.SunilOS.com 48
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 }
 }
 }
www.SunilOS.com 49
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]);
o break;
 default :
o for (int i = 0; i < size; i++) {
 S.o.p(i + " = Hello " + args[i]);
o }//for
 }//switch
 }//method
www.SunilOS.com 50
Add.java – Integer Arguments
 public class Add {
 public static void main(String[] args) {
o int a = Integer.parseInt(args[0]);
o int b = Integer.parseInt(args[1]);
o int sum = a + b;
o System.out.println("Sum is " + sum);
 }
 }
 C:>java Add 10 20
www.SunilOS.com 51
Division
 public class Division {
o public static void main(String[] args) {
o int a = Integer.parseInt(args[0]);
o int b = Integer.parseInt(args[1]);
o double div = a/b;
o System.out.println("Division is " + div);
o }
 }
www.SunilOS.com 52
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
www.SunilOS.com 53
Return a Value
 public static double getDivision(int a, int b)
o {
 double div = a / b;
 return div;
o }
 }
www.SunilOS.com 54
Command line Menu
 public static void main(String[] args) throws Exception{
 int ch = System.in.read(); //Read data from keyboard
 System.out.println( "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 }
 }
www.SunilOS.com 55
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;
www.SunilOS.com 56
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};
www.SunilOS.com 57
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”}
www.SunilOS.com 58
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
www.SunilOS.com 59
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
www.SunilOS.com 60
10
length
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
www.SunilOS.com 61
int[][] table = new int[10][9];
table
1010
1000
1000
1011
1111
1010
1011
1111
www.SunilOS.com 62
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];
www.SunilOS.com 63
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]
www.SunilOS.com 64
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
www.SunilOS.com 65
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
Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 66
Thank You!
www.SunilOS.com 67
www.SunilOS.com

More Related Content

What's hot (20)

Hibernate
Hibernate Hibernate
Hibernate
 
Resource Bundle
Resource BundleResource Bundle
Resource Bundle
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Log4 J
Log4 JLog4 J
Log4 J
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java Basics
Java BasicsJava Basics
Java Basics
 
JavaScript
JavaScriptJavaScript
JavaScript
 
PDBC
PDBCPDBC
PDBC
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
C++
C++C++
C++
 
Machine learning ( Part 1 )
Machine learning ( Part 1 )Machine learning ( Part 1 )
Machine learning ( Part 1 )
 
Machine learning ( Part 3 )
Machine learning ( Part 3 )Machine learning ( Part 3 )
Machine learning ( Part 3 )
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
C++ oop
C++ oopC++ oop
C++ oop
 
DJango
DJangoDJango
DJango
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 

Similar to Java Basics V3

JavaProgrammingForBeginners-Presentation.pdf
JavaProgrammingForBeginners-Presentation.pdfJavaProgrammingForBeginners-Presentation.pdf
JavaProgrammingForBeginners-Presentation.pdf
Sathwika7
 

Similar to Java Basics V3 (20)

Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
JavaProgrammingForBeginners-Presentation.pdf
JavaProgrammingForBeginners-Presentation.pdfJavaProgrammingForBeginners-Presentation.pdf
JavaProgrammingForBeginners-Presentation.pdf
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
Core java
Core javaCore java
Core java
 
Java introduction
Java introductionJava introduction
Java introduction
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java
Java Java
Java
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
 
Dallas Scala Meetup
Dallas Scala MeetupDallas Scala Meetup
Dallas Scala Meetup
 
Java practical
Java practicalJava practical
Java practical
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 

More from Sunil OS (11)

OOP v3
OOP v3OOP v3
OOP v3
 
Threads v3
Threads v3Threads v3
Threads v3
 
Exception Handling v3
Exception Handling v3Exception Handling v3
Exception Handling v3
 
Machine learning ( Part 2 )
Machine learning ( Part 2 )Machine learning ( Part 2 )
Machine learning ( Part 2 )
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Angular 8
Angular 8 Angular 8
Angular 8
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Rays Technologies
Rays TechnologiesRays Technologies
Rays Technologies
 
C Basics
C BasicsC Basics
C Basics
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

Java Basics V3

  • 2. www.SunilOS.com 2 Java is a Programming Language A programing language. Write computer programs Other programming language: o C/ C++ o Python o .Net o PHP etc.
  • 4. Calculator / Bill Payment www.SunilOS.com 4
  • 5. www.SunilOS.com 5 Basic elements of the Language 1. Command: an action 2. Variables/Attributes: memory to remember data 3. Data Type: number, decimal, character 4. Control Statement: conditional action 5. Functions/Methods: a task contains set of commands
  • 6. www.SunilOS.com 6 Java is OOP 3 Idiot Java is Object Oriented Programming . follows OOP methodology. Java thinks only Objects.  Just like a Money Oriented Person who always thinks of Money. मेरी 4 लाख की घडी!! मेरी डेढ़ लाख की शेरवानी!! My 300$ shoes
  • 7. www.SunilOS.com 7 Basic Unit of Java is Object Program of 1. Print Hello World is an object 2. Sum of two numbers is an object 3. Fibonacci Series is an object 4. SMS Service is an object 5. Email Service is an object 6. Account Service is an object Basic unit of Java is an Object. OOP is real world programming.
  • 8. Class: structure of an object  What object will do? What are responsibilities of an object? What tasks are done by Object?  Class is the structure of an Object  Class defines Role and Responsibilities of an object  Ultimately a Program is a Class  When memory is allocated to class, it will become object www.SunilOS.com 8
  • 9. Object Vs Class www.SunilOS.com 9 State/Variables • currentGear • speed • color Methods • changeGear() • accelerator() • break() State • name • address Methods • changeName() • earnMoney() Structure Real world entities
  • 10. Object Vs Class www.SunilOS.com 10 State/Variables • balance • owner • type Methods • getBalance() • fundTransfer () • payBill() Real world entities
  • 11. 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. www.SunilOS.com 11
  • 12. www.SunilOS.com 12 Class Class contains program elements Class contains methods and variables. Variables contain data Types of data are int, float, boolean, char and String. Methods perform operations.
  • 13. Executable Class A Class is program A Class can be executed Executable Class must contain one method, names ‘main’ When you execute a class main() method is executed www.SunilOS.com 13
  • 14. www.SunilOS.com 14 HelloWorld – First Java Program 1. public class HelloWorld { 2. public static void main(String[] args) { 3. System.out.println(“Hello World”); 4. } 5. } public, class, static, and void are keywords. Keywords are always written in small letters.
  • 15. www.SunilOS.com 15 Pizza 1. public class Pizza { 2. public static void main(String[] args) { 3. int money = 99; 4. if (money > 100) { 5. System.out.println("Wow!!, I can buy Pizza :)"); 6. } else { 7. System.out.println("Oh!, I can not buy Pizza (:"); 8. } 9. } 10. }
  • 16. Sum of Two Numbers public class Sum {  public static void main(String[] args) {  int a = 10;  int b = 5;  int c = a + b;  System.out.println(c);  } } www.SunilOS.com 16
  • 17. Expert Class Each class is an Expert class An expert class contains related attributes and methods www.SunilOS.com 17 Doctor Advocate Engineer Astronaut
  • 18. Specialization -Inheritance www.SunilOS.com 18 Doctor Heart Specialist ENT Specialist Cancer Specialist
  • 19. Expert never overlaps other’s responsibilities www.SunilOS.com 19 Creator Preserver Destroyer Trimurti
  • 20. Expert Classes www.SunilOS.com 20 :Automobile -color :String -speed:int -make:String +$NO_OF_GEARS +getColor():String +setColor() +getMake():String +setMake() +break() +changeGear() +accelerator() +getSpeed():int :Person -name:String -dob : Date -address:String +$AVG_AGE +getName():String +setName() +getAdress():String +setAddress() +getDob (): Date +setDob () +getAge() : int :Account -number:String -accountType : String -balance:double +getNumber():String +setNumber() +getAccountType():String +setAccountType() +deposit () +withdrawal () +getBalance():double +fundTransfer() +payBill()
  • 21. Experts bring Modularity and Reusability www.SunilOS.com 21
  • 22. www.SunilOS.com 22 Class Program Structure - Java int i = 5 //global variable void main(){ .. a(5); } void a(int k){ int j = 0; //local variable .. }
  • 23. www.SunilOS.com 23 Package – group of related classes Package Class 1 Class 2 Class 3 Class 4
  • 24. www.SunilOS.com 24 JAVA Application Application Package 1 Package 2 Package 3 Package 4  An application has multiple packages
  • 25. Java Platform www.SunilOS.com 25 JDK JRE Development Kit javac.exe JVM (java.exe) JAVA Class Libraries AWT, I/O, Net etc.
  • 26. Compile and Execute www.SunilOS.com 26 Hello.java (text) JVM (java.exe) Hello.class (bytecode) Compile (javac) • Compile .java file • c:>javac Hello.java • It will generate file Hello.class • Run the class by • c:>java Hello
  • 27. Compile once run anywhere www.SunilOS.com 27 JVM Linux Hello.class (bytecode) JVM MacOS JVM Windows
  • 30. www.SunilOS.com While Loop  public class HelloWhile {  public static void main(String[] args) { o boolean जबतकहेजान = true; o int round = 0; o while (जबतकहेजान ) {  System.out.println(“मै बसंती नाचंगी !!!");  if(++round > 500 ) • जबतकहेजान = false; o }  }  } 30
  • 31. www.SunilOS.com For Loop ₹10 for 5 shots How Much? Okay!! 31
  • 32.  public class HelloFor {  public static void main(String[] args)  { o for (int shot=1; shot <= 5; shot++) o { o System.out.println(“Shot Balloon” + i); o } o }  } www.SunilOS.com For Loop – Five shots 32
  • 33. www.SunilOS.com 33 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 } }
  • 34. www.SunilOS.com 34 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 } } }
  • 35. www.SunilOS.com 35 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); } }
  • 36. www.SunilOS.com 36 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 } }
  • 37. www.SunilOS.com 37 Add.java public class Add { public static void main(String[] args) { o int a = 5; o int b = 10; o int sum = a + b; o System.out.println("Sum is " + sum); } }
  • 38. www.SunilOS.com 38 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
  • 39. www.SunilOS.com 39 java.lang.String class  String name = "Vijay Dinanath Chauhan";  S.o.p(" String Length- " + name.length());  S.o.p(" 7th character 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(“ Chhota 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));  Note : S.o.p = System.out.println
  • 40. www.SunilOS.com 40 Java.lang.StringBuffer class  public static void main(String[] args) {  StringBuffer sb = new StringBuffer("Vijay");  sb.append(“ Dinanath Chauhan");  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());
  • 41. www.SunilOS.com 41 String vs StringBuffer String is immutable o Memory object can not be changed. StringBuffer is mutable o Memory object can be changed.
  • 42. www.SunilOS.com 42 java.lang.Math class  public static void main(String[] args) { o S.o.p(“ Mathematics functions"); o S.o.p(" Max 2,5 - " + Math.max(2,5)); o S.o.p(" Min 2,5 - " + Math.min(2,5)); o S.o.p(" Absolute 3.7 - " + Math.abs(3.7)); o S.o.p(" Exp 10 - " + Math.exp(10)); o S.o.p(" Random Number- " + Math.random()); o S.o.p(" Square Root- " + Math.sqrt(4));  }  Note : S.o.p = System.out.println
  • 43. www.SunilOS.com 43 Static vs Instance String name = “Vijay”; String surname = “Chauhan” 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));
  • 44. www.SunilOS.com 44 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.
  • 45. www.SunilOS.com 45 Hello <Name>  public class HelloName { o public static void main(String[] args) { o System.out.println("Hello " + args[0]); o }  }  C:>java HelloName Vijay Dinanath Chauhan  class args[0] args[1] args[2]  C:>java HelloName “Vijay Dinanath” Chauhan
  • 46. www.SunilOS.com 46 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 }  }  }
  • 47. www.SunilOS.com 47 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 } } }
  • 48. www.SunilOS.com 48 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 }  }  }
  • 49. www.SunilOS.com 49 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]); o break;  default : o for (int i = 0; i < size; i++) {  S.o.p(i + " = Hello " + args[i]); o }//for  }//switch  }//method
  • 50. www.SunilOS.com 50 Add.java – Integer Arguments  public class Add {  public static void main(String[] args) { o int a = Integer.parseInt(args[0]); o int b = Integer.parseInt(args[1]); o int sum = a + b; o System.out.println("Sum is " + sum);  }  }  C:>java Add 10 20
  • 51. www.SunilOS.com 51 Division  public class Division { o public static void main(String[] args) { o int a = Integer.parseInt(args[0]); o int b = Integer.parseInt(args[1]); o double div = a/b; o System.out.println("Division is " + div); o }  }
  • 52. www.SunilOS.com 52 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
  • 53. www.SunilOS.com 53 Return a Value  public static double getDivision(int a, int b) o {  double div = a / b;  return div; o }  }
  • 54. www.SunilOS.com 54 Command line Menu  public static void main(String[] args) throws Exception{  int ch = System.in.read(); //Read data from keyboard  System.out.println( "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 }  }
  • 55. www.SunilOS.com 55 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;
  • 56. www.SunilOS.com 56 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};
  • 57. www.SunilOS.com 57 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”}
  • 58. www.SunilOS.com 58 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
  • 59. www.SunilOS.com 59 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
  • 61. www.SunilOS.com 61 int[][] table = new int[10][9]; table 1010 1000 1000 1011 1111 1010 1011 1111
  • 62. www.SunilOS.com 62 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];
  • 63. www.SunilOS.com 63 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]
  • 64. www.SunilOS.com 64 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
  • 65. www.SunilOS.com 65 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
  • 66. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 66