Skip to content

Instantly share code, notes, and snippets.

@madevelopers
madevelopers / gist:8b5e7371402bb99a96c2
Created June 29, 2015 05:29
How to print Arrays to the console in Java #java #flashcard

Use Arrays.toString(arr) or Arrays.deepToString(arr) from java.util.Arrays.

int[] intArray = new int[] {1, 2, 3, 4, 5};
String[] strArray = new String[] {"John", "Mary", "Bob"};

System.out.println(Arrays.toString(intArray));
System.out.println(Arrays.toString(strArray));
@madevelopers
madevelopers / gist:fc6257d9df0521dc435c
Created June 27, 2015 09:01
How to compare strings in Java #java #flashcard

To compare Strings, we have to use the equals and compareTo methods. For example:

String name1 = "Alan Turing";
String name2 = "Ada Lovelace";

if (name1.equals (name2)) {
  System.out.println("The names are the same.");
}
@madevelopers
madevelopers / gist:f5c10a802ec1848e8270
Created June 26, 2015 02:48
How to do I get the current index in a forEach loop in Java #java #flashcard

You can't. You need to keep track of the index.

int[] nums = new int[]{1, 2, 3, 4, 5};

int index = 0;
for (int i: nums) {
	System.out.println(index + ": " + i);			
	index++;
}
@madevelopers
madevelopers / gist:7802a78c378789a55315
Created June 26, 2015 02:45
How to do a forEach loop in Java #java #flashcard
int[] nums = new int[]{1, 2, 3, 4, 5};
		
for (int i: nums) {
	System.out.println(i);
}
@madevelopers
madevelopers / gist:5244ba5b27086c43b942
Created June 26, 2015 02:31
How to create a method that returns something in Java #java #flashcard

Declare the return type of the method. In this case we'll return the combined name which is a String.

public static String combineName(String firstName, String lastName) {
  return firstName + " " + lastName;
}
@madevelopers
madevelopers / gist:cb053e48d0a418219dfc
Last active August 29, 2015 14:23
How to create a method that returns nothing in Java #java #flashcard

Use the void keyword.

public static void doSomething() {
  // do something here
}
@madevelopers
madevelopers / gist:65984271841bdf61e0a9
Created June 25, 2015 10:10
How to typecast in Java #java #flashcard

The syntax for typecasting is to put the name of the type in parentheses and use it as an operator.

Example:

double pi = 3.14159;
int x = (int) pi;
@madevelopers
madevelopers / gist:0c1bafb6f9079c95f232
Created June 25, 2015 09:39
How to concatenate strings in Java #java #flashcard

Use the + operator.

Example:

System.out.println("Hello" + " World");
@madevelopers
madevelopers / gist:f1f756e067764e7595f9
Last active August 29, 2015 14:23
What are the reserved words/keywords in Java #java #flashcard

Java's reserved words includes: public, private, static, void and many more. See the full list here.

@madevelopers
madevelopers / gist:6a8d14b08cee2c64a8bc
Last active August 29, 2015 14:23
How to assign values to variables in Java #java #flashcard
String firstName;
String lastName;
int minute, hour;

firstName = "Mark Anthony";
lastName = "Degamo";
minute = 59;
hour = 12;