Java Overview

What is Java?:

  • Java is a general-purpose, object-oriented programming language
  • It is important for our uses as it also powers Android applications
  • It is easy to learn, versatile, and well known

Variables:

  • Just pieces of information
  • Example:
    • int number = 120;
      • “create an integer variable, ‘number’ and set it to 120”
    • number = 81;
      • “reset the previously created variable to 81”
    • number++;
      • “increase ‘number’ value by 1 (now 82)”
    • System.out.print(number);
      • “display ‘number’ on the screen”

Arrays: 

  • Arrays are similar to variables but store a list of information
  • The length of arrays do not change
  • Arrays can only hold one data type
  • Example:
    • public int[] numbers = new int[3];
      • “create an integer array ‘numbers’ and set it to a new integer array of size three’
    • numbers[2] = 3;
      • “reset the value at index 2 of ‘numbers’ to 3”
      • *in java the “index” starts at zero, so the value at index 2 is actually the third element in the array (0, 1, 2)
    • System.out.println(numbers[2]);
      • “display the value at index 2 of ‘numbers’ on the screen”

Loops:

  • Allow repeating commands
  • A for loop allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line

for (int i = 0; i < 5; i++){

System.out.print(i);

}

  • A while loop is more basic, will evaluate the condition within the brackets, run code, reevaluate condition, so on

while(true){

            System.out.print(“hello”);

}

Conditional Statements:

  • Conditionals allows the code to make decisions
  • Example:

if (name == “Rahul”) {

System.out.print(“Why u sick?”);

}

  • Note: && means and, || means not, == means equals?, and != not equals
  • Remember == checks equality; = sets equality

Methods:

  • A block of reusable code
  • Also called functions
  • Methods can (but are not required to) take inputs and give outputs
  • inputs are called arguments or parameters, and outputs are called returns
  • can have mutiple inputs but only 1 output
  • Practical Example: think a math function, it takes in a value (x), performs an operation and returns another value (y)
  • Examples:

int giveOne() { /* “this function, ‘giveOne,’ will take no inputs and will output an integer value” */

return 1; // “output 1”

 } // “we are done with this method”

void print(String text) { /* “this function, ‘print,’ will take a String input  but no output” */

System.out.println(text); // “display the inputed text on the screen”

} // “we are done with this method”