JAVA Quick Reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JAVA QUICK REFERENCE - This is single line comment. | |
import java.util.Scanner; | |
/* This is a multi line comment | |
A public class named 'HelloWorld' */ | |
public class HelloWorld { | |
/* This is a 'public' & 'static' method. | |
Static method belongs to class, | |
Not to its instance. */ | |
public static void main(String [] args) { | |
// This is a statement | |
System.out.println("Hello World!"); | |
// Built in data types | |
int weeks = 5; | |
double interestRate = 8.75; | |
char initial = 's'; | |
String day = "Monday"; | |
Boolean isHoliday = false; | |
// Arithmetic operators | |
int add = 1 + 2; // 3 | |
int subtract = 6 - 3; // 3 | |
int multiply = 2 * 3; // 6 | |
int divide = 12 / 6; // 2 | |
int remainder = 15 % 2; // 1 | |
// variables | |
int inputX = 1; | |
int inputY = 2; | |
// Comparison operators | |
Boolean equal = inputX == inputY; // false | |
Boolean notEqual = inputX != inputY; // true | |
Boolean lessThan = inputX < inputY; // true | |
Boolean lessThanOrEqual = inputX <= inputY; // true | |
Boolean greaterThan = inputX > inputY; // false | |
Boolean greaterThanOrEqual = inputX >= inputY; // false | |
System.out.print("Hello"); // Prints 'Hello' | |
System.out.println(day); // Prints 'Monday' & a newline | |
System.out.println(); // Prints a newline | |
// Read input from stdin | |
Scanner scanner = new Scanner(System.in); | |
String input = scanner.nextLine(); | |
int number = scanner.nextInt(); | |
double fraction = scanner.nextDouble(); | |
// Parsing inputs | |
int x = Integer.parseInt(input); | |
double y = Double.parseDouble(input); | |
boolean z = Boolean.parseBoolean(input); | |
} | |
} | |
public class HelloAgain { | |
public static void main(String[] args) { | |
int x = 1; int y = 2; int weeks = 5; | |
double interest = 8.75; | |
if(x > y) { // IF | |
System.out.println("X is greater"); | |
} | |
if(x > y) { // IF ELSE | |
System.out.println("X is greater"); | |
} | |
else { | |
System.out.println("Y is greater"); | |
} | |
if(weeks > 10) { // IF ELSE IF | |
interest = 8.25; | |
} | |
else if (weeks > 5) { | |
interest = 5.4; | |
} | |
else { | |
interest = 3.5; | |
} | |
switch (y) { // Switch case | |
case 0: | |
System.out.println("SUN"); | |
break; | |
case 1: | |
System.out.println("MON"); | |
break; | |
default: | |
System.out.println("Invalid"); | |
break; | |
} | |
int num = 0; | |
while (num < x) { // WHILE Loop | |
System.out.println(num); | |
num++; | |
} | |
do { // DO while | |
y = y - 1; | |
} while (y > 0); | |
for(int i = 0; i < 10; i++) { // FOR Loop | |
System.out.println(i); | |
} | |
// Arrays | |
int[] numbers = { 32, 65, 13, 17, 10 }; | |
int[] names = { "John", "Vivek", "Brad" }; | |
int sum = 0; | |
for (int i = 0; i < numbers.length; i++) { | |
sum = sum + numbers[i]; | |
} | |
for (int i = 0; i < numbers.length; i++) { | |
numbers[i] = numbers[i] * 2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment