Have you ever seen a Java course that is 'visual' in nature? The kind of lessons where you can see what happens when you are creating an object, doing method overloading, and using run-time polymorphism. I bet you haven't. Welcome to the most 'visual' Java course you will ever take.
This course teaches you everything you need to get started with Java programming. It divides each topic into explanation and example.
The explanation section deals with how a particular concept works in Java and covers the following:
Data Types & Variables, Typecasting,
Operators & Conditional statements such as if, if else, switch
Loops such as for, for each, while, and do while
Methods, Types of methods, Call by Value vs. Call by Reference, Variable Scope, Method Overloading,
Arrays and ArrayLists,
Classes and objects, How classes and objects work, and How primitive types vs. reference types are stored
Swing classes to take input from the user and command line methods to take input from the user such as BufferedReader, Scanner, Console,
Static variables, static methods, this keyword, super keyword, method overriding,
What is inheritance
And a real world example of run-time polymorphism that actually demonstrates what happens with vs. without polymorphism.
The example section covers an example for every concept discussed above. The course will be updated constantly to keep in sync with the real world developments in Java. So, are you ready to become a Java PRO?
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
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
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
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
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
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
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!
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!
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
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
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
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
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
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
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
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