Skip to content

Instantly share code, notes, and snippets.

@bytao7mao
Last active July 28, 2017 14:55
Show Gist options
  • Save bytao7mao/ee00057023813431601ef570c436b1c0 to your computer and use it in GitHub Desktop.
Save bytao7mao/ee00057023813431601ef570c436b1c0 to your computer and use it in GitHub Desktop.
1.
public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.
static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.
void means that the method has no return value. If the method returned an int you would write int instead of void.
--------------------------------------------------------------------------
2.The precedence of each Boolean operator is as follows:
! is evaluated first
&& is evaluated second
|| is evaluated third
3. TERNARY STATEMENT
int pointsScored = 21;
char gameResult = (pointsScored > 20) ? 'W' : 'L';
System.out.println(gameResult);
(These three parts are:
A Boolean expression
A single statement that gets executed if the Boolean expression is true
A single statement that gets executed if the Boolean expression is false)
------------------------------------------------------------------------------
public
It means that you can call this method from outside of the class you are currently in. This is necessary because this method is being called by the Java runtime system which is not located in your current class.
static
When the JVM makes call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.
void
Java is platform independent language and if it will return some value then the value may mean different things to different platforms. Also there are other ways to exit the program on a multithreaded system. Detailed explaination.
main
It's just the name of method. This name is fixed and as it's called by the JVM as entry point for an application.
String args[]
These are the arguments of type String that your Java application accepts when you run it.
-----------------------------------------
Class: a blueprint for how a data structure should function
Constructor: instructs the class to set up the initial state of an object
Object: instance of a class that stores the state of a class
Method: set of instructions that can be called on an object
Parameter: values that can be specified when creating an object or calling a method
Return value: specifies the data type that a method will return after it runs
Inheritance: allows one class to use functionality defined in another class
------------------------------------------------------------------
ArrayList example------
import java.util.ArrayList;
public class TemperaturesC {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
weeklyTemperatures.add(2, 111);
/*for (Integer temperature : weeklyTemperatures) {
System.out.println(temperature);
} ^^^^^^^^^^^^^^^^^^ shortcut for the code beneath^^^^^^^^^^^^^^^^^^^^*/
for (int j = 0; j < weeklyTemperatures.size(); j++) {
System.out.println( weeklyTemperatures.get(j) );
}
}
}
----------------------------------------------------------
MENU----------
import java.util.HashMap;
public class RestaurantForEach {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
System.out.println(restaurantMenu.size());
for (String item: restaurantMenu.keySet()) {
System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
}
}
}
----------------------------------------------------------------------------------
import java.util.*;
public class GeneralizationsD {
public static void main(String[] args) {
ArrayList<String> sports = new ArrayList<String>();
sports.add("Football");
sports.add("Boxing");
for(String sport : sports) {
System.out.println(sport);
}
//Major cities and the year they were founded
HashMap<String, Integer> majorCities = new HashMap<String, Integer>();
majorCities.put("New York", 1624);
majorCities.put("London", 43);
majorCities.put("Mexico City", 1521);
majorCities.put("Sao Paulo", 1554);
for ( String city : majorCities.keySet() ) {
System.out.println(city + " was founded in " + majorCities.get(city));
}
}
}
@bytao7mao
Copy link
Author

Started JAVA - 25.07.2017 - ETA to finish a mini game = 3 months

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment