mirror of
https://github.com/namibia/awesome-cheatsheets.git
synced 2024-12-22 10:08:54 +00:00
Merge branch 'master' of github.com:LeCoupa/awesome-cheatsheets
This commit is contained in:
commit
2137ac0559
@ -12,12 +12,12 @@ public class HelloWorld {
|
||||
|
||||
### 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
|
||||
|
||||
> **javac HelloWorld.java (your program file name)**
|
||||
|
||||
* For run program
|
||||
|
||||
* For run program
|
||||
|
||||
> **java HelloWorld (main class name)**
|
||||
|
||||
@ -61,13 +61,13 @@ int c = a + b;
|
||||
### PRINTING
|
||||
```java
|
||||
String s = "Happy Coding Folks!!"
|
||||
void System.out.print(String s) //print s
|
||||
void System.out.println(String s) //print s, followed by a newline
|
||||
void System.out.println() //print a newline
|
||||
System.out.print(String s) //print s
|
||||
System.out.println(String s) //print s, followed by a newline
|
||||
System.out.println() //print a newline
|
||||
```
|
||||
|
||||
### PARSING COMMAND-LINE ARGUMENTS
|
||||
```java
|
||||
```java
|
||||
String s = "Java is the best!!"
|
||||
int Integer.parseInt(String s) //convert s to an int 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
|
||||
//Sequence of statements
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> IF-ELSE STATEMENT
|
||||
```java
|
||||
if (BOOLEAN EXPRESSION) {
|
||||
if (BOOLEAN EXPRESSION) {
|
||||
//Sequence of statements
|
||||
} else {
|
||||
//Sequence of statements
|
||||
@ -125,7 +125,7 @@ int c = a + b;
|
||||
|
||||
> NESTED IF STATEMENT
|
||||
```java
|
||||
if (BOOLEAN EXPRESSION) {
|
||||
if (BOOLEAN EXPRESSION) {
|
||||
//Sequence of statements
|
||||
} else if {
|
||||
//Sequence of statements
|
||||
@ -189,7 +189,7 @@ int c = a + b;
|
||||
{
|
||||
//Statement
|
||||
}
|
||||
```
|
||||
```
|
||||
**Example:**
|
||||
```java
|
||||
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
|
||||
for(dataType item : array) {
|
||||
...
|
||||
@ -208,8 +208,8 @@ for(dataType item : array) {
|
||||
```java
|
||||
// array of numbers
|
||||
int[] numbers = {100, 200, 300, 400};
|
||||
|
||||
// for each loop
|
||||
|
||||
// for each loop
|
||||
for (int number: numbers) {
|
||||
System.out.println(number);
|
||||
```
|
||||
@ -217,7 +217,7 @@ for(dataType item : array) {
|
||||
> WHILE LOOP STATEMENT
|
||||
```java
|
||||
while(condition){ //till condition will be true.
|
||||
//code to be executed
|
||||
//code to be executed
|
||||
}
|
||||
```
|
||||
**Example:**
|
||||
@ -245,7 +245,7 @@ for(dataType item : array) {
|
||||
do{
|
||||
System.out.println(i);
|
||||
i++;
|
||||
}while(i<=10);
|
||||
}while(i<=10);
|
||||
```
|
||||
|
||||
### ARRAY
|
||||
@ -273,19 +273,19 @@ for(dataType item : array) {
|
||||
|
||||
### ACCESS MODIFIERS
|
||||
|
||||
1. defualt(No keyword required)
|
||||
1. defualt(No keyword required)
|
||||
2. private
|
||||
3. public
|
||||
4. protected
|
||||
4. protected
|
||||
|
||||
### NON ACCESS MODIFIERS
|
||||
|
||||
1. static
|
||||
2. final
|
||||
1. static
|
||||
2. final
|
||||
3. transient
|
||||
4. abstract
|
||||
5. synchronized
|
||||
6. volatile
|
||||
6. volatile
|
||||
|
||||
## Object Oriented Programming (OOPs) Concept :clipboard:
|
||||
|
||||
@ -324,7 +324,7 @@ for(dataType item : array) {
|
||||
### CLASS DECLARATION
|
||||
```java
|
||||
class MyClass {
|
||||
// field, constructor, and
|
||||
// field, constructor, and
|
||||
// method declarations
|
||||
}
|
||||
```
|
||||
@ -377,11 +377,11 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
|
||||
```java
|
||||
public class Bicycle{
|
||||
|
||||
|
||||
private int gear;
|
||||
private int cadence;
|
||||
private int speed;
|
||||
|
||||
|
||||
public Bicycle(int startCadence, int startSpeed, int startGear) { //args-constructor
|
||||
gear = startGear;
|
||||
cadence = startCadence;
|
||||
@ -396,13 +396,13 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
|
||||
### POLYMORPHISM
|
||||
* Polymorphism is the concept where an object behaves differently in different situations.
|
||||
* There are two types of polymorphism
|
||||
1. compile time polymorphism
|
||||
* There are two types of polymorphism
|
||||
1. compile time polymorphism
|
||||
2. runtime polymorphism.
|
||||
|
||||
#### 1. Compile Time Polymorphism
|
||||
* 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
|
||||
public class Circle {
|
||||
|
||||
@ -461,7 +461,7 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
|
||||
### 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.
|
||||
* We use `extends` keyword in java to implement inheritance from class.
|
||||
* 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.
|
||||
1. Abstract Class
|
||||
2. Interface
|
||||
|
||||
|
||||
#### 1. Abstract Class
|
||||
* An abstract class must be declared with an `abstract` keyword.
|
||||
* It can have abstract and non-abstract methods.
|
||||
@ -499,7 +499,7 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
```java
|
||||
abstract class Flower{
|
||||
abstract String Smell(); //abstract method.
|
||||
String Oil(){ // non-abstract method.
|
||||
String Oil(){ // non-abstract method.
|
||||
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**.
|
||||
* 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).
|
||||
|
||||
|
||||
```java
|
||||
public class User {
|
||||
private String username;
|
||||
@ -558,7 +558,7 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## ADVANCE DATA TYPE
|
||||
* **STACK DATA TYPE**
|
||||
@ -591,7 +591,7 @@ class MyClass extends MySuperClass implements YourInterface {
|
||||
//import Iterator
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Queue<Item> implements Iterable<Item> {
|
||||
public class Queue<Item> implements Iterable<Item> {
|
||||
|
||||
//FIFO queue
|
||||
private Node first;
|
||||
|
Loading…
Reference in New Issue
Block a user