0% found this document useful (0 votes)
6 views

Son9912 Java Oop Concept

Uploaded by

Leng Hongmeng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Son9912 Java Oop Concept

Uploaded by

Leng Hongmeng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java + OOP concept Cheat Sheet

by Kunanon S (son9912) via cheatography.com/43384/cs/12920/

Hello World! Operators Loop

Start your Java day with Hello World program Operand What they do  for (int i: someArray) {}
public class HelloWorld { = Assign value  while (somet​hing) {}
public static void main(S​tring[] args) {  do {somet​hing} while (true)
== Check value/​address similarity
// Prints "​Hello, World" to the terminal window.
> More than
System.ou​t.p​rin​tln​("Hello, World"); Prime number function
>= More than or equals
} if (n < 2) { return false; }
} >>> Move bit to the right by
for (int i=2; i <= n/i; i++)
When you want to run the program, choose this class
++ as main Increment
class. by 1 ​ if (n%i == 0) return false;
inverse of these operands still working the return true;
Run your code
same.
 returns a boolean
Compile from single class up HelloWorld class For example : != is not equal
 javac HelloW​orl​d.java
String Pool - Optimi​zations
 java HelloWorld Defining variable
String pool is created to make the same
Compile from multiple classes and choose main class
Defining new variable attributes
value string use the same address. By
 javac *.java int x = 12;
doing that, it will save memory and time for
 java HelloWorld // HelloWorld is your
int preferred
x; // willmain cl
be defined as 0
compiler to do stuff
ass Define by creating new instances Basic testing
String x = new String; String s1 = "​Hello World";
Variables Type Casting (decre​asing bit use) String s2 = "​Hello World;
Type Default Memory Allocation Expanding data types will not require type casting. Narrowing
Check it using "​=="
Value does.
 System.ou​t.p​rin​tln(s1 == s2
double x = 10; // Expanding data types
byte 0 8 bits );
int y = (int) 10.222222; // Narrowing data types
short 0 16 bits  True

int 0 32 bits "​==" will check its address


Conditions
Allocate a new address using new
long 0L 64 bits
If statement String s1 = "​Hello World";
float 0.0F 32 bits (decimal)  if (state​ment) {} String s2 = new String;
double 0.00D 64 bits (decimal) If - else statement s2 = "​Hello World";
boolean False varies on impliment  if (state​ment) {} else{} System.ou​t.p​rin​tln(s1 == s2);
String NULL depends on  False
Switch Allocate new address by changing its value
character count
char \u0000 16 bits (unicode) switch (num) { String s1 = "​Hello World";
​ case 1: doSome​thi​ng(); String s2 = "​Hello World";
​ ​ ​ ​break; s2 = "​Hello Thaila​nd";
​ ​def​ault: doThis(); System.ou​t.p​rin​tln(s1 == s2);
​ ​ ​ ​break;  False
}

By Kunanon S (son9912) Published 25th September, 2017. Sponsored by ApolloPad.com


cheatography.com/son9912/ Last updated 29th September, 2017. Everyone has a novel in them. Finish
Page 1 of 4. Yours!
https://apollopad.com
Java + OOP concept Cheat Sheet
by Kunanon S (son9912) via cheatography.com/43384/cs/12920/

Naming Grammars Access Modifier Constr​uctor (cont)

Naming should be regulated for easier But will be created automa​tically by not
recogition from others writing any constr​uctor
Use Upper Camel Case for classes : Veloc Create an argume​nt-​defined constr​uctor
i​tyR​esp​ons​eWriter  <mo​dif​ier> Person (String nam
Use Lower Case for packages: com.co​mp e) {
a​ny.p​ro​ject.ui ​ ​thi​s.name = name;
Use Lower Camel Case for variables: stud }
en​tName
Use Upper Case for constants: MAX_PA​RA - Java uses <de​fau​lt> modifier when Abstract Class
M​ETE​R_COUNT = 100 not assigning any. Abstract is a type of class but it can consist of incomple
Use Camel Case for enum class names - public modifier allows same class Create new abstract
Use Upper Case for enum values access  <ac​ces​s_m​odi​fie​r> abstract class
Don't use '_' anywhere except constants - Works in inherited class means itself and d () {}
and enum values (which are consta​nts). the classes that inherit from it.
Interface
Receiving user input Attribute modifier
Interface is different from constr​uctor. It
There is normally 2 ways to receive user keyboardAttribute
input Access Grants consists of incomplete assign​ments
1. java.u​til.Sc​anner Type Interface allows you to make sure that any
Scanner x = new Scanne​r(S​yst​em.in); inherited class can do the following methods.
Private Allows only in class where
String inputS​tring = x.next(); // for String type input (It's like a contract to agree that this thing must
variable belongs
int inputI​nteger = x.next​Int(); // for Integer type inpu be able to do this shit.) The method is then
Public Allows any class to have this
t completed in the class that implements it.
attribute
2. String[] args from public static void main() Creating a new interface
Static Attribute that dependent on
NOTE: args is already in a array. It can receives unlimited amount of interface Bicycle {
class (not object)
arguments. ​ void speedUp (int increm​ent);
String inputS​tring = args[0]; // forFinal Defined
String type once. Does not allow
input }
any change​
type /inpu
in​her​itance
Int inputS​tring = (int) args[0]; // for Integer ----
t class fuckBike implements Bicycle {
Methods
To use Scanner, importing Scanner library ​ ...
Methods are fucking easy, dud. ​ void speedUp (int increment) {
is required : import java.O​bje​ct.S​‐
 <mo​d> <re​tur​n> mthdName (<a​rgs ​ ​ ​ ​speed += increment;
canner
​>) { } ​ }
Example: ​ ...
All types of input can be received. (not just
 public double getAge () {
String or int) }
​ ​return someDo​uble;
}

Constr​uctor

Constr​uctors allow you to create an object


template. It consists of complete
procedures.
Create a blank constr​uctor to allow its
extension classes to inherit this super
constr​uctor.
 <mo​dif​ier> Person () {}
By Kunanon S (son9912) Published 25th September, 2017. Sponsored by ApolloPad.com
cheatography.com/son9912/ Last updated 29th September, 2017. Everyone has a novel in them. Finish
Page 2 of 4. Yours!
https://apollopad.com
Java + OOP concept Cheat Sheet
by Kunanon S (son9912) via cheatography.com/43384/cs/12920/

Encaps​ulation Overload (cont) java.u​til.Sc​anner

Encaps​ulation allows individual methods to have different


Java willaccess
not allow
modifier.
this to be run, because it Create a Scanner object
Creating setters and getters is one way to use encaps​
cannot
ulation
determine the value.  Scanner sc = new Scanne​r(S​yst​em
For example .in);
Override int second)
private void setTim​e(int hour, int minuite, Accept input
{ When you have inherit some of the class from  double d = sc.nex​tDo​uble()
this.hour = hour; parents, but you want to do something
this.m​inuite = minuite; different. In override feature, all the subcla​ss/​‐ java.l​ang.Math
this.s​econd = second; class object will use the newer method. Methods Usage
} To make sure JDK knows what you are doing,
Math.m​ax(​<va​lue​1>, <va​lu Return
type @Override in front of the public name. If
e​2>) maximum
Inheri​tance the override is unsucc​essful, JDK will returns
value
error.
Inheri​tance helps class to import the superc​lass' method. Math.m​in(​<va​lue​1>, <va​lu Return
Importing superclass Example of overriden helloW​orld() method :
e​2>) minimum va
 class HelloWorld extends Object {} Class Student
public void helloW​orld(){ Math.a​bs(​<va​lue​>) Return
Normally, the class that does not inherit any class will
System.ou​t.p​rin​tln​("He​llo​"); unsigned va
inherit Object class.*
Class can only inherit 1 class/​abs​tract } Math.p​ow(​<nu​mbe​r>, <ex​po Return valu
Importing Interface Class GradSt​udent extends Student n​ent> a
@Override
 class HelloWorld inherits Interf​ace​Thin number

g {} public void helloW​orld(){ Math.s​qrt​(<v​alu​e>) Return squa


Class can inherit unlimited amount of interface System.ou​t.p​rin​tln​("Hello Worl root of a val
d");
Overload } java.l​ang.String
Rules of Overridden methods Find the length -> int
We use overload when you want different input to work differ​ently,
1. Access modifier priority can only be
but remains the same name.  msg.le​ngth()
narrower or same as superclass To lower/​upp​ercase -> String
Example of Overload
2. There is the same name method in  msg.to​Low​erC​ase()
public printe​r(S​tring x){}
superclass / libraries
public printe​r(S​tring x, String y){}  msg.to​Upp​erC​ase()
If the input is 2 string, it will go to the second method instead of Replace a string -> String
java.i​o.P​rin​tStream
first one.  msg.re​pla​ceA​ll(​String a, String b)
But you cannot overload by using the same input type Printsequence.
with new line Split string between delimeter -> array
For example  System.ou​t.p​rin​tln​("Hello W  msg.sp​lit​(String delimeter)
public printe​r(S​tring x){} orld"); Start/end with -> boolean
Print
public printe​r(S​tring x, String y){} // confli  msg.st​art​sWi​th(​String pre)
ct  System.ou​t.p​rin​t("Hello Worl  msg.en​dsW​ith​(String post)
d");
public printe​r(S​tring y, String x){} // confli String format -> String
ct  String.fo​rma​t(S​tring format, Object
args)

By Kunanon S (son9912) Published 25th September, 2017. Sponsored by ApolloPad.com


cheatography.com/son9912/ Last updated 29th September, 2017. Everyone has a novel in them. Finish
Page 3 of 4. Yours!
https://apollopad.com
Java + OOP concept Cheat Sheet
by Kunanon S (son9912) via cheatography.com/43384/cs/12920/

java.l​ang.String java.u​til.Co​lle​ction (Colle​cti​onAPI) HashMap - Collec​tionAPI

Methods Descri​ption Provides ways to keep variables and access Method Usability
charAt(int index) Returns the char value it faster
at the specified index Ways to keep data Collec​tions
1. Set - Care about duplicity, not queue (eg.
compar​eTo​‐ Compare 2 strings Create List of 1, 2, 3 on-the-fly
HashSet)
(String otherS​‐ lexico​gra​phi​cally  Arrays.as​List(1, 2, 3)
2. List - Care about queue, not duplicity (eg.
tring) Convert primitive array to Stream
Linked​List)
concat​(String str) Concat​enate specified  Arrays.st​rea​m(p​rim​iti​veA​‐
3. Map - Care about both queue and key
string rray)
duplicity (eg.Ha​shMap)
Convert ArrayList to Stream
endsWi​th(​String Test if the string ends Methods that will be included
 arrayL​ist.st​ream()
suffix) with specified suffix boolean add(Object element);
equals​(String Test if strings values boolean remove​(Object element);
LinkedList - Collec​tionAPI
andObject) are the same int size();
boolean isEmpty(); Create empty LinkedList of Integer
toChar​Array() Convert string to
 LinkedList myList = new Linked​Lis​t<I
character array boolean contai​ns(​Object element
er​>t()
toLowe​rCase() Convert string to );
Create LinkedList with values in it
lowercase Iterator Iterat​or();
 new Linked​Lis​t<>​(Ar​ray​s.a​sLi​st(
toUppe​rCase() Convert string to 3)))
HashList - Collec​tionAPI
uppercase
Add an object to LinkedList
toString() Convert things to string Method Usability
 myList.ad​d(50)
valueO​f(<​val​ue>) Return the repres​ent​‐ void add (int index, Add value to list

ation of argument Object element)

length() Return length of the Object remove(int Remove item

string index) #index from list

replac​eAl​l(S​tring Replace string a to Object get(int index) Retrieve item

a, String b) string b #index from list

split(​String Split string between void set(int index, Set data to

delimeter) delimeter Object element) correspond #index

starts​Wit​h(S​tring Test if string starts with int indexO​f(O​bject Find the #index

prefix) specified prefix element) from element

format​(String Format strings to the ListIt​erator listIt​era​tor()

format, Object format given It also includes all Collec​tionAPI methods


arg)

There is many more in Java documents : Create new HashList by using

https:​//d​ocs.or​acl​e.c​om/​jav​ase​/9/​doc​s/a​pi/​‐ List x = new HashLi​st();

jav​a/l​ang​/St​rin​g.html

By Kunanon S (son9912) Published 25th September, 2017. Sponsored by ApolloPad.com


cheatography.com/son9912/ Last updated 29th September, 2017. Everyone has a novel in them. Finish
Page 4 of 4. Yours!
https://apollopad.com

You might also like