850 likes | 1.5k Views
Java Basics. Things to notice. A Bolded word means that this will come up a lot, so ask questions. An Example of each basic will be on each slide Each example will be broken down sequentially. A “Lets combine them!” slide will be presented every few slides. important: Remember these!
E N D
Things to notice • A Bolded word means that this will come up a lot, so ask questions. • An Example of each basic will be on each slide • Each example will be broken down sequentially. • A “Lets combine them!” slide will be presented every few slides. • important: Remember these! • important: All Java code that is executed is from within a class (to be explained in another lesson).
Statements • “A statement in Java forms a complete command to be executed and can include one or more expressions.” • Example: • System.out.println(“Hello World”); • Lets break this down: • This statement prints out hello world to a console. • The System.out.printLn accesses a function of printLn in the system library (package). • The () Pass on arguments to the printLn Function, in this case, the “Hello World” string (to be explained later). • The Semicolon (;) ends the statement. • important: Every statement ends with a semicolon! Remember where that key is!
Variables • Variables in Java hold data, there are many types of data for variables (which we will go over in a later slide). • Here is a basic example of a variable: • intnumberOfPeople = 10; • Lets break down this statement: • intis the type of variable it is. • numberOfPeople is the name of the variable. • The equals sign (=) assigns the variable some value of the same datatype (in this case 10). • The semicolon (;) ends the statement.
Basic Data Types (Just a tidbit) • int – A numerical type that holds Whole numbers (Integershint hint) that can range from a value of -2,147,483,648 to 2,147,483,647 • byte – Like int but can only hold one byte of data (values from -128 to 127) • short – Like int but can only hold 16 bits of data (Values from -32,768 to 32,767) • double – A double precision floating number (Decimal) • float – A single precision floating number (Decimal) • long – Like int but holds 64 bits of data (ask us for max, its VERY big). • boolean – A true/false bit. • char – A single character like ‘a’
Control Flow Statements: If (and Else and if else) • The if statement evaluates a given condition, if it is true, then it will execute it, if not it will look for an if else or else to evaluate and execute. • Example: • intage = 21;if (age >= 21){System.out.println(“You can now drink…”);} else {System.out.println(“You can’t Drink!”);} • Lets break this down: • The intage = 21; declares a variable of Age with the initial value of 21. • The if starts a conditional operation. That conditional operation can be translated like this: (If Age is Greater Than or equal to (>=) 21) ({)Then… • Execute System.out.printLn(“You can now drink…”); • (})Else({)… • Execute System.out.printLn(“You can’t Drink!”); • End of if (})
Conditional Operators (a tidbit) • Greater than is > • Example: 10 > 12 = false. • Less than is < • Example: 10 < 12 = true. • Greater than or equal to is >= • Example: 12 >= 12 = true. • Less than or equal to is <= • Example: 12 <= 13 = false • Equal to is == (NOT =) • Example: 12 == 12 = true • Not Equal to is != • Example: 12 != 13 = true • Or is || • Explanation: If one side is true, the whole thing is true • Example: 12 == 11 || 12 < 13 = true • And is && • Explanation: Only true if both sides are true • Example: 12 == 10 && 12 > 10 = false • Not (opposite) is ! • Explanation: It gives back the opposite of what is true. (Use with parenthesis) • Example: !(12 > 10) = false • Important element of logical comparisons (|| and &&) is that they are evaluated from left to right and will not evaluate the right side if not needed.
Arrays and for loops • An Array is a sequence of values in a numbered index. • Important: All arrays begin at the index of 0. • A for loop is a loop that has an initializer, control statement, and end of loop operation. It is commonly used to loop through a set of values. • Example: • String[] names = new String[4];names[0] = “James”;names[1] = “Dev”;names[2] = “Mr. Rutten”;names[3] = “Mr. Donlon”;for (inti = 0; i < names.Length; i++){System.out.println(“Name Number: ” + (i + 1) + “ is “ + names[i]);} • Lets Break this down: • This prints out all the names: James, Dev, Mr. Rutten, Mr. Donlon with formatting. • The first line declares a String(a class type) Array with 4 elements. • Lines 2-5 give each element a name. • Translation of line 6: for the integer (int) index (i) equals 0 (I = 0), while the index < the length of the names array (i < names.Length), increment the Index by one ( i++) ) • The 7th line prints out each name in a fancy formatting.
Functions (methods) • Methods are pieces of code that are executed when they are called. println is a method, so is anything that has a name and a set of parenthesis (something()). • A method can return a value and take arguments. • Important: Every java program starts in a special method called main. The declaration of this method is this: public static void main(string[] args){…} • Example: • public void PrintSillyMessage(){System.out.println(“I like turtles”);}…public static void main(string[] args){PrintSillyMessage();} • Lets break this down: • The public void PrintSillyMessage() declares a method that is accessible to everyone(public) and returns nothing (void) that is named PrintSillyMessage. • The System.out.Println function prints to the console “I like turtles”. • Inside the main function (where the program starts), it calls the PrintSillyMessage which then prints the message “I like turtles”.
Functions (methods) Cont. • Arguments to methods are inserted so: • Example: • public void PrintMessage(String message){System.out.println(“You wanted to say “ + message);}Public static void main(string[] args){PrintMessage(“I’m in a cubicle… Going crazy”);} • Lets break this down: • The first line declares a public method that returns nothing named PrintMessage that takes the string argument of message. • The second line prints out the message • The main method calls PrintMessage with the arguments of “I’m in a cubicle… Going crazy”.
Lets combine them! • Lets combined what we discussed and break it down together! • String[] names;public static void main(string[] args){ names = new String[4]; names[0] = “James”;names[1] = “Dev”;names[2] = “Mr. Rutten”;names[3] = “Mr. Donlon”;GoThroughNames();}public void GoThroughNames(){ for (inti = 0; i < names.Length; i++){ if (names[i] == “James”){System.out.println(“Dev is wrong, I am right”); } else if (names[i] == “Dev”){System.out.println(“James is wrong, I am right”); } else if (names[i] == “Mr.Rutten”){System.out.println(“Is it done yet?”); } else if (names[i] == “Mr. Donlon”){System.out.println(“I work at intel.”); } else {System.out.println(names[i]); } }}