1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-11-23 21:27:40 +00:00

Merge branch 'master' of github.com:LeCoupa/awesome-cheatsheets

This commit is contained in:
Julien Le Coupanec 2022-04-15 22:01:16 +02:00
commit 2137ac0559

View File

@ -12,12 +12,12 @@ public class HelloWorld {
### COMPILATION & EXECUTING JAVA CODE ### COMPILATION & EXECUTING JAVA CODE
* Goto your program directory in terminal (Assumed JAVA Path is set) * Go to your program directory in terminal (Assumed JAVA Path is set)
* After for compile your code * After for compile your code
> **javac HelloWorld.java (your program file name)** > **javac HelloWorld.java (your program file name)**
* For run program * For run program
> **java HelloWorld (main class name)** > **java HelloWorld (main class name)**
@ -61,13 +61,13 @@ int c = a + b;
### PRINTING ### PRINTING
```java ```java
String s = "Happy Coding Folks!!" String s = "Happy Coding Folks!!"
void System.out.print(String s) //print s System.out.print(String s) //print s
void System.out.println(String s) //print s, followed by a newline System.out.println(String s) //print s, followed by a newline
void System.out.println() //print a newline System.out.println() //print a newline
``` ```
### PARSING COMMAND-LINE ARGUMENTS ### PARSING COMMAND-LINE ARGUMENTS
```java ```java
String s = "Java is the best!!" String s = "Java is the best!!"
int Integer.parseInt(String s) //convert s to an int value int Integer.parseInt(String s) //convert s to an int value
double Double.parseDouble(String) //convert s to a double value double Double.parseDouble(String) //convert s to a double value
@ -111,12 +111,12 @@ int c = a + b;
if (x>y) { // x > y is the boolean expression if (x>y) { // x > y is the boolean expression
//Sequence of statements //Sequence of statements
x = y; x = y;
} }
``` ```
> IF-ELSE STATEMENT > IF-ELSE STATEMENT
```java ```java
if (BOOLEAN EXPRESSION) { if (BOOLEAN EXPRESSION) {
//Sequence of statements //Sequence of statements
} else { } else {
//Sequence of statements //Sequence of statements
@ -125,7 +125,7 @@ int c = a + b;
> NESTED IF STATEMENT > NESTED IF STATEMENT
```java ```java
if (BOOLEAN EXPRESSION) { if (BOOLEAN EXPRESSION) {
//Sequence of statements //Sequence of statements
} else if { } else if {
//Sequence of statements //Sequence of statements
@ -189,7 +189,7 @@ int c = a + b;
{ {
//Statement //Statement
} }
``` ```
**Example:** **Example:**
```java ```java
for (int i = 0; i <= n; i++) { for (int i = 0; i <= n; i++) {
@ -197,7 +197,7 @@ int c = a + b;
} }
``` ```
>Enhanced for loop/for-each >Enhanced for loop/for-each
```java ```java
for(dataType item : array) { for(dataType item : array) {
... ...
@ -208,8 +208,8 @@ for(dataType item : array) {
```java ```java
// array of numbers // array of numbers
int[] numbers = {100, 200, 300, 400}; int[] numbers = {100, 200, 300, 400};
// for each loop // for each loop
for (int number: numbers) { for (int number: numbers) {
System.out.println(number); System.out.println(number);
``` ```
@ -217,7 +217,7 @@ for(dataType item : array) {
> WHILE LOOP STATEMENT > WHILE LOOP STATEMENT
```java ```java
while(condition){ //till condition will be true. while(condition){ //till condition will be true.
//code to be executed //code to be executed
} }
``` ```
**Example:** **Example:**
@ -245,7 +245,7 @@ for(dataType item : array) {
do{ do{
System.out.println(i); System.out.println(i);
i++; i++;
}while(i<=10); }while(i<=10);
``` ```
### ARRAY ### ARRAY
@ -273,19 +273,19 @@ for(dataType item : array) {
### ACCESS MODIFIERS ### ACCESS MODIFIERS
1. defualt(No keyword required) 1. defualt(No keyword required)
2. private 2. private
3. public 3. public
4. protected 4. protected
### NON ACCESS MODIFIERS ### NON ACCESS MODIFIERS
1. static 1. static
2. final 2. final
3. transient 3. transient
4. abstract 4. abstract
5. synchronized 5. synchronized
6. volatile 6. volatile
## Object Oriented Programming (OOPs) Concept :clipboard: ## Object Oriented Programming (OOPs) Concept :clipboard:
@ -324,7 +324,7 @@ for(dataType item : array) {
### CLASS DECLARATION ### CLASS DECLARATION
```java ```java
class MyClass { class MyClass {
// field, constructor, and // field, constructor, and
// method declarations // method declarations
} }
``` ```
@ -377,11 +377,11 @@ class MyClass extends MySuperClass implements YourInterface {
```java ```java
public class Bicycle{ public class Bicycle{
private int gear; private int gear;
private int cadence; private int cadence;
private int speed; private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) { //args-constructor public Bicycle(int startCadence, int startSpeed, int startGear) { //args-constructor
gear = startGear; gear = startGear;
cadence = startCadence; cadence = startCadence;
@ -396,13 +396,13 @@ class MyClass extends MySuperClass implements YourInterface {
### POLYMORPHISM ### POLYMORPHISM
* Polymorphism is the concept where an object behaves differently in different situations. * Polymorphism is the concept where an object behaves differently in different situations.
* There are two types of polymorphism * There are two types of polymorphism
1. compile time polymorphism 1. compile time polymorphism
2. runtime polymorphism. 2. runtime polymorphism.
#### 1. Compile Time Polymorphism #### 1. Compile Time Polymorphism
* Compile-time polymorphism is achieved by method overloading. * Compile-time polymorphism is achieved by method overloading.
* method overloading is creating multiple method with methods name is same and arguments are different. * method overloading is creating multiple method with methods name is same and arguments are different.
```java ```java
public class Circle { public class Circle {
@ -461,7 +461,7 @@ class MyClass extends MySuperClass implements YourInterface {
### INHERITANCE ### INHERITANCE
* Inheritance is the mechanism of code reuse. * Inheritance is the mechanism of code reuse.
* The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass. * The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass.
* We use `extends` keyword in java to implement inheritance from class. * We use `extends` keyword in java to implement inheritance from class.
* We use `implements` keyword in java to implement inheritance from interface. * We use `implements` keyword in java to implement inheritance from interface.
@ -488,7 +488,7 @@ class MyClass extends MySuperClass implements YourInterface {
* Abstraction can be achieved by two ways. * Abstraction can be achieved by two ways.
1. Abstract Class 1. Abstract Class
2. Interface 2. Interface
#### 1. Abstract Class #### 1. Abstract Class
* An abstract class must be declared with an `abstract` keyword. * An abstract class must be declared with an `abstract` keyword.
* It can have abstract and non-abstract methods. * It can have abstract and non-abstract methods.
@ -499,7 +499,7 @@ class MyClass extends MySuperClass implements YourInterface {
```java ```java
abstract class Flower{ abstract class Flower{
abstract String Smell(); //abstract method. abstract String Smell(); //abstract method.
String Oil(){ // non-abstract method. String Oil(){ // non-abstract method.
System.out.println("Flower Oil is good."); System.out.println("Flower Oil is good.");
} }
} }
@ -535,7 +535,7 @@ class MyClass extends MySuperClass implements YourInterface {
* As in encapsulation, the data in a class is hidden from other classes, so it is also known as **data-hiding**. * As in encapsulation, the data in a class is hidden from other classes, so it is also known as **data-hiding**.
* Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. * Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
* Best example of Encapsulation is POJO (Plain-Java-Object-Class). * Best example of Encapsulation is POJO (Plain-Java-Object-Class).
```java ```java
public class User { public class User {
private String username; private String username;
@ -558,7 +558,7 @@ class MyClass extends MySuperClass implements YourInterface {
} }
} }
``` ```
## ADVANCE DATA TYPE ## ADVANCE DATA TYPE
* **STACK DATA TYPE** * **STACK DATA TYPE**
@ -591,7 +591,7 @@ class MyClass extends MySuperClass implements YourInterface {
//import Iterator //import Iterator
import java.util.Iterator; import java.util.Iterator;
public class Queue<Item> implements Iterable<Item> { public class Queue<Item> implements Iterable<Item> {
//FIFO queue //FIFO queue
private Node first; private Node first;