O slideshow foi denunciado.

The Ultimate FREE Java Course Part 1

182

Compartilhar

Carregando em…3
×
1 de 173
1 de 173

The Ultimate FREE Java Course Part 1

182

Compartilhar

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?

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?

Mais Conteúdo rRelacionado

Audiolivros relacionados

Gratuito durante 14 dias do Scribd

Ver tudo

The Ultimate FREE Java Course Part 1

  1. 1. The FREE Java Course that doesn’t SUCK Part 1
  2. 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. 3. Agenda 7. Operators 1. Arithmetic 2. Relational 3. Assignment 4. Increment/Decrement 5. Logical
  4. 4. Agenda 8. If 9. Ternary Operator 10. for 11. while 12. do while 13. break 14. continue
  5. 5. Agenda 15. Strings 16. String Builder & Conversions 17. Methods & Types of methods 18. Method Overloading 19. Variable Scope 20. Arrays
  6. 6. Where do I watch these videos? coursetro.com
  7. 7. Mouse Display Printer Keyboard Main Memory Secondary Storage CPU Control Unit ALU CPU Storage Input Devices Output Devices
  8. 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. 9. +1300042774 +1400593419 +1200274027 load pay add overtime store total total = pay + overtime Machine Level Assembly Level High Level coursetro.com
  10. 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. 11. Google It! • ASCII table • unicode table • ASCII vs Unicode • utf-8 means • utf-8 vs utf-16 • machine level language vs assembly language
  12. 12. Hello World coursetro.com
  13. 13. “Hello World” coursetro.com
  14. 14. println(“Hello World”); coursetro.com
  15. 15. System.out.println(“Hello World”); coursetro.com
  16. 16. coursetro.com
  17. 17. coursetro.com
  18. 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. 19. 5 steps to run a Java program Create Compile LoadVerify Run
  20. 20. Create Editor Disk coursetro.com
  21. 21. Compile Compiler Disk javac HelloWorld.java -> HelloWorld.class coursetro.com
  22. 22. Load Class Loader Disk Primary Memory java HelloWorld coursetro.com
  23. 23. Verify Bytecode Verifier Primary Memory coursetro.com
  24. 24. Run Java Virtual Machine (JVM) Primary Memory coursetro.com
  25. 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. 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. 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
  28. 28. byte short int long 2 bytes1 byte 4 bytes 8 bytes
  29. 29. boolean char float double 2 bytesNot precisely defined 4 bytes 8 bytes
  30. 30. byte myByte = 100; short myShort = 1000; int myInt = 100000; long myLong = 1000000; coursetro.com
  31. 31. float myFloat = 125.124f; double myDouble = 125.214545544; char myChar = ‘C’; boolean condition = true coursetro.com
  32. 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. 33. Example final double PI = 3.14159625; coursetro.com
  34. 34. 1. Data types in java 2. java constants 3. java final variable Google It!
  35. 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. 36. Type Conversion int earthRad = 6371; long radius = earthRad; long int coursetro.com
  37. 37. Type Conversion long galaxyRad= 1000000000000000000L; int radius = (int) galaxyRad; long int coursetro.com
  38. 38. Variables And Constants Explained Variables And Constants Example Typecasting Explained Typecasting Example Relevant Videos Click To Watch Videos Below
  39. 39. Arithmetic Operators Addition + Subtraction - Multiplication * Division / Modulus % coursetro.com
  40. 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. 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. 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. 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. 44. Greater Than boolean result = 5 > 4; //Contains true int numOne = 5; int numTwo = 4; boolean result = numOne > numTwo; //Contains true coursetro.com
  45. 45. Less Than boolean result = 5 < 4; //Contains false int numOne = 5; int numTwo = 4; boolean result = numOne < numTwo; //Contains false coursetro.com
  46. 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. 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. 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. 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
  50. 50. Arithmetic Operators Explained Arithmetic Operators Example Relational Operators Explained Relational Operators Example Relevant Videos Click To Watch Videos Below
  51. 51. Assignment Int result = 95; //Right to Left int numOne = 5 , numTwo = 4; coursetro.com
  52. 52. += int numOne = 5; int numTwo = 4; //numOne = numOne + numTwo; numOne += numTwo; //Contains 9 coursetro.com
  53. 53. -= int numOne = 5; int numTwo = 4; //numOne = numOne - numTwo; numOne -= numTwo; //Contains 1 coursetro.com
  54. 54. *= int numOne = 5; int numTwo = 4; //numOne = numOne * numTwo; numOne *= numTwo; //Contains 20 coursetro.com
  55. 55. /= int numOne = 9; int numTwo = 2; //numOne = numOne / numTwo; numOne /= numTwo; //Contains 4 coursetro.com
  56. 56. %= int numOne = 5; int numTwo = 4; //numOne = numOne % numTwo; numOne %= numTwo; //Contains 1 coursetro.com
  57. 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. 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. 59. ! A !A true false false true boolean engaged = false; boolean notEngaged = !engaged; coursetro.com
  60. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 70. Ternary Operator int marks = 91; char grade = marks >= 90 ? ‘A’ : ‘B’;
  71. 71. If Else Explained If Else Example Ternary Explained Ternary Example Relevant Videos Click To Watch Videos Below
  72. 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. 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. 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. 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. 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. 77. for for( initial value; loop continuation condition; increment) { //…something that you want to do a certain number of times } coursetro.com
  78. 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. 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. 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. 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. 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. 83. Switch Explained Switch Example For Explained For Example Relevant Videos Click To Watch Videos Below
  84. 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. 85. while while( some condition is true ) { //…something that you want to do } coursetro.com
  86. 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. 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. 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. 89. do…while do{ //…something that you want to do } while( some condition is true ); coursetro.com
  90. 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. 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. 92. While Explained While Example Do While Explained Do While Example Relevant Videos Click To Watch Videos Below
  93. 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. 94. break Break the cases in a switch Break loops Labelled break (covered in code) coursetro.com
  95. 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. 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. 97. continue Skip an iteration in the loops with or without a label. coursetro.com
  98. 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. 99. Break Explained Break Example Continue Explained Continue Example Relevant Videos Click To Watch Videos Below
  100. 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. 101. What are Strings Strings are simply a sequence of characters defined inside “” Examples: “Hello” “123” “!@#$%^&*(” “NB *(*(&GB86786rfyv” coursetro.com
  102. 102. Kinds of Strings in Java • String • StringBuilder • StringBuffer coursetro.com
  103. 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. 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. 105. Concatenating Strings String str = “Hello”; // Hello str += “World”; // Hello World str = str + “!”; // Hello World ! str.concat(“ Again”); // Hello World ! Again coursetro.com
  106. 106. Escape Sequences ’ ”” n t … coursetro.com
  107. 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. 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. 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. 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. 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. 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. 113. Length StringBuilder sb = new StringBuilder(“Hello World”); System.out.println(sb.length()); // 11 coursetro.com
  114. 114. Concatenation StringBuilder sb = new StringBuilder("Hello"); // Hello sb.append(“World”); // Hello World sb.append(“!”); // Hello World ! coursetro.com
  115. 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. 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. 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. 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. 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. 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. 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. 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. 123. Another example main() Math.pow()Send 2 and 3 Calculate 23 Give result Use result
  124. 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. 125. Why repeat yourself? Bunch the 2 lines together! { int num1 = 2, num2 = 5; int sum = num1 + num2; } coursetro.com
  126. 126. Give that bunch a name add(){ int num1 = 2, num2 = 5; int sum = num1 + num2; } coursetro.com
  127. 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. 128. Tell them what to add while calling them add(3, 7) coursetro.com
  129. 129. Assistant says, “Give me the numbers, I will add them” add(int num1, int num2){ int sum = num1 + num2; } coursetro.com
  130. 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. 131. Assistant says it will return an integer int add(int num1, int num2){ int sum = num1 + num2; return sum; } coursetro.com
  132. 132. Boss is free to do whatever he/she wants with the result int result = add(3, 7); …println(result); coursetro.com
  133. 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. 134. Boss calls the assistant to display the result add(3, 7); The assistant’s code has a println() remember? coursetro.com
  135. 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. 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. 137. No instructions, no reporting Boss Assistant sendEmail() public void sendEmail(){ //code written to send email } coursetro.com
  138. 138. No instructions, With reporting Boss Assistant boolean outcome = public boolean sendEmail(){ //code written to send email return success; } Reporting sendEmail() coursetro.com
  139. 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. 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. 141. Assistant handling 1 set of instructions Boss Assistant Instructions [to whom, subject, message] Reporting coursetro.com
  142. 142. New assistant for email with attachments? coursetro.com
  143. 143. Multiple assistants with same name Boss Assistant Instructions [to whom, subject, message] Reporting Instructions [to whom, subject, message, attachment] coursetro.com
  144. 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. 145. What is variable scope? Scope = part of the program where you can access your variable… coursetro.com
  146. 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. 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. 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. 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. 150. 1. java method variable scope 2. java method overloading 3. java method overloading rules Google It!
  151. 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. 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
  153. 153. Method A int[] numbers; numbers = coursetro.com
  154. 154. Method A int [] numbers; numbers = new int[10]; int[] 0 0 0 0 0 0 0 0 0 0
  155. 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. 156. Method B int[] numbers = {0, 0, 45, 18}; int[] 0 0 45 18 0 1 2 3 coursetro.com
  157. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 170. Arrays Explained Arrays Example Multidimensional Arrays Explained Multidimensional Arrays Example Relevant Videos Click To Watch Videos Below
  171. 171. 1. java multidimensional array 2. Java multidimensional array length 3. java jagged array 4. java jagged array length Google It!
  172. 172. What Next? Part 2 covers all Object Oriented Stuff, search for it J
  173. 173. Thank You From… coursetro.com

×