SlideShare ist ein Scribd-Unternehmen logo
1 von 173
The FREE Java Course
that doesn’t SUCK
Part 1
Agenda
1. Memory Organization
2. Data Hierarchy
3. Types of programming languages
4. Hello World
5. Steps to Run A Java Program
6. Type Conversions
Agenda
7. Operators
1. Arithmetic
2. Relational
3. Assignment
4. Increment/Decrement
5. Logical
Agenda
8. If
9. Ternary Operator
10. for
11. while
12. do while
13. break
14. continue
Agenda
15. Strings
16. String Builder & Conversions
17. Methods & Types of methods
18. Method Overloading
19. Variable Scope
20. Arrays
Where do I watch these videos?
coursetro.com
Mouse
Display
Printer
Keyboard
Main
Memory
Secondary
Storage
CPU
Control Unit
ALU
CPU Storage
Input
Devices
Output
Devices
0
0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0
1
V i v z
Vivz 26
Vivz 26
Gary 32
Anky 26
Bit
Unicode
Letter V
Field
File
Record
or
coursetro.com
+1300042774
+1400593419
+1200274027
load pay
add overtime
store total
total = pay + overtime
Machine Level Assembly Level High Level
coursetro.com
Install Java JDK
On Windows
Install Java JDK
On Mac
Install IntelliJ On
Windows
Install IntelliJ On
Mac
Relevant
Videos
Click To Watch Videos Below
Google	It!
• ASCII table
• unicode table
• ASCII vs Unicode
• utf-8 means
• utf-8 vs utf-16
• machine level language vs assembly language
Hello World
coursetro.com
“Hello World”
coursetro.com
println(“Hello World”);
coursetro.com
System.out.println(“Hello World”);
coursetro.com
coursetro.com
coursetro.com
1. println in Java
2. system.out.println
3. java system class
4. java String
5. java main method
6. string args in java
7. void in java
Google	It!
5 steps to
run a Java
program
Create
Compile
LoadVerify
Run
Create
Editor Disk
coursetro.com
Compile
Compiler Disk
javac HelloWorld.java -> HelloWorld.class
coursetro.com
Load
Class Loader
Disk
Primary
Memory
java HelloWorld
coursetro.com
Verify
Bytecode Verifier
Primary
Memory
coursetro.com
Run
Java Virtual
Machine (JVM)
Primary
Memory
coursetro.com
Memory
Organization And
Data Hierarchy
Hello World
Explained
Hello World
Example
Steps To Run A
Java Program
Relevant
Videos
Click To Watch Videos Below
1. list of java editors
2. javac command
3. java command
4. java compiler vs interpreter
5. java classloader
6. java bytecode verifier
7. jvm in java
8. jvm vs jre
9. jvm vs jre vs jdk vs jit
Google	It!
What is a variable?
Storage location in a computer program. It has a name and
value. Assign a value to it any number of times.
coursetro.com
byte
short
int
long
2 bytes1 byte 4 bytes 8 bytes
boolean
char
float
double
2 bytesNot precisely defined 4 bytes 8 bytes
byte myByte = 100;
short myShort = 1000;
int myInt = 100000;
long myLong = 1000000;
coursetro.com
float myFloat = 125.124f;
double myDouble = 125.214545544;
char myChar = ‘C’;
boolean condition = true
coursetro.com
What is a constant?
It has a name and a value. You must assign the value while
creating it and cannot change it afterwards.
coursetro.com
Example
final double PI = 3.14159625;
coursetro.com
1. Data types in java
2. java constants
3. java final variable
Google	It!
Type Conversion
A small box can be fitted into a bigger box but vice versa is tricky.
Implicit Type Conversion
long
long
int
int
Explicit Type Conversion
Type Conversion
int earthRad = 6371;
long radius = earthRad;
long
int
coursetro.com
Type Conversion
long galaxyRad= 1000000000000000000L;
int radius = (int) galaxyRad;
long
int
coursetro.com
Variables And
Constants
Explained
Variables And
Constants
Example
Typecasting
Explained
Typecasting
Example
Relevant
Videos
Click To Watch Videos Below
Arithmetic Operators
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
coursetro.com
Modulus Operator
• It gives you the remainder between 2 numbers.
5 % 2 = 1
2 * 2 = 4
• 3 % 4 = 3
21 43 5
coursetro.com
Expression
• Expression in Algebra
1 + 2 + 3 + 4+ 5
= 3
5
• Expression in Java
(1 + 2 + 3 + 4+ 5) / 5 = 3
coursetro.com
Operator Precedence
In Java Arithmetic Operators are evaluated in the
following order from first to last
Multiplicative
* / %
Additive
+ -
Example:
3 * 41 + 7 / 3 – 2 % 1 = ((3 * 41) + (7 / 3)) – (2 % 1) = 125.33
coursetro.com
1. explicit typecasting in java
2. implicit typecasting in java
3. integer division in java
4. modulus operator can be applied to which of the following
5. java expression example
6. precedence of operators in java
7. associativity of operators in java
Google	It!
Greater Than
boolean result = 5 > 4; //Contains true
int numOne = 5;
int numTwo = 4;
boolean result = numOne > numTwo; //Contains true
coursetro.com
Less Than
boolean result = 5 < 4; //Contains false
int numOne = 5;
int numTwo = 4;
boolean result = numOne < numTwo; //Contains false
coursetro.com
Greater Than Or Equal To
boolean result = 5 >= 4; //Contains true
boolean result = 5 >= 5; //Contains true
int numOne = 5;
int numTwo = 4;
boolean result = numOne >= numTwo; //Contains true
coursetro.com
Less Than Or Equal To
boolean result = 5 <= 4; //Contains false
boolean result = 5 <= 5; //Contains true
int numOne = 5;
int numTwo = 4;
boolean result = numOne <= numTwo; //Contains false
coursetro.com
Equals To
boolean result = 5 == 4; //Contains false
boolean result = 5 == 5; //Contains true
int numOne = 5;
int numTwo = 5;
boolean result = numOne == numTwo; //Contains true
coursetro.com
Not Equals To
boolean result = 5 != 4; //Contains true
boolean result = 5 != 5; //Contains false
int numOne = 5;
int numTwo = 5;
boolean result = numOne != numTwo; //Contains false
coursetro.com
Arithmetic
Operators
Explained
Arithmetic
Operators
Example
Relational
Operators
Explained
Relational
Operators
Example
Relevant
Videos
Click To Watch Videos Below
Assignment
Int result = 95; //Right to Left
int numOne = 5 , numTwo = 4;
coursetro.com
+=
int numOne = 5;
int numTwo = 4;
//numOne = numOne + numTwo;
numOne += numTwo; //Contains 9
coursetro.com
-=
int numOne = 5;
int numTwo = 4;
//numOne = numOne - numTwo;
numOne -= numTwo; //Contains 1
coursetro.com
*=
int numOne = 5;
int numTwo = 4;
//numOne = numOne * numTwo;
numOne *= numTwo; //Contains 20
coursetro.com
/=
int numOne = 9;
int numTwo = 2;
//numOne = numOne / numTwo;
numOne /= numTwo; //Contains 4
coursetro.com
%=
int numOne = 5;
int numTwo = 4;
//numOne = numOne % numTwo;
numOne %= numTwo; //Contains 1
coursetro.com
Increment
int year = 2050;
newYear = year++;
//Contains 2050
newYear = year;
year = year + 1;
int year = 2050;
newYear = ++year;
//Contains 2051
year = year + 1;
newYear = year;
coursetro.com
Decrement
int year = 2050;
newYear = year--;
//Contains 2050
newYear = year;
year = year - 1;
int year = 2050;
newYear = --year;
//Contains 2049
year = year - 1;
newYear = year;
coursetro.com
!
A !A
true false
false true
boolean engaged = false;
boolean notEngaged = !engaged;
coursetro.com
&&
A B A && B
true true true
true false false
false true false
false false false
boolean dcFan = true, marvelFan = true;
if(dcFan && marvelFan){
System.out.println(“You like superheroes…”);
}
||
A B A || B
true true true
True false true
False true true
false false false
boolean dcFan = false, marvelFan = false;
if(dcFan || marvelFan){
System.out.println(“You like some superhero, don’t you?”);
}
else{
System.out.println(“I guess not!”);
}
Assignment
Operators Explained
Assignment
Operators Example
Increment
Decrement And
Logical Operators
Explained
Increment
Decrement And
Logical Operators
Example
Relevant
Videos
Click To Watch Videos Below
if
If you get 90% or more this year I will get
you a bike.
if ( marks >= 90 ) {
System.out.println(“You get a bike”);
}
coursetro.com
if
If you get more than 80% but less than
90% you get a phone.
if (marks >= 80 && marks < 90) {
System.out.println(“You get a phone”);
}
coursetro.com
If else
If you pass, you get a chocolate
Else no video games.
if ( marks >= 40 ) {
System.out.println(“Chocolate yay!”);
} else {
System.out.println(“No video games”);
}
coursetro.com
If else ladder
If marks greater than 90%, you get a bike,
if marks are between 80-90% you get a
phone, otherwise you get a chocolate, if
you failed, then no video games
if ( marks >= 90 ) {
System.out.println(“You get a bike”);
}
else if (marks >=80){
System.out.println(“You get a phone”);
}
else if (marks>=40){
System.out.println(“Chocolate yay!”);
}
else {
System.out.println(“No video games”);
}
coursetro.com
Nested if else
if marks greater than 90 and physical
fitness examination score greater than 80
you get a car else you get a bike
If marks less than 90 go to the gym
if ( marks >= 90 ) {
if (score>=80){
System.out.println(“You get a car”);
}
else {
System.out.println(“You get a bike!”);
}
}
else {
System.out.println(“Go to the gym”);
}
coursetro.com
1. assignment vs equality operator in java
2. == vs = in java
3. java logical operators
4. dangling else problem in java
5. semicolon after if statement in java
6. short circuit evaluation in java
Google	It!
Ternary Operator
result = some condition ? An expression
that returns some value if true : An
expression that returns some value if it is
false;
coursetro.com
Ternary Operator
int marks = 91;
char grade = marks >= 90 ? ‘A’ : ‘B’;
If Else Explained If Else Example
Ternary
Explained
Ternary Example
Relevant
Videos
Click To Watch Videos Below
Case Study
If it is the first day, it is a Sunday… the second day is a
Monday...the last day is a Saturday...anything else is not a day
coursetro.com
int day = 1;
if(day == 1){
… (“It is a Sunday”);
} else if (day == 2) {
… (“It is a Monday”);
}
…
else {
… (“Not a valid day”);
}
coursetro.com
int day = 1;
switch(day){
case 1:
… (“It is a Sunday”);
break;
case 2:
… (“It is a Monday”);
break;
default:
… (“Not a valid day”);
break;
}
coursetro.com
1. break default java
2. switch fallthrough java
3. switch case expressions must be constant expressions
4. switch vs if else performance java
5. switch statement break after default
Google	It!
Print Hello World 4 times : The Noob
Way
System.out.println(“Hello World”);
System.out.println(“Hello World”);
System.out.println(“Hello World”);
System.out.println(“Hello World”);
coursetro.com
for
for( initial value; loop continuation condition; increment) {
//…something that you want to do a certain number of times
}
coursetro.com
Print “Hello World” 4 times
for(int i = 0; i < 4; i++){
…("Hello World");
}
Initialization Check Condition Run the code inside Increment
i = 0 0 < 4 = true Hello World i = 1
1 < 4 = true Hello World i = 2
i = 3Hello World2 < 4 = true
3 < 4 = true Hello World i = 4
4 < 4 = false
coursetro.com
Print “Hello World” 4 times
for(int i = 1; i <= 4; i++){
…("Hello World");
}
Initialization Check Condition Run the code inside Increment
i = 1 1 <= 4 = true Hello World i = 2
2 <= 4 = true Hello World i = 3
i = 4Hello World3 <= 4 = true
4 <= 4 = true Hello World i = 5
5 <= 4 = false
coursetro.com
Print “Hello World” 4 times
for(int i = 4; i > 0; i--){
…("Hello World");
}
Initialization Check Condition Run the code inside Decrement
i = 4 4 > 0 = true Hello World i = 3
3 > 0 = true Hello World i = 2
i = 1Hello World2 > 0 = true
1 > 0 = true Hello World i = 0
0 > 0 = false
coursetro.com
Vary the control variable from 1 to 10 in increments of 1.
for (int i = 1; i <= 10; i++)
Vary the control variable from 10 to 1 in decrements of 1.
for (int i = 10; i >= 1; i--)
Vary the control variable from 6 to 66 in increments of 6.
for (int i = 6; i <= 66; i += 6)
coursetro.com
Vary the control variable from 30 to 3 in decrements of 3.
for (int i = 30; i >= 3; i -= 3)
Vary the control variable over the values 3, 7, 11, 15, 19.
for (int i = 3; i <= 20; i += 4)
Vary the control variable over the values 100, 89, 78, 67, 56, 45, 34, 23, 12
for (int i = 100; i >= 10; i -= 11)
coursetro.com
Switch Explained Switch Example
For Explained For Example
Relevant
Videos
Click To Watch Videos Below
1. java for statement
2. java for statement multiple conditions
3. java for statement colon
4. java nested for loop number pyramid
5. java nested for loop exercises
6. java for loop off by one
7. java infinite for loop
Google	It!
while
while( some condition is true ) {
//…something that you want to do
}
coursetro.com
Print “Hello World” 4 times
int i = 0;
while(i < 4){
…("Hello World");
i++;
}
Initialization Check Condition Run the code inside
i = 0 0 < 4 = true Hello World i = 1
1 < 4 = true Hello World i = 2
i = 3Hello World2 < 4 = true
3 < 4 = true Hello World i = 4
4 < 4 = false
coursetro.com
Print “Hello World” Till
Condition Becomes False
boolean condition = true;
int i = 0;
while(condition){
…("Hello World");
i++;
if(i == 4){
condition = false;
}
}
Initialization Condition Run the code inside
i = 0 true Hello World i = 1
true Hello World i = 2
i = 3Hello Worldtrue
true Hello World i = 4
false
Increment if
1 == 4 is false
2 == 4 is false
3 == 4 is false
4 == 4 is true
So make
condition = false
1. java while loop
2. java nested while loop
3. infinite while loop in java
4. while vs for loop java
Google	It!
do…while
do{
//…something that you want to do
}
while( some condition is true );
coursetro.com
Print “Hello World” 4 times
int i = 0;
do
{
…("Hello World");
i++;
}
while(i < 4);
Initialization Check ConditionRun the code inside
i = 0 Hello World i = 1 1 < 4 = true
Hello World i = 2
i = 3Hello World
2 < 4 = true
3 < 4 = true
Hello World i = 4 4 < 4 = false
coursetro.com
while vs. do…while
boolean condition = false;
while(condition){
…(“Hello World”);
}
//Prints nothing
boolean condition = false;
do{
…(“Hello World”);
}
while(condition);
//Prints Hello World
coursetro.com
While Explained While Example
Do While
Explained
Do While
Example
Relevant
Videos
Click To Watch Videos Below
1. do while loop in java
2. do while vs while java
3. for vs while vs do while
4. infinite do while loop java
5. do while semicolon java
Google	It!
break
Break the cases in a switch
Break loops
Labelled break (covered in code)
coursetro.com
Break the cases of a switch
int day = 0;
switch(day){
case 0:
…(“Sunday”);
case 1:
…(“Monday”);
case 2:
…(“Tuesday”);
}
Output
Sunday
Monday
Tuesday
int day = 0;
switch(day){
case 0:
…(“Sunday”);
break;
case 1:
…(“Monday”);
break;
case 2:
…(“Tuesday”);
break;
}
Output
Sunday
coursetro.com
Break Loops
for(int i = 0; i < 9 ; i++) {
…(i);
if(i == 3){
break;
}
}
Output
0
1
2
3
int i = 0;
while(i < 9) {
…(i);
if(i == 3){
break;
}
i++;
}
Output
0
1
2
3
coursetro.com
continue
Skip an iteration in the loops with or
without a label.
coursetro.com
for(int i = 0; i < 9 ; i++) {
if(i == 3){
continue;
}
…println(i);
}
Output
0
1
2
4
5
6
7
8
int i = 0;
while(i < 9) {
i++;
if(i == 3){
continue;
}
…println(i);
}
Output
1
2
4
5
6
7
8
9 coursetro.com
Break Explained Break Example
Continue
Explained
Continue
Example
Relevant
Videos
Click To Watch Videos Below
1. break statement in java
2. break out of loops java
3. labelled break in java
4. continue statement in java
5. break vs continue in java
6. labelled continue in java
Google	It!
What are Strings
Strings are simply a sequence of characters defined inside “”
Examples:
“Hello”
“123”
“!@#$%^&*(”
“NB *(*(&GB86786rfyv”
coursetro.com
Kinds of Strings in Java
• String
• StringBuilder
• StringBuffer
coursetro.com
Creating a String
• String str = “Hello String”;
• String nullStr = null;
nullStr = “Hello String”;
• String newStr = new String(“Hello String”);
• String string = String.valueOf(“Hello String”);
Using String class
coursetro.com
Length
String str = “Hello World”;
System.out.println(str.length()); // 11
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
Concatenating Strings
String str = “Hello”; // Hello
str += “World”; // Hello World
str = str + “!”; // Hello World !
str.concat(“ Again”); // Hello World ! Again
coursetro.com
Escape Sequences
’
””

n
t
…
coursetro.com
Whose equal?
String s1 = “Hello”;
String s2 = “Hello”;
String s3 = “Bye”;
String s4 = new String(“Hello”);
String s5 = “hello”;
s1 == s2 ; // true
s1 == s4; // false
s1.equals(s4); // true
s1.equalsIgnoreCase(s5); // true
s1.equals(s3); // false
Hello
Bye
Hello
hello
coursetro.com
str.indexOf('e');// 1
str.indexOf("ello"); // 1
str.indexOf('o', 5); // 7
str.charAt(3); // l
str.substring(3); // lo World
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
coursetro.com
str.substring(3, 9); // lo Wor
str.contains("Z"); // false
"".isEmpty(); // true
str.toUpperCase(); // HELLO WORLD
str.toLowerCase(); // hello world
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
coursetro.com
“ Hello ”.trim(); // Hello
"Potatoes, Apples, Oranges".split(","); //
[“Potatoes”, “Apples”, “Oranges”]
str.join(",", "Apples", "Oranges", "Potatoes"); //
“Apples,Oranges,Potatoes”
H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
coursetro.com
1. java string class
2. java String.valueOf
3. java string length
4. java string concat
5. string concat vs + in java
6. java escape special
characters
7. java string equals
8. equals vs == in java
8. java string indexof
9. java string
equalsignorecase
10. java string charat
11. java string substring
12. java string contains
13. java check empty string
14. java string trim
15. Java string join
Google	It!
Creating a String Builder
StringBuilder sb= new StringBuilder();
sb.append("Hello");
StringBuilder sb= new StringBuilder("Hello Again");
StringBuilder sb= new StringBuilder(5);
sb.append("Hello One more time");
coursetro.com
Length
StringBuilder sb = new StringBuilder(“Hello World”);
System.out.println(sb.length()); // 11
coursetro.com
Concatenation
StringBuilder sb = new StringBuilder("Hello"); // Hello
sb.append(“World”); // Hello World
sb.append(“!”); // Hello World !
coursetro.com
Methods
StringBuilder sb = new StringBuilder("Hello"); // Hello
sb.append(" Again"); // Hello Again
sb.insert(5, "2"); // Hello2 Again
sb.replace(5, sb.length(), " World"); // Hello World
sb.delete(5, 11); // Hello
sb.deleteCharAt(4); // Hello coursetro.com
Conversions to String
byte b = 1;
String result = String.valueOf(b);
short s = 12;
String result = String.valueOf(s);
String result = String.valueOf(123);
char letter = ‘A’;
String result = String.valueOf(letter); coursetro.com
Conversions to String
long l = 1224;
String result = String.valueOf(l);
String result = String.valueOf(23.32f);
String result = String.valueOf(12.34);
String result = String.valueOf(true); coursetro.com
Conversions from String
String value = “12”;
byte b= Byte.parseByte(value);
String value = “123”;
short s= Short.parseShort(value);
int i = Integer.parseInt(“123”);
String value = “A”;
char c =value.charAt(0); coursetro.com
Conversions from String
String value = “1254”;
long l = Long.parseLong(value);
float f= Float.parseFloat(“23.42F”);
double d = Double.parseDouble(“12.3456”);
boolean b =Boolean.parseBoolean(“false”);
Click to watch videos below
String Explained String Example I
String Example
II
String Builder
And String
Conversions
Explained
String Builder
And String
Conversions
Example
1. java stringbuffer class
2. java stringbuilder class
3. string vs stringbuffer
4. string vs stringbuilder
5. stringbuffer vs
stringbuilder
6. stringbuilder append
7. stringbuilder delete
8. stringbuilder insert
9. stringbuilder replace
10. stringbuilder to string
11. integer.parseint
12. float.parsefloat
13. double.parsedouble
14. string.valueof
Google	It!
What are methods ?
A group of instructions that which you can name.
Calling the name runs all the instructions in that group.
Sure Boss.
Send that email
coursetro.com
Another example
main()
Math.pow()Send 2 and 3
Calculate 23
Give result
Use result
Add 2
numbers
without
methods
Add 2 + 5
int num1 = 2, num2 = 5;
int sum = num1 + num2;
Add 3 + 7
int num1 = 3, num2 = 7;
int sum = num1 + num2;
coursetro.com
Why repeat
yourself?
Bunch the 2 lines together!
{
int num1 = 2, num2 = 5;
int sum = num1 + num2;
}
coursetro.com
Give that
bunch a
name
add(){
int num1 = 2, num2 = 5;
int sum = num1 + num2;
}
coursetro.com
Call the
name() to add
2 + 5 every
time
add();
Its like calling your assistant
Wait, who wants to add 2 + 5 every
time?
coursetro.com
Tell them
what to add
while calling
them
add(3, 7)
coursetro.com
Assistant
says, “Give
me the
numbers, I
will add
them”
add(int num1, int num2){
int sum = num1 + num2;
}
coursetro.com
What should
the assistant
do after
adding?
Display the result?
Print them on a printer?
Email the boss?
Why not tell your assistant to give
the results back to the boss?
coursetro.com
Assistant says
it will return
an integer
int add(int num1, int num2){
int sum = num1 + num2;
return sum;
}
coursetro.com
Boss is free
to do
whatever
he/she wants
with the
result
int result = add(3, 7);
…println(result);
coursetro.com
Assistant says
“I will display
the value but
I wont give
the boss any
result”
void add(int num1, int num2){
int sum = num1 + num2;
…println(sum);
}
coursetro.com
Boss calls the
assistant to
display the
result
add(3, 7);
The assistant’s code has a println()
remember?
coursetro.com
1. java methods example
2. java method vs function
3. java method signature
4. java argument promotion and casting
5. java method overloading
6. java method call stack
Google	It!
Kinds of Methods
• Methods that take no input and give no result back.
• Methods that take input and give no result back.
• Methods that take no input and give some result back.
• Methods that take input and give some result back.
coursetro.com
No instructions, no reporting
Boss Assistant
sendEmail() public void sendEmail(){
//code written to send email
}
coursetro.com
No instructions, With reporting
Boss Assistant
boolean outcome = public boolean sendEmail(){
//code written to send email
return success;
}
Reporting
sendEmail()
coursetro.com
Instructions included, no reporting
Boss Assistant
sendEmail(
“contact@coursetro.com”,
”Hello”,
“I would love to buy your course”)
public void sendEmail(String to,
String subject, String message){
//code written to send email to the
//person with the subject and message
}
Instructions
coursetro.com
Instructions included, With reporting
Boss Assistant
boolean outcome = public boolean sendEmail(String to,
String subject, String message){
//code written to send email to the
//person with the subject and message
return success;
}
Instructions
Reporting
sendEmail(
“contact@coursetro.com”,
”Hello”,
“I would love to buy your course”)
coursetro.com
Assistant handling 1 set of instructions
Boss Assistant
Instructions [to whom, subject, message]
Reporting
coursetro.com
New assistant for email with
attachments?
coursetro.com
Multiple assistants with same name
Boss Assistant
Instructions [to whom, subject, message]
Reporting
Instructions [to whom, subject, message, attachment]
coursetro.com
Boss Assistant
boolean outcome = sendEmail(
“contact@coursetro.com”,
”Hello”,
“I would love to buy your course”)
//OR
public boolean sendEmail(String to,
String subject, String message){
//code written to send email to the
//person with the subject and message
return success;
}
Instructions
Reporting
boolean outcome =
sendEmail(
“contact@coursetro.com”,
”Hello”,
“I would love to buy your
course”,
…)
public boolean sendEmail(String to,
String subject, String message, Object attachment){
//code written to send email to the
//person with the subject and message
return success;
}
coursetro.com
What is variable scope?
Scope = part of the program where you can access your
variable…
coursetro.com
Example 1
public static void main(String[] args){
System.out.println(volumeOfCube(10.0));
}
public static double volumeOfCube(double length){
return length * length * length;
}
length is called a local variable and can be accessed or used only inside the blue box
Scope = within {} where it was created
Example 2
Both i and square can be accessed or used only inside the green box
Scope = within {} where it was created
public static void main(String[] args){
int sum = 0;
}
for(int i = 0 ; i <= 10 ; i++ ) {
int square = i * i;
sum = sum + square;
}
Same variable name in different scopes
public static void main(String[] args){
int result = square(5);
};
public static double square(int number){
int result = number * number;
return result;
}
Variables created inside different boxes can have the same name
Click to watch videos below
Methods
Explained
Types of
Methods
Methods
Example
Method
Overloading And
Variable Scope
Explained
Method
Overloading And
Variable Scope
Example
1. java method variable scope
2. java method overloading
3. java method overloading rules
Google	It!
What is an array ?
It’s a collection of variables of fixed size and same type.
Collection of integers.
Collection of Booleans
Collection of Characters
1 52 0
true falsefalse true
‘c’ ‘r’‘q’ ‘t’
Representation of an Array
Every item in an Array has a location
Location starts from 0, not 1
Length = 9, last position in the above array = 8
In Memory
Locati
on
0 1 2 3 4 5 6 7 8
Value C o u r s e t r o
coursetro.com
Method A
int[] numbers;
numbers =
coursetro.com
Method A
int [] numbers;
numbers = new int[10];
int[]
0
0
0
0
0
0
0
0
0
0
Method A
int [] numbers;
numbers = new int[10];
numbers[5] = 26;
int[]
0
0
0
0
0
26
0
0
0
0
0
1
2
3
4
5
6
7
8
9
Method B
int[] numbers = {0, 0, 45, 18}; int[]
0
0
45
18
0
1
2
3
coursetro.com
Array References
int[] scores = {10, 9, 7, 6, 8};
int[] values = scores;
scores[3] = 10;
System.out.println(values[3]); //Prints 10
int[]
10
9
7
6
8
0
1
2
3
4
scores =
values =
10
9
7
10
8
0
1
2
3
4
coursetro.com
Reading values from Arrays.
Using for loop:
int[] numbers = {4,1,5,7,8};
for( int i = 0 ; i < numbers.length ; i++ )
{
…(“Value at index ” + i + “ is: “ + numbers[i] );
}
coursetro.com
Reading values from Arrays.
Using while loop:
int[] numbers = {4,1,5,7,8};
int counter = 0;
while( counter < numbers.length ){
…(“Value at index ” + i + “ is: “ + numbers[i] );
counter += 1;
}
coursetro.com
Reading values from Arrays.
Using for-each loop:
int[] numbers = {4,1,5,7,8};
for( int i : numbers )
{
…(“Value “ + i );
}
coursetro.com
Send email to an array of addresses
Boss Assistant
String[] to = {
“contact@coursetro.com”,
“slidenerd@gmail.com”,
”contact@designcourse.com”};
sendEmail(to)
public void sendEmail(String[] to){
//code written to send ‘hello there’
//email to the person
}
Instructions
coursetro.com
Send and receive arrays from a method
Boss Assistant
String[] to = {
“contact@coursetro.com”,
“slidenerd@gmail.com”,
”contact@designcourse.com”};
boolean[] outcomes = sendEmail(to)
public boolean[] sendEmail(String[] to){
boolean[] successes = …
//code written to send hello to everyone
return successes;
}
Instructions
Reporting
coursetro.com
1. java array with variable size
2. java array declaration
3. java array initialization
4. where are arrays stored in memory in java
5. java string array
6. java character array
7. char array to string java
8. string to char array java
9. java for each loop
Google	It!
Single dimensional Array
int[] numbers = {9,8,7,6}; int[][] numbers = {
{9,8,7},
{6,5,4},
{3,2,1}
};
Position 0 1 2 3
Value 9 8 7 6
Position Col	0 Col	1 Col	2
Row	0 9 8 7
Row	1 6 5 4
Row	2 3 2 1
Multi dimensional Array
int[][] numbers;
numbers = new int[3][3];
numbers[0][0] = 9;
…
numbers[2][2] = 1;
Position Col	0 Col	1 Col	2
Row	0 9 8 7
Row	1 6 5 4
Row	2 3 2 1
coursetro.com
Print column wise
We want to print:
column 0, column 1, column 2 for row 0
column 0, column 1, column 2 for row 1
column 0, column 1, column 2 for row 2
We want to print
column c for row r
Position Col	0 Col	1 Col	2
Row	0 9 8 7
Row	1 6 5 4
Row	2 3 2 1
coursetro.com
Print column wise
If we wanted to print only the first row:
for(int c = 0; c < 3; c++){
…(numbers[0][c]);
}
Position Col	0 Col	1 Col	2
Row	0 9 8 7
Row	1 6 5 4
Row	2 3 2 1
coursetro.com
Print column wise
If we wanted to print all rows and all columns:
for(int r = 0; r < 3; r++){ //Increase the rows by 1 after inner loop runs
for(int c = 0; c < 3; c++){ //Increase the column by 1 each time
…(numbers[r][c]);
}
}
Position Col	0 Col	1 Col	2
Row	0 9 8 7
Row	1 6 5 4
Row	2 3 2 1
coursetro.com
Multidimensional Array
0 1 2
0 9 8 7
1 6 5 4
2 3 2 1
Jagged Array
0
1
2
9
8
6
7
5 4
0 1 2
int[][] numbers = new int[3][3];
numbers[0][0] = 9;
…
numbers[2][2] = 1;
int[][] numbers = new int[3][ ];
numbers[0] = new int[1];
numbers[1] = new int[2];
numbers[2] = new int[3];
numbers[0][0]=9;
…
numbers[2][2]=4;
coursetro.com
Arrays Explained Arrays Example
Multidimensional
Arrays Explained
Multidimensional
Arrays Example
Relevant
Videos
Click To Watch Videos Below
1. java multidimensional array
2. Java multidimensional array length
3. java jagged array
4. java jagged array length
Google	It!
What Next?
Part 2 covers all Object Oriented Stuff, search for it J
Thank You From…
coursetro.com

Weitere ähnliche Inhalte

Was ist angesagt?

05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
The Ring programming language version 1.7 book - Part 90 of 196
The Ring programming language version 1.7 book - Part 90 of 196The Ring programming language version 1.7 book - Part 90 of 196
The Ring programming language version 1.7 book - Part 90 of 196Mahmoud Samir Fayed
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - PolymorphismOum Saokosal
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassOum Saokosal
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.10 book - Part 86 of 212The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.10 book - Part 86 of 212Mahmoud Samir Fayed
 
oops -concepts
oops -conceptsoops -concepts
oops -conceptssinhacp
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-conceptsraahulwasule
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)Shambhavi Vats
 

Was ist angesagt? (20)

05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
The Ring programming language version 1.7 book - Part 90 of 196
The Ring programming language version 1.7 book - Part 90 of 196The Ring programming language version 1.7 book - Part 90 of 196
The Ring programming language version 1.7 book - Part 90 of 196
 
Java basic
Java basicJava basic
Java basic
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
 
Icom4015 lecture4-f16
Icom4015 lecture4-f16Icom4015 lecture4-f16
Icom4015 lecture4-f16
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract Class
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.10 book - Part 86 of 212The Ring programming language version 1.10 book - Part 86 of 212
The Ring programming language version 1.10 book - Part 86 of 212
 
oops -concepts
oops -conceptsoops -concepts
oops -concepts
 
38 object-concepts
38 object-concepts38 object-concepts
38 object-concepts
 
Core java oop
Core java oopCore java oop
Core java oop
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
38 object-concepts (1)
38 object-concepts (1)38 object-concepts (1)
38 object-concepts (1)
 
Icom4015 lecture3-f17
Icom4015 lecture3-f17Icom4015 lecture3-f17
Icom4015 lecture3-f17
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Icom4015 lecture12-s16
Icom4015 lecture12-s16Icom4015 lecture12-s16
Icom4015 lecture12-s16
 

Ähnlich wie The Ultimate FREE Java Course Part 1

Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Test code that will not slow you down
Test code that will not slow you downTest code that will not slow you down
Test code that will not slow you downKostadin Golev
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...Codemotion
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 VariablesHock Leng PUAH
 
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsEsoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsRasan Samarasinghe
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PITRafał Leszko
 
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system Tarin Gamberini
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operatorsmcollison
 
MUTANTS KILLER - PIT: state of the art of mutation testing system
MUTANTS KILLER - PIT: state of the art of mutation testing system MUTANTS KILLER - PIT: state of the art of mutation testing system
MUTANTS KILLER - PIT: state of the art of mutation testing system Tarin Gamberini
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05HUST
 

Ähnlich wie The Ultimate FREE Java Course Part 1 (20)

Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Test code that will not slow you down
Test code that will not slow you downTest code that will not slow you down
Test code that will not slow you down
 
DITEC - Programming with Java
DITEC - Programming with JavaDITEC - Programming with Java
DITEC - Programming with Java
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
java switch
java switchjava switch
java switch
 
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...
Paul Hofmann - Recruiting with Jenkins - How engineers can recruit engineers ...
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
Ch5(loops)
Ch5(loops)Ch5(loops)
Ch5(loops)
 
Spf Chapter4 Variables
Spf Chapter4 VariablesSpf Chapter4 Variables
Spf Chapter4 Variables
 
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsEsoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basics
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
 
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
MUTANTS KILLER - PIT: state of the art of mutation testing system
MUTANTS KILLER - PIT: state of the art of mutation testing system MUTANTS KILLER - PIT: state of the art of mutation testing system
MUTANTS KILLER - PIT: state of the art of mutation testing system
 
Csphtp1 05
Csphtp1 05Csphtp1 05
Csphtp1 05
 
J Unit
J UnitJ Unit
J Unit
 
M C6java5
M C6java5M C6java5
M C6java5
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 

Kürzlich hochgeladen

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
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
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Kürzlich hochgeladen (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
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
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

The Ultimate FREE Java Course Part 1

  • 1. The FREE Java Course that doesn’t SUCK Part 1
  • 2. Agenda 1. Memory Organization 2. Data Hierarchy 3. Types of programming languages 4. Hello World 5. Steps to Run A Java Program 6. Type Conversions
  • 3. Agenda 7. Operators 1. Arithmetic 2. Relational 3. Assignment 4. Increment/Decrement 5. Logical
  • 4. Agenda 8. If 9. Ternary Operator 10. for 11. while 12. do while 13. break 14. continue
  • 5. Agenda 15. Strings 16. String Builder & Conversions 17. Methods & Types of methods 18. Method Overloading 19. Variable Scope 20. Arrays
  • 6. Where do I watch these videos? coursetro.com
  • 8. 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 1 V i v z Vivz 26 Vivz 26 Gary 32 Anky 26 Bit Unicode Letter V Field File Record or coursetro.com
  • 9. +1300042774 +1400593419 +1200274027 load pay add overtime store total total = pay + overtime Machine Level Assembly Level High Level coursetro.com
  • 10. Install Java JDK On Windows Install Java JDK On Mac Install IntelliJ On Windows Install IntelliJ On Mac Relevant Videos Click To Watch Videos Below
  • 11. Google It! • ASCII table • unicode table • ASCII vs Unicode • utf-8 means • utf-8 vs utf-16 • machine level language vs assembly language
  • 18. 1. println in Java 2. system.out.println 3. java system class 4. java String 5. java main method 6. string args in java 7. void in java Google It!
  • 19. 5 steps to run a Java program Create Compile LoadVerify Run
  • 21. Compile Compiler Disk javac HelloWorld.java -> HelloWorld.class coursetro.com
  • 25. Memory Organization And Data Hierarchy Hello World Explained Hello World Example Steps To Run A Java Program Relevant Videos Click To Watch Videos Below
  • 26. 1. list of java editors 2. javac command 3. java command 4. java compiler vs interpreter 5. java classloader 6. java bytecode verifier 7. jvm in java 8. jvm vs jre 9. jvm vs jre vs jdk vs jit Google It!
  • 27. What is a variable? Storage location in a computer program. It has a name and value. Assign a value to it any number of times. coursetro.com
  • 30. byte myByte = 100; short myShort = 1000; int myInt = 100000; long myLong = 1000000; coursetro.com
  • 31. float myFloat = 125.124f; double myDouble = 125.214545544; char myChar = ‘C’; boolean condition = true coursetro.com
  • 32. What is a constant? It has a name and a value. You must assign the value while creating it and cannot change it afterwards. coursetro.com
  • 33. Example final double PI = 3.14159625; coursetro.com
  • 34. 1. Data types in java 2. java constants 3. java final variable Google It!
  • 35. Type Conversion A small box can be fitted into a bigger box but vice versa is tricky. Implicit Type Conversion long long int int Explicit Type Conversion
  • 36. Type Conversion int earthRad = 6371; long radius = earthRad; long int coursetro.com
  • 37. Type Conversion long galaxyRad= 1000000000000000000L; int radius = (int) galaxyRad; long int coursetro.com
  • 39. Arithmetic Operators Addition + Subtraction - Multiplication * Division / Modulus % coursetro.com
  • 40. Modulus Operator • It gives you the remainder between 2 numbers. 5 % 2 = 1 2 * 2 = 4 • 3 % 4 = 3 21 43 5 coursetro.com
  • 41. Expression • Expression in Algebra 1 + 2 + 3 + 4+ 5 = 3 5 • Expression in Java (1 + 2 + 3 + 4+ 5) / 5 = 3 coursetro.com
  • 42. Operator Precedence In Java Arithmetic Operators are evaluated in the following order from first to last Multiplicative * / % Additive + - Example: 3 * 41 + 7 / 3 – 2 % 1 = ((3 * 41) + (7 / 3)) – (2 % 1) = 125.33 coursetro.com
  • 43. 1. explicit typecasting in java 2. implicit typecasting in java 3. integer division in java 4. modulus operator can be applied to which of the following 5. java expression example 6. precedence of operators in java 7. associativity of operators in java Google It!
  • 44. Greater Than boolean result = 5 > 4; //Contains true int numOne = 5; int numTwo = 4; boolean result = numOne > numTwo; //Contains true coursetro.com
  • 45. Less Than boolean result = 5 < 4; //Contains false int numOne = 5; int numTwo = 4; boolean result = numOne < numTwo; //Contains false coursetro.com
  • 46. Greater Than Or Equal To boolean result = 5 >= 4; //Contains true boolean result = 5 >= 5; //Contains true int numOne = 5; int numTwo = 4; boolean result = numOne >= numTwo; //Contains true coursetro.com
  • 47. Less Than Or Equal To boolean result = 5 <= 4; //Contains false boolean result = 5 <= 5; //Contains true int numOne = 5; int numTwo = 4; boolean result = numOne <= numTwo; //Contains false coursetro.com
  • 48. Equals To boolean result = 5 == 4; //Contains false boolean result = 5 == 5; //Contains true int numOne = 5; int numTwo = 5; boolean result = numOne == numTwo; //Contains true coursetro.com
  • 49. Not Equals To boolean result = 5 != 4; //Contains true boolean result = 5 != 5; //Contains false int numOne = 5; int numTwo = 5; boolean result = numOne != numTwo; //Contains false coursetro.com
  • 51. Assignment Int result = 95; //Right to Left int numOne = 5 , numTwo = 4; coursetro.com
  • 52. += int numOne = 5; int numTwo = 4; //numOne = numOne + numTwo; numOne += numTwo; //Contains 9 coursetro.com
  • 53. -= int numOne = 5; int numTwo = 4; //numOne = numOne - numTwo; numOne -= numTwo; //Contains 1 coursetro.com
  • 54. *= int numOne = 5; int numTwo = 4; //numOne = numOne * numTwo; numOne *= numTwo; //Contains 20 coursetro.com
  • 55. /= int numOne = 9; int numTwo = 2; //numOne = numOne / numTwo; numOne /= numTwo; //Contains 4 coursetro.com
  • 56. %= int numOne = 5; int numTwo = 4; //numOne = numOne % numTwo; numOne %= numTwo; //Contains 1 coursetro.com
  • 57. Increment int year = 2050; newYear = year++; //Contains 2050 newYear = year; year = year + 1; int year = 2050; newYear = ++year; //Contains 2051 year = year + 1; newYear = year; coursetro.com
  • 58. Decrement int year = 2050; newYear = year--; //Contains 2050 newYear = year; year = year - 1; int year = 2050; newYear = --year; //Contains 2049 year = year - 1; newYear = year; coursetro.com
  • 59. ! A !A true false false true boolean engaged = false; boolean notEngaged = !engaged; coursetro.com
  • 60. && A B A && B true true true true false false false true false false false false boolean dcFan = true, marvelFan = true; if(dcFan && marvelFan){ System.out.println(“You like superheroes…”); }
  • 61. || A B A || B true true true True false true False true true false false false boolean dcFan = false, marvelFan = false; if(dcFan || marvelFan){ System.out.println(“You like some superhero, don’t you?”); } else{ System.out.println(“I guess not!”); }
  • 62. Assignment Operators Explained Assignment Operators Example Increment Decrement And Logical Operators Explained Increment Decrement And Logical Operators Example Relevant Videos Click To Watch Videos Below
  • 63. if If you get 90% or more this year I will get you a bike. if ( marks >= 90 ) { System.out.println(“You get a bike”); } coursetro.com
  • 64. if If you get more than 80% but less than 90% you get a phone. if (marks >= 80 && marks < 90) { System.out.println(“You get a phone”); } coursetro.com
  • 65. If else If you pass, you get a chocolate Else no video games. if ( marks >= 40 ) { System.out.println(“Chocolate yay!”); } else { System.out.println(“No video games”); } coursetro.com
  • 66. If else ladder If marks greater than 90%, you get a bike, if marks are between 80-90% you get a phone, otherwise you get a chocolate, if you failed, then no video games if ( marks >= 90 ) { System.out.println(“You get a bike”); } else if (marks >=80){ System.out.println(“You get a phone”); } else if (marks>=40){ System.out.println(“Chocolate yay!”); } else { System.out.println(“No video games”); } coursetro.com
  • 67. Nested if else if marks greater than 90 and physical fitness examination score greater than 80 you get a car else you get a bike If marks less than 90 go to the gym if ( marks >= 90 ) { if (score>=80){ System.out.println(“You get a car”); } else { System.out.println(“You get a bike!”); } } else { System.out.println(“Go to the gym”); } coursetro.com
  • 68. 1. assignment vs equality operator in java 2. == vs = in java 3. java logical operators 4. dangling else problem in java 5. semicolon after if statement in java 6. short circuit evaluation in java Google It!
  • 69. Ternary Operator result = some condition ? An expression that returns some value if true : An expression that returns some value if it is false; coursetro.com
  • 70. Ternary Operator int marks = 91; char grade = marks >= 90 ? ‘A’ : ‘B’;
  • 71. If Else Explained If Else Example Ternary Explained Ternary Example Relevant Videos Click To Watch Videos Below
  • 72. Case Study If it is the first day, it is a Sunday… the second day is a Monday...the last day is a Saturday...anything else is not a day coursetro.com
  • 73. int day = 1; if(day == 1){ … (“It is a Sunday”); } else if (day == 2) { … (“It is a Monday”); } … else { … (“Not a valid day”); } coursetro.com
  • 74. int day = 1; switch(day){ case 1: … (“It is a Sunday”); break; case 2: … (“It is a Monday”); break; default: … (“Not a valid day”); break; } coursetro.com
  • 75. 1. break default java 2. switch fallthrough java 3. switch case expressions must be constant expressions 4. switch vs if else performance java 5. switch statement break after default Google It!
  • 76. Print Hello World 4 times : The Noob Way System.out.println(“Hello World”); System.out.println(“Hello World”); System.out.println(“Hello World”); System.out.println(“Hello World”); coursetro.com
  • 77. for for( initial value; loop continuation condition; increment) { //…something that you want to do a certain number of times } coursetro.com
  • 78. Print “Hello World” 4 times for(int i = 0; i < 4; i++){ …("Hello World"); } Initialization Check Condition Run the code inside Increment i = 0 0 < 4 = true Hello World i = 1 1 < 4 = true Hello World i = 2 i = 3Hello World2 < 4 = true 3 < 4 = true Hello World i = 4 4 < 4 = false coursetro.com
  • 79. Print “Hello World” 4 times for(int i = 1; i <= 4; i++){ …("Hello World"); } Initialization Check Condition Run the code inside Increment i = 1 1 <= 4 = true Hello World i = 2 2 <= 4 = true Hello World i = 3 i = 4Hello World3 <= 4 = true 4 <= 4 = true Hello World i = 5 5 <= 4 = false coursetro.com
  • 80. Print “Hello World” 4 times for(int i = 4; i > 0; i--){ …("Hello World"); } Initialization Check Condition Run the code inside Decrement i = 4 4 > 0 = true Hello World i = 3 3 > 0 = true Hello World i = 2 i = 1Hello World2 > 0 = true 1 > 0 = true Hello World i = 0 0 > 0 = false coursetro.com
  • 81. Vary the control variable from 1 to 10 in increments of 1. for (int i = 1; i <= 10; i++) Vary the control variable from 10 to 1 in decrements of 1. for (int i = 10; i >= 1; i--) Vary the control variable from 6 to 66 in increments of 6. for (int i = 6; i <= 66; i += 6) coursetro.com
  • 82. Vary the control variable from 30 to 3 in decrements of 3. for (int i = 30; i >= 3; i -= 3) Vary the control variable over the values 3, 7, 11, 15, 19. for (int i = 3; i <= 20; i += 4) Vary the control variable over the values 100, 89, 78, 67, 56, 45, 34, 23, 12 for (int i = 100; i >= 10; i -= 11) coursetro.com
  • 83. Switch Explained Switch Example For Explained For Example Relevant Videos Click To Watch Videos Below
  • 84. 1. java for statement 2. java for statement multiple conditions 3. java for statement colon 4. java nested for loop number pyramid 5. java nested for loop exercises 6. java for loop off by one 7. java infinite for loop Google It!
  • 85. while while( some condition is true ) { //…something that you want to do } coursetro.com
  • 86. Print “Hello World” 4 times int i = 0; while(i < 4){ …("Hello World"); i++; } Initialization Check Condition Run the code inside i = 0 0 < 4 = true Hello World i = 1 1 < 4 = true Hello World i = 2 i = 3Hello World2 < 4 = true 3 < 4 = true Hello World i = 4 4 < 4 = false coursetro.com
  • 87. Print “Hello World” Till Condition Becomes False boolean condition = true; int i = 0; while(condition){ …("Hello World"); i++; if(i == 4){ condition = false; } } Initialization Condition Run the code inside i = 0 true Hello World i = 1 true Hello World i = 2 i = 3Hello Worldtrue true Hello World i = 4 false Increment if 1 == 4 is false 2 == 4 is false 3 == 4 is false 4 == 4 is true So make condition = false
  • 88. 1. java while loop 2. java nested while loop 3. infinite while loop in java 4. while vs for loop java Google It!
  • 89. do…while do{ //…something that you want to do } while( some condition is true ); coursetro.com
  • 90. Print “Hello World” 4 times int i = 0; do { …("Hello World"); i++; } while(i < 4); Initialization Check ConditionRun the code inside i = 0 Hello World i = 1 1 < 4 = true Hello World i = 2 i = 3Hello World 2 < 4 = true 3 < 4 = true Hello World i = 4 4 < 4 = false coursetro.com
  • 91. while vs. do…while boolean condition = false; while(condition){ …(“Hello World”); } //Prints nothing boolean condition = false; do{ …(“Hello World”); } while(condition); //Prints Hello World coursetro.com
  • 92. While Explained While Example Do While Explained Do While Example Relevant Videos Click To Watch Videos Below
  • 93. 1. do while loop in java 2. do while vs while java 3. for vs while vs do while 4. infinite do while loop java 5. do while semicolon java Google It!
  • 94. break Break the cases in a switch Break loops Labelled break (covered in code) coursetro.com
  • 95. Break the cases of a switch int day = 0; switch(day){ case 0: …(“Sunday”); case 1: …(“Monday”); case 2: …(“Tuesday”); } Output Sunday Monday Tuesday int day = 0; switch(day){ case 0: …(“Sunday”); break; case 1: …(“Monday”); break; case 2: …(“Tuesday”); break; } Output Sunday coursetro.com
  • 96. Break Loops for(int i = 0; i < 9 ; i++) { …(i); if(i == 3){ break; } } Output 0 1 2 3 int i = 0; while(i < 9) { …(i); if(i == 3){ break; } i++; } Output 0 1 2 3 coursetro.com
  • 97. continue Skip an iteration in the loops with or without a label. coursetro.com
  • 98. for(int i = 0; i < 9 ; i++) { if(i == 3){ continue; } …println(i); } Output 0 1 2 4 5 6 7 8 int i = 0; while(i < 9) { i++; if(i == 3){ continue; } …println(i); } Output 1 2 4 5 6 7 8 9 coursetro.com
  • 99. Break Explained Break Example Continue Explained Continue Example Relevant Videos Click To Watch Videos Below
  • 100. 1. break statement in java 2. break out of loops java 3. labelled break in java 4. continue statement in java 5. break vs continue in java 6. labelled continue in java Google It!
  • 101. What are Strings Strings are simply a sequence of characters defined inside “” Examples: “Hello” “123” “!@#$%^&*(” “NB *(*(&GB86786rfyv” coursetro.com
  • 102. Kinds of Strings in Java • String • StringBuilder • StringBuffer coursetro.com
  • 103. Creating a String • String str = “Hello String”; • String nullStr = null; nullStr = “Hello String”; • String newStr = new String(“Hello String”); • String string = String.valueOf(“Hello String”); Using String class coursetro.com
  • 104. Length String str = “Hello World”; System.out.println(str.length()); // 11 H e l l o W o r l d 0 1 2 3 4 5 6 7 8 9 10
  • 105. Concatenating Strings String str = “Hello”; // Hello str += “World”; // Hello World str = str + “!”; // Hello World ! str.concat(“ Again”); // Hello World ! Again coursetro.com
  • 107. Whose equal? String s1 = “Hello”; String s2 = “Hello”; String s3 = “Bye”; String s4 = new String(“Hello”); String s5 = “hello”; s1 == s2 ; // true s1 == s4; // false s1.equals(s4); // true s1.equalsIgnoreCase(s5); // true s1.equals(s3); // false Hello Bye Hello hello coursetro.com
  • 108. str.indexOf('e');// 1 str.indexOf("ello"); // 1 str.indexOf('o', 5); // 7 str.charAt(3); // l str.substring(3); // lo World H e l l o W o r l d 0 1 2 3 4 5 6 7 8 9 10 coursetro.com
  • 109. str.substring(3, 9); // lo Wor str.contains("Z"); // false "".isEmpty(); // true str.toUpperCase(); // HELLO WORLD str.toLowerCase(); // hello world H e l l o W o r l d 0 1 2 3 4 5 6 7 8 9 10 coursetro.com
  • 110. “ Hello ”.trim(); // Hello "Potatoes, Apples, Oranges".split(","); // [“Potatoes”, “Apples”, “Oranges”] str.join(",", "Apples", "Oranges", "Potatoes"); // “Apples,Oranges,Potatoes” H e l l o W o r l d 0 1 2 3 4 5 6 7 8 9 10 coursetro.com
  • 111. 1. java string class 2. java String.valueOf 3. java string length 4. java string concat 5. string concat vs + in java 6. java escape special characters 7. java string equals 8. equals vs == in java 8. java string indexof 9. java string equalsignorecase 10. java string charat 11. java string substring 12. java string contains 13. java check empty string 14. java string trim 15. Java string join Google It!
  • 112. Creating a String Builder StringBuilder sb= new StringBuilder(); sb.append("Hello"); StringBuilder sb= new StringBuilder("Hello Again"); StringBuilder sb= new StringBuilder(5); sb.append("Hello One more time"); coursetro.com
  • 113. Length StringBuilder sb = new StringBuilder(“Hello World”); System.out.println(sb.length()); // 11 coursetro.com
  • 114. Concatenation StringBuilder sb = new StringBuilder("Hello"); // Hello sb.append(“World”); // Hello World sb.append(“!”); // Hello World ! coursetro.com
  • 115. Methods StringBuilder sb = new StringBuilder("Hello"); // Hello sb.append(" Again"); // Hello Again sb.insert(5, "2"); // Hello2 Again sb.replace(5, sb.length(), " World"); // Hello World sb.delete(5, 11); // Hello sb.deleteCharAt(4); // Hello coursetro.com
  • 116. Conversions to String byte b = 1; String result = String.valueOf(b); short s = 12; String result = String.valueOf(s); String result = String.valueOf(123); char letter = ‘A’; String result = String.valueOf(letter); coursetro.com
  • 117. Conversions to String long l = 1224; String result = String.valueOf(l); String result = String.valueOf(23.32f); String result = String.valueOf(12.34); String result = String.valueOf(true); coursetro.com
  • 118. Conversions from String String value = “12”; byte b= Byte.parseByte(value); String value = “123”; short s= Short.parseShort(value); int i = Integer.parseInt(“123”); String value = “A”; char c =value.charAt(0); coursetro.com
  • 119. Conversions from String String value = “1254”; long l = Long.parseLong(value); float f= Float.parseFloat(“23.42F”); double d = Double.parseDouble(“12.3456”); boolean b =Boolean.parseBoolean(“false”);
  • 120. Click to watch videos below String Explained String Example I String Example II String Builder And String Conversions Explained String Builder And String Conversions Example
  • 121. 1. java stringbuffer class 2. java stringbuilder class 3. string vs stringbuffer 4. string vs stringbuilder 5. stringbuffer vs stringbuilder 6. stringbuilder append 7. stringbuilder delete 8. stringbuilder insert 9. stringbuilder replace 10. stringbuilder to string 11. integer.parseint 12. float.parsefloat 13. double.parsedouble 14. string.valueof Google It!
  • 122. What are methods ? A group of instructions that which you can name. Calling the name runs all the instructions in that group. Sure Boss. Send that email coursetro.com
  • 123. Another example main() Math.pow()Send 2 and 3 Calculate 23 Give result Use result
  • 124. Add 2 numbers without methods Add 2 + 5 int num1 = 2, num2 = 5; int sum = num1 + num2; Add 3 + 7 int num1 = 3, num2 = 7; int sum = num1 + num2; coursetro.com
  • 125. Why repeat yourself? Bunch the 2 lines together! { int num1 = 2, num2 = 5; int sum = num1 + num2; } coursetro.com
  • 126. Give that bunch a name add(){ int num1 = 2, num2 = 5; int sum = num1 + num2; } coursetro.com
  • 127. Call the name() to add 2 + 5 every time add(); Its like calling your assistant Wait, who wants to add 2 + 5 every time? coursetro.com
  • 128. Tell them what to add while calling them add(3, 7) coursetro.com
  • 129. Assistant says, “Give me the numbers, I will add them” add(int num1, int num2){ int sum = num1 + num2; } coursetro.com
  • 130. What should the assistant do after adding? Display the result? Print them on a printer? Email the boss? Why not tell your assistant to give the results back to the boss? coursetro.com
  • 131. Assistant says it will return an integer int add(int num1, int num2){ int sum = num1 + num2; return sum; } coursetro.com
  • 132. Boss is free to do whatever he/she wants with the result int result = add(3, 7); …println(result); coursetro.com
  • 133. Assistant says “I will display the value but I wont give the boss any result” void add(int num1, int num2){ int sum = num1 + num2; …println(sum); } coursetro.com
  • 134. Boss calls the assistant to display the result add(3, 7); The assistant’s code has a println() remember? coursetro.com
  • 135. 1. java methods example 2. java method vs function 3. java method signature 4. java argument promotion and casting 5. java method overloading 6. java method call stack Google It!
  • 136. Kinds of Methods • Methods that take no input and give no result back. • Methods that take input and give no result back. • Methods that take no input and give some result back. • Methods that take input and give some result back. coursetro.com
  • 137. No instructions, no reporting Boss Assistant sendEmail() public void sendEmail(){ //code written to send email } coursetro.com
  • 138. No instructions, With reporting Boss Assistant boolean outcome = public boolean sendEmail(){ //code written to send email return success; } Reporting sendEmail() coursetro.com
  • 139. Instructions included, no reporting Boss Assistant sendEmail( “contact@coursetro.com”, ”Hello”, “I would love to buy your course”) public void sendEmail(String to, String subject, String message){ //code written to send email to the //person with the subject and message } Instructions coursetro.com
  • 140. Instructions included, With reporting Boss Assistant boolean outcome = public boolean sendEmail(String to, String subject, String message){ //code written to send email to the //person with the subject and message return success; } Instructions Reporting sendEmail( “contact@coursetro.com”, ”Hello”, “I would love to buy your course”) coursetro.com
  • 141. Assistant handling 1 set of instructions Boss Assistant Instructions [to whom, subject, message] Reporting coursetro.com
  • 142. New assistant for email with attachments? coursetro.com
  • 143. Multiple assistants with same name Boss Assistant Instructions [to whom, subject, message] Reporting Instructions [to whom, subject, message, attachment] coursetro.com
  • 144. Boss Assistant boolean outcome = sendEmail( “contact@coursetro.com”, ”Hello”, “I would love to buy your course”) //OR public boolean sendEmail(String to, String subject, String message){ //code written to send email to the //person with the subject and message return success; } Instructions Reporting boolean outcome = sendEmail( “contact@coursetro.com”, ”Hello”, “I would love to buy your course”, …) public boolean sendEmail(String to, String subject, String message, Object attachment){ //code written to send email to the //person with the subject and message return success; } coursetro.com
  • 145. What is variable scope? Scope = part of the program where you can access your variable… coursetro.com
  • 146. Example 1 public static void main(String[] args){ System.out.println(volumeOfCube(10.0)); } public static double volumeOfCube(double length){ return length * length * length; } length is called a local variable and can be accessed or used only inside the blue box Scope = within {} where it was created
  • 147. Example 2 Both i and square can be accessed or used only inside the green box Scope = within {} where it was created public static void main(String[] args){ int sum = 0; } for(int i = 0 ; i <= 10 ; i++ ) { int square = i * i; sum = sum + square; }
  • 148. Same variable name in different scopes public static void main(String[] args){ int result = square(5); }; public static double square(int number){ int result = number * number; return result; } Variables created inside different boxes can have the same name
  • 149. Click to watch videos below Methods Explained Types of Methods Methods Example Method Overloading And Variable Scope Explained Method Overloading And Variable Scope Example
  • 150. 1. java method variable scope 2. java method overloading 3. java method overloading rules Google It!
  • 151. What is an array ? It’s a collection of variables of fixed size and same type. Collection of integers. Collection of Booleans Collection of Characters 1 52 0 true falsefalse true ‘c’ ‘r’‘q’ ‘t’
  • 152. Representation of an Array Every item in an Array has a location Location starts from 0, not 1 Length = 9, last position in the above array = 8 In Memory Locati on 0 1 2 3 4 5 6 7 8 Value C o u r s e t r o coursetro.com
  • 154. Method A int [] numbers; numbers = new int[10]; int[] 0 0 0 0 0 0 0 0 0 0
  • 155. Method A int [] numbers; numbers = new int[10]; numbers[5] = 26; int[] 0 0 0 0 0 26 0 0 0 0 0 1 2 3 4 5 6 7 8 9
  • 156. Method B int[] numbers = {0, 0, 45, 18}; int[] 0 0 45 18 0 1 2 3 coursetro.com
  • 157. Array References int[] scores = {10, 9, 7, 6, 8}; int[] values = scores; scores[3] = 10; System.out.println(values[3]); //Prints 10 int[] 10 9 7 6 8 0 1 2 3 4 scores = values = 10 9 7 10 8 0 1 2 3 4 coursetro.com
  • 158. Reading values from Arrays. Using for loop: int[] numbers = {4,1,5,7,8}; for( int i = 0 ; i < numbers.length ; i++ ) { …(“Value at index ” + i + “ is: “ + numbers[i] ); } coursetro.com
  • 159. Reading values from Arrays. Using while loop: int[] numbers = {4,1,5,7,8}; int counter = 0; while( counter < numbers.length ){ …(“Value at index ” + i + “ is: “ + numbers[i] ); counter += 1; } coursetro.com
  • 160. Reading values from Arrays. Using for-each loop: int[] numbers = {4,1,5,7,8}; for( int i : numbers ) { …(“Value “ + i ); } coursetro.com
  • 161. Send email to an array of addresses Boss Assistant String[] to = { “contact@coursetro.com”, “slidenerd@gmail.com”, ”contact@designcourse.com”}; sendEmail(to) public void sendEmail(String[] to){ //code written to send ‘hello there’ //email to the person } Instructions coursetro.com
  • 162. Send and receive arrays from a method Boss Assistant String[] to = { “contact@coursetro.com”, “slidenerd@gmail.com”, ”contact@designcourse.com”}; boolean[] outcomes = sendEmail(to) public boolean[] sendEmail(String[] to){ boolean[] successes = … //code written to send hello to everyone return successes; } Instructions Reporting coursetro.com
  • 163. 1. java array with variable size 2. java array declaration 3. java array initialization 4. where are arrays stored in memory in java 5. java string array 6. java character array 7. char array to string java 8. string to char array java 9. java for each loop Google It!
  • 164. Single dimensional Array int[] numbers = {9,8,7,6}; int[][] numbers = { {9,8,7}, {6,5,4}, {3,2,1} }; Position 0 1 2 3 Value 9 8 7 6 Position Col 0 Col 1 Col 2 Row 0 9 8 7 Row 1 6 5 4 Row 2 3 2 1
  • 165. Multi dimensional Array int[][] numbers; numbers = new int[3][3]; numbers[0][0] = 9; … numbers[2][2] = 1; Position Col 0 Col 1 Col 2 Row 0 9 8 7 Row 1 6 5 4 Row 2 3 2 1 coursetro.com
  • 166. Print column wise We want to print: column 0, column 1, column 2 for row 0 column 0, column 1, column 2 for row 1 column 0, column 1, column 2 for row 2 We want to print column c for row r Position Col 0 Col 1 Col 2 Row 0 9 8 7 Row 1 6 5 4 Row 2 3 2 1 coursetro.com
  • 167. Print column wise If we wanted to print only the first row: for(int c = 0; c < 3; c++){ …(numbers[0][c]); } Position Col 0 Col 1 Col 2 Row 0 9 8 7 Row 1 6 5 4 Row 2 3 2 1 coursetro.com
  • 168. Print column wise If we wanted to print all rows and all columns: for(int r = 0; r < 3; r++){ //Increase the rows by 1 after inner loop runs for(int c = 0; c < 3; c++){ //Increase the column by 1 each time …(numbers[r][c]); } } Position Col 0 Col 1 Col 2 Row 0 9 8 7 Row 1 6 5 4 Row 2 3 2 1 coursetro.com
  • 169. Multidimensional Array 0 1 2 0 9 8 7 1 6 5 4 2 3 2 1 Jagged Array 0 1 2 9 8 6 7 5 4 0 1 2 int[][] numbers = new int[3][3]; numbers[0][0] = 9; … numbers[2][2] = 1; int[][] numbers = new int[3][ ]; numbers[0] = new int[1]; numbers[1] = new int[2]; numbers[2] = new int[3]; numbers[0][0]=9; … numbers[2][2]=4; coursetro.com
  • 170. Arrays Explained Arrays Example Multidimensional Arrays Explained Multidimensional Arrays Example Relevant Videos Click To Watch Videos Below
  • 171. 1. java multidimensional array 2. Java multidimensional array length 3. java jagged array 4. java jagged array length Google It!
  • 172. What Next? Part 2 covers all Object Oriented Stuff, search for it J