技术
JAVA
- 2025-12-15
- 晓宇
HELLO WORLD :ghost:
1 | //Text file name HelloWorld.java |
COMPILATION & EXECUTING JAVA CODE
- 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
java HelloWorld (main class name)
DATA TYPES
| Type | Set of values | Values | Operators |
|---|---|---|---|
| short | integers | between -2^15 and + (2^15)-1 | + - * / % |
| int | integers | between -2^31 and + (2^31)-1 | + - * / % |
| long | integers | between -2^63 and + (2^63)-1 | + - * / % |
| float | integers | real numbers 32 bit | + - * / |
| double | floating-point numbers | real numbers 64 bit | + - * / |
| boolean | boolean values | true or false | && || ! |
| char | characters | 16 bit | |
| String | sequences of characters | it’s not a primitive data type |
DECLARATION AND ASSIGNMENT STATEMENTS
1 | //Declaration statement |
COMPARISON OPERATORS
| Operation | Meaning |
|---|---|
| == | equal |
| != | not equal |
| < | less than |
| > | greater than |
| <= | less than or equal |
| >= | greater than or equal |
PRINTING
1 | String s = "Happy Coding Folks!!" |
PARSING COMMAND-LINE ARGUMENTS
1 | String s = "Java is the best!!" |
EXAMPLES OF TYPE CONVERSION
| Expression | Expression type | Expression value |
|---|---|---|
| (1 + 2 + 3 + 4) / 4.0 | double | 2.5 |
| Math.sqrt(4) | double | 2.0 |
| “123343” + 99 | String | “12334399” |
| 11 * 0.25 | double | 2.75 |
| (int) 11 * 0.25 | double | 2.75 |
| 11 * (int) 0.25 | int | 0 |
| (int) (11 * 0.25) | int | 2 |
CONDITIONAL & LOOP STATEMENT
ANATOMY OF CONDITIONAL STATEMENT
IF Statement
1 | if (x>y) { // x > y is the boolean expression |
IF-ELSE STATEMENT
1 | if (BOOLEAN EXPRESSION) { |
NESTED IF STATEMENT
1 | if (BOOLEAN EXPRESSION) { |
SWITCH STATEMENT
1 | switch (VARIABLE TO EVALUATE ITS VALUE) { |
Example:
1 | int month = 8; |
ANATOMY OF A LOOP STATEMENT
FOR LOOP STATEMENT
1 | for (declare and initialize a loop control variable; loop-continuation condition/s; increment or decrement of the variable of control) |
Example:
1 | for (int i = 0; i <= n; i++) { |
Enhanced for loop/for-each
1 | for(dataType item : array) { |
Example:
1 | // array of numbers |
WHILE LOOP STATEMENT
1 | while(condition){ //till condition will be true. |
Example:
1 | //Initialization is a separate statement |
DO-WHILE LOOP STATEMENT
1 | do{ //always run one time even if condition would be false |
Example:
1 | int i=1; |
ARRAY
ARRAY DECLARATION
1 | int[] ai; // array of int |
DECLARATION OF ARRAY VARIABLE
1 | Exception ae[] = new Exception[3]; |
ACCESS MODIFIERS
- defualt(No keyword required)
- private
- public
- protected
NON ACCESS MODIFIERS
- static
- final
- transient
- abstract
- synchronized
- volatile
Object Oriented Programming (OOPs) Concept :clipboard:
OBJECT
1 | //Declare a variable, object name |
INSTANCE VARIABLES
1 | public class Charge { |
METHODS
1 | 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; |
CLASS DECLARATION
1 | class MyClass { |
Example:
1 | public class Bicycle { |
DECLARING CLASSESS IMPLEMENTATING AN INTERFACE AND EXTENDING PARENT CLASS
1 | class MyClass extends MySuperClass implements YourInterface { |
- MyClass is a subclass of MySuperClass and that it implements the YourInterface interface.
CONSTRUCTORS
- A class contains constructors that are invoked to create objects from the class blueprint.
- Constructor declarations look like method declarations—except that they use the name of the class and have no return type
- Each and every class has defualt No-args constructor.
1 | public class Bicycle{ |
POLYMORPHISM
- Polymorphism is the concept where an object behaves differently in different situations.
- There are two types of polymorphism
- compile time polymorphism
- 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14public class Circle {
public void draw(){
System.out.println("Drwaing circle with default color Black and diameter 1 cm.");
}
public void draw(int diameter){ //method draw() overloaded.
System.out.println("Drwaing circle with default color Black and diameter"+diameter+" cm.");
}
public void draw(int diameter, String color){ //method draw() overloaded.
System.out.println("Drwaing circle with color"+color+" and diameter"+diameter+" cm.");
}
}
2. Run Time Polymorphism
- Run-time polymorphism is achieved by method overriding.
- Runtime polymorphism is implemented when we have an “IS-A” relationship between objects.
- method overriding is the subclass has to override the superclass method.
1
2
3
4public interface Shape {
public void draw();
}1
2
3
4
5
6
7
8public class Circle implements Shape{
public void draw(){
System.out.println("Drwaing circle");
}
}1
2
3
4
5
6
7
8public class Square implements Shape {
public void draw() {
System.out.println("Drawing Square");
}
} Shapeis the superclass and there are two subclassesCircleandSquare- Below is an example of runtime polymorphism.
1
2
3
4
5Shape sh = new Circle();
sh.draw();
Shape sh1 = getShape(); //some third party logic to determine shape
sh1.draw();
INHERITANCE
- 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
extendskeyword in java to implement inheritance from class. - We use
implementskeyword in java to implement inheritance from interface.
1 | public class Superclass{ |
1 | public interface Superinterface{ |
1 | public class Subclass extends Superclass implements Superinterface{ |
Abstraction
- Abstraction is the concept of hiding the internal details and describing things in simple terms.
- Abstraction can be achieved by two ways.
- Abstract Class
- Interface
1. Abstract Class
- An abstract class must be declared with an
abstractkeyword. - It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
1 | abstract class Flower{ |
2. Interface
- Interface is a blueprint of a class.
- It can have only abstract methods. [Except Java 8 and next versions.]
- Since Java 8, we can have default and static methods in an interface.
1 | interface print{ |
Encapsulation
- Encapsulation is used for access restriction to class members and methods.
- Encapsulation is the technique used to implement abstraction in OOP.
- 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).
1 | public class User { |
ADVANCE DATA TYPE
- STACK DATA TYPE
1 | public class Stack<Item> implements Iterable <Item> |
- QUEUE DATA TYPE
1 | public class Queue<Item> implements Iterable<Item> |
- ITERABLE
1 | //import Iterator |
- SYMBOL TABLE DATA TYPE
1 | public class ST<Key extends Comparable<Key>, Value> |
- SET DATA TYPE
1 | public class Set<Key extends Comparable<Key>> implements Iterable<Key> |