Skip to content

Instantly share code, notes, and snippets.

@asyraffff
Last active January 24, 2021 07:25
Show Gist options
  • Save asyraffff/6dcbde00e8899ad6812fe3b28b7ca005 to your computer and use it in GitHub Desktop.
Save asyraffff/6dcbde00e8899ad6812fe3b28b7ca005 to your computer and use it in GitHub Desktop.
CheatSheet for Java 🔥. || Folow this order ; intro-1 => intro-2 => intro-3 => intro-4
package intro3.java;
public class Browser {
public void navigate(String address){
String ip = findIpAddress(address);
String html = sendHttpRequest(ip);
}
private String sendHttpRequest(String ip) {
return "<html></html>";
}
private String findIpAddress(String address) {
return "127.0.0.1";
}
}
package intro3.java;
public class Employee {
private int baseSalary;
private int hourlyRate;
// public int extraHours;
// Custom Constructor => initialized our object
// By default, Java will create an anonymous constructor with zero as a default value
// Constructor Overloading
public Employee(int baseSalary){
this(baseSalary, 0);
}
public Employee(int baseSalary, int hourlyRate){
// this.baseSalary = baseSalary;// nope
setBaseSalary(baseSalary);
setHourlyRate(hourlyRate);
}
// METHOD OVERLOADING
public int calculateWage(int extraHours){
return baseSalary + (hourlyRate * extraHours);
}
public int calculateWage(){
return baseSalary;
}
private void setBaseSalary(int baseSalary){
if (baseSalary <= 0)
throw new IllegalArgumentException("Salary cannot be 0 or less");
this.baseSalary = baseSalary;
}
// ABSTRACTION
private int getBaseSalary(){
return baseSalary;
}
private void setHourlyRate(int hourlyRate){
if (hourlyRate <= 0)
throw new IllegalArgumentException("Hourly Rate cannot be 0 or negative");
this.hourlyRate = hourlyRate;
}
// ABSTRACTION
private int getHourlyRate(){
return hourlyRate;
}
}
package com.asyraf;
// => compilation of classes
import java.awt.*;
import java.awt.desktop.SystemEventListener;
// other package, so we need to import to use it
import java.util.Arrays;
import java.util.Date;
public class Main {
// compilation of method
public static void main(String[] args) {
// class(c) . member(m) . field(f)
//System.out.println("Hello World");
//VARIABLES//🔥
// camelCase letter 🐪
// types . name/label/identifier . value
// int age = 19, room = 24; // not recommend for inline variable
// int room2 = 24; // yup
// age = 20; // can assign a new value
// int myAge = 19;
// int herAge = myAge; // can copy the value
//System.out.println(age + " " + room);
//System.out.println(herAge);
//TYPES//🔥
// 1️⃣ Primitive
// => storing simple values
// // 1) byte
// byte age = 20;
// // 2) short
// short wages = 30_000;
// // 3) int
// int ytViews = 1_234_567_891;
// // 4) long
// long funding = 3_000_000_000L; // l or L
// // 5) float
// float price = 19.99F; // f or F
// // 6) double
// double housePrice = 12_345_678.99;
// // 7) char
// char letter = 'a'; // char = '' , string = ""
// // 8) boolean
// boolean isFinished = true;
// 2️⃣ Reference (non-primitive)
// => storing complex objects = date, mail message
// we need to allocate memory
// Date now = new Date();
// now is an instance of Date class
// now = object , Date = class
// class = Person, object = Asyraf, Raju, Kim...
// System.out.println(now);
//Primitive vs Reference//🔥
// byte x = 1;
// x = 3;
// byte y = x;
// x = 2;
// System.out.println(y);
// System.out.println(x);
// x and y is independent (primitive)
// Point point1 = new Point(1, 2);
// Point point2 = point1;
// point1.x = 3;
// System.out.println(point2);
// point1 and point2 refer to the same memory location (reference)
//String//🔥
// reference type
// String message = new String("Hello World!!"); //'new String' is redundant
// String newMessage = " Hello World "; //shortcut 🙂
// System.out.println(newMessage);
// message = class
// message.endsWith("ld");
// System.out.println(message.endsWith("ld"));
// System.out.println(message.startsWith("Hell"));
// System.out.println(message.length());
// System.out.println(message.replace("!", "*")); // does not modify the original string
// // String is a immutable, we cannot change them ‼️
// System.out.println(message);
// // parameter: What variable we give to method/function
// // argument: the value for the parameter
// System.out.println(newMessage.trim());
//ESCAPE SEQUENCE//🔥
// String slash = "Don't worry dude";
// String doubleQuote = "Try \"Double Quote\"";
// String backSlash = "c:\\windows\\user\\....";
// String newLineAndTab = "Hi👋, \n\tI'm Asyraf\n\t\tnice to meet you";
// System.out.println(slash);
// System.out.println(doubleQuote);
// System.out.println(backSlash);
// System.out.println(newLineAndTab);
//ARRAYS//🔥
// Reference type
// older syntax ;
// int[] oldArrays = new int[4];
// oldArrays[0] = 2;
// oldArrays[1] = 5;
// //numbers[9] = 8; => Error
// System.out.println(oldArrays); // Java will print the address for that memory
// // and each new array will give different string
// System.out.println(Arrays.toString(oldArrays)); // method overloading
// // New syntax ;
// int[] newArrays = {4,5,2,6,7};
// Arrays.sort(newArrays);
// int[][] matrix = new int[3][2]; // 3 rows, 2 column
// matrix[2][0] = 5;
// System.out.println(Arrays.toString(matrix)); // weird string 😂
// System.out.println(Arrays.deepToString(matrix)); // special for multi-dimensional array
/// // new syntax
// int[][] twoD = {{2,3}, {5,7}}; // 2 rows, 2 column
// System.out.println(Arrays.deepToString(twoD));
// System.out.println(Arrays.toString(newArrays));
//CONSTANT//🔥
// final
// final float PI = 3.1415926535F; // cast to Float
// pi = 4; // error
//ARITHMETIC EXPRESSION//🔥
// int mehDivision = 22/7;
// double coolDivision = 22D/7D;
// System.out.println(mehDivision);
// System.out.println(coolDivision);
// // Augmented/Compound operator
// int x = 5;
// x += 4;
//CASTING//🔥
// Compatible type only => number with number
// Implicit casting(automatically) => we don't need to worry about it
// byte > short > int > long > float > double
// short x = 2;
// int y = x + 2;
// System.out.println(y);
// // Explicit casting => we need to cast Myself
// double a = 8.5;
// int b = (int)a + 5;
// System.out.println(b);
// // Non-compatible type => number with string
// String g = "6";
// // int p = g + 4; // error
// int p = Integer.parseInt(g) + 2;
// System.out.println(p);
}
}
package intro2.java;
public class Main {
public static void main(String[] args) {
// greetUser("Amirul", "Asyraf");
//
// String message = sayHi("Asyraf");
// System.out.println(message);
//ERRORS//🔥
//1️⃣ Compile-time Error
//2️⃣ Run-time Error
System.out.println("Start");
printNumbers(4);
System.out.println("End");
}
//METHODS//🔥
// void => return statement
// public static void greetUser(String firstname, String lastname){
// System.out.println("Hello " + firstname + " " + lastname);
// }
// // String => return value
// public static String sayHi(String name){
// return "HI " + name;
// }
public static void printNumbers(int limit){
for(int i = 1; i <= limit; i++){
System.out.println(i);
}
}
}
package intro3.java;
public class Main {
public static void main(String[] args) {
// Object => new instance of the class
// TextBox textBox1 = new TextBox(); // noisy
// var textBox1 = new TextBox(); // see TextBox Class below
// textBox1 = 1; // error => different with var in javascript
// textBox1.setText("Hello Dude🚀");
// // If we don't initiate reference type variable(String) => null will be default
// System.out.println(textBox1.text.toUpperCase());
// var textBox2 = new TextBox();
// textBox2.setText("Have a nice Day!!");
// System.out.println(textBox2.text);
// var textBox3 = new TextBox();
// var textBox4 = textBox3;
// textBox4.setText("Box 4");
// System.out.println(textBox3.text);
// System.out.println(textBox4.text);
// PROCEDURAL PROGRAMMING
// int baseSalary = 50_000;
// int extraHours = 10;
// int hourlyRate = 20;
// int wage = calculateWage(baseSalary, extraHours, hourlyRate);
// System.out.println("Min Wage of Employee : " + wage);
// Encapsulation 🔥
var employee1 = new Employee(60_000, 25);
// employee1.baseSalary = 60_000;
// employee1.setBaseSalary(60_000);
// employee1.setHourlyRate(10);
int wage1 = employee1.calculateWage(20);
System.out.println("Wage for Employee 1 : " + wage1);
// ABSTRACTION
var browser = new Browser();
}
// public static int calculateWage(int baseSalary, int extraHours, int hourlyRate){
// return baseSalary + (extraHours * hourlyRate);
// }
}
package inheritance.cool;
public class Main {
public static void main(String[] args){
// var control = new UIControl();
// var control = new TextBox2();
// control.disable();
// System.out.println(control.isEnabled());
//
// // All classes inherit by default with Object Class
// var obj = new Object();
//
// var box1 = new TextBox2();
// var box2 = box1;
// System.out.println(box1.hashCode());
// System.out.println(box2.hashCode());
// System.out.println(box1.equals(box2));
// var control = new UIControl(true);
// control.disable();
// System.out.println(control);
//
// var textbox = new TextBox2();
// textbox.setText("Hello World");
// System.out.println(textbox.toString());
}
}
package intro3.java;
// public => access modifier => determine whether other classes can use this class
public class TextBox {
// FIELD
public String text = ""; // if don't initiate , it will give NullError
// METHOD
public void setText(String text){
this.text = text;
}
public void clear(){
// this.text = "";
text = "";
}
}
package inheritance.cool;
// extends => Inheritance
public class TextBox2 extends UIControl{
private String text = "";
public TextBox() {
// we need to specify the base constructor first
super(true);
System.out.println("TextBox");
// super(true); // error
}
@Override
public String toString(){
return text;
}
public void setText(String text){
this.text = text;
}
public void clear(){
text = "";
}
}
package inheritance.cool;
public class UIControl {
private boolean isEnabled = true;
public UIControl(boolean isEnabled) {
this.isEnabled = isEnabled;
System.out.println("UIControl");
}
public void enable(){
isEnabled = true;
}
public void disable(){
isEnabled = false;
}
public boolean isEnabled(){
return isEnabled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment