Son9912 Java Oop Concept
Son9912 Java Oop Concept
Start your Java day with Hello World program Operand What they do for (int i: someArray) {}
public class HelloWorld { = Assign value while (something) {}
public static void main(String[] args) { do {something} while (true)
== Check value/address similarity
// Prints "Hello, World" to the terminal window.
> More than
System.out.println("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 HelloWorld.java
String Pool - Optimizations
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 (decreasing 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.out.println(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
Naming should be regulated for easier But will be created automatically by not
recogition from others writing any constructor
Use Upper Camel Case for classes : Veloc Create an argument-defined constructor
ityResponseWriter <modifier> Person (String nam
Use Lower Case for packages: com.comp e) {
any.project.ui this.name = name;
Use Lower Camel Case for variables: stud }
entName
Use Upper Case for constants: MAX_PARA - Java uses <default> modifier when Abstract Class
METER_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 <access_modifier> abstract class
Don't use '_' anywhere except constants - Works in inherited class means itself and d () {}
and enum values (which are constants). the classes that inherit from it.
Interface
Receiving user input Attribute modifier
Interface is different from constructor. It
There is normally 2 ways to receive user keyboardAttribute
input Access Grants consists of incomplete assignments
1. java.util.Scanner Type Interface allows you to make sure that any
Scanner x = new Scanner(System.in); inherited class can do the following methods.
Private Allows only in class where
String inputString = x.next(); // for String type input (It's like a contract to agree that this thing must
variable belongs
int inputInteger = x.nextInt(); // 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 increment);
String inputString = args[0]; // forFinal Defined
String type once. Does not allow
input }
any change
type /inpu
inheritance
Int inputString = (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.Object.S‐
<mod> <return> mthdName (<args speed += increment;
canner
>) { } }
Example: ...
All types of input can be received. (not just
public double getAge () {
String or int) }
return someDouble;
}
Constructor
Methods Description 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 Collections
1. Set - Care about duplicity, not queue (eg.
compareTo‐ Compare 2 strings Create List of 1, 2, 3 on-the-fly
HashSet)
(String otherS‐ lexicographically Arrays.asList(1, 2, 3)
2. List - Care about queue, not duplicity (eg.
tring) Convert primitive array to Stream
LinkedList)
concat(String str) Concatenate specified Arrays.stream(primitiveA‐
3. Map - Care about both queue and key
string rray)
duplicity (eg.HashMap)
Convert ArrayList to Stream
endsWith(String Test if the string ends Methods that will be included
arrayList.stream()
suffix) with specified suffix boolean add(Object element);
equals(String Test if strings values boolean remove(Object element);
LinkedList - CollectionAPI
andObject) are the same int size();
boolean isEmpty(); Create empty LinkedList of Integer
toCharArray() Convert string to
LinkedList myList = new LinkedList<I
character array boolean contains(Object element
er>t()
toLowerCase() Convert string to );
Create LinkedList with values in it
lowercase Iterator Iterator();
new LinkedList<>(Arrays.asList(
toUpperCase() Convert string to 3)))
HashList - CollectionAPI
uppercase
Add an object to LinkedList
toString() Convert things to string Method Usability
myList.add(50)
valueOf(<value>) Return the represent‐ void add (int index, Add value to list
startsWith(String Test if string starts with int indexOf(Object Find the #index
java/lang/String.html