1. Java Main Method:
The main
method is the entry point of any Java application. It tells the Java Virtual Machine (JVM) where to start program execution.
// 1.1. Basic Main Method
public class SampleProgram {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
// 1.2. Main Method with Different Argument Name (Not necessary to keep it args)
public class SampleProgram {
public static void main(String[] myArgs) {
System.out.println("Arguments length: " + myArgs.length);
}
}
Run this with:
java CommandLineDemo Hello Java
// 1.3. Main Method with Command Line Arguments
public class SampleProgram {
public static void main(String[] args) {
for (String arg : args) {
System.out.println("Argument: " + arg);
}
System.out.println("Argument: " + arg[0]);
}
}
// 1.Overloading main Method?
public class SampleProgram {
public static void main(String[] args) {
System.out.println("Main method called");
main(5);
}
public static void main(int number) {
System.out.println("Overloaded main: " + number);
}
}
2. Java Variables and Datatypes:
A variable is a name for a memory location that stores data. In Java, it must be declared with a data type that defines the kind of data it holds.
Java Data Types:
1. Primitive Data Types:
Data Type | Size | Default Value |
---|---|---|
byte | 1 byte (8-bit) | 0 |
short | 2 bytes (16-bit) | 0 |
int | 4 bytes (32-bit) | 0 |
long | 8 bytes (64-bit) | 0L |
float | 4 bytes (32-bit) | 0.0f |
double | 8 bytes (64-bit) | 0.0d |
char | 2 bytes (16-bit) | '\u0000' (null char) |
boolean | ~1 bit (JVM dependent) | false |
2. Non-Primitive Data Types:
– String, Array, Class, Interface, etc.
public class VariablesAndDatatypes {
static int a;
public static void main(String[] args) {
int a = 10;
int A = 10;
String employeeName = "Amit";
System.out.println("a: " + a);
int age = 25;
String name = "Rahul";
double price = 99.99;
boolean isActive = true;
System.out.println(age);
System.out.println(name);
System.out.println(price);
System.out.println(isActive);
String city = "Delhi";
int[] marks = {90, 85, 88};
}
}
3. Java Conditional and Control Statements:
Control statements are used to control the flow of execution in a program
. if, else, else if, switch
public class ControlAndConditionalStatements {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("Eligible to vote1");
} else if (age == 17){
System.out.println("Eligible to vote2");
}else {
System.out.println("Not Eligible");
}
int a = 2;
switch(a) {
case 1:
System.out.println("It is 1");
break;
case 2:
System.out.println("It is 2");
break;
case 3:
System.out.println("It is 3");
break;
default:
System.out.println("Invalid data");
}
}
}
4. Java Looping Statement:
Loops allow us to execute a block of code multiple times without having to write it manually each time.
public class ControlAndConditionalStatements {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Hello " + i);
}
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
int j = 1;
do {
System.out.println("Run at least once");
j++;
} while (j <= 1);
}
}
5. Java Loop Control Statement:
public class ControlAndConditionalStatements {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if(i == 4) {
continue;//break;
}
System.out.println("Hello " + i);
}
}
}
6. Class & Object (OOP):
Class in Java:
A class
in Java is a code structure that defines data members (variables) and methods (functions). It acts as a template to define how objects will behave and what data they will hold.
Object in Java:
An object
is a runtime instance of a class. It holds actual values in memory and can use the class’s methods. An object is used to access the members of a class.
public class SampleProgram {
public static void main(String[] args) {
ABC2 obj = new ABC2();
obj.display1();
ABC2.display2();
main();
}
public static void main() {
ABC2 obj = new ABC2();
obj.display2();
ABC2.display();
}
public void main2() {
ABC2 obj = new ABC2();
obj.display2();
ABC2.display1();
}
}
class ABC2 {
int a = 10;
static int b = 10;
static void display1() {
int c = 10;
b++;
//System.out.println(a);
System.out.println(b);
}
void display2() {
System.out.println(a);
//As a was a local variable so we can not access it outside the method
//System.out.println(b);
}
}
Name: Akash
Age: 22
7. Abstraction (OOP):
Abstraction is the OOP concept of hiding implementation details and showing only essential features of an object.
How to Achieve in Java?:
- Abstract classes
- Interfaces
abstract class C1{
abstract void display1();
void display2() {
System.out.println("Inside display2()");
}
}
interface I1{
void display3();//public & abstract
}
class C2 extends C1 implements I1{
void display1() {
System.out.println("Inside display1()");
}
public void display3() {
System.out.println("Inside display3()");
}
}
public class AbstractionInJava {
public static void main(String[] args) {
C1 obj = new C2();
obj.display1();
obj.display2();
System.out.println("\n");
I1 obj2 = new C2();
obj2.display3();
}
}
8. Encapsulation (OOP):
Encapsulation is the OOP principle of binding data and methods together and restricting direct access to some components of an object.
Key Features of Encapsulation:
- Private variables – restrict direct access.
- Public getters/setters – provide controlled access.
- Helps achieve data hiding.
class MyData{
private String name = "Amit";
public int salary = 12000;
public String getName() {
return "Mr. " + name;
}
public void setName(String s1) {
name = s1;
}
}
public class EncapsulationInJava {
public static void main(String[] args) {
MyData obj = new MyData();
System.out.println(obj.salary);
// System.out.println(obj.getName());
// System.out.println(obj.name);
// obj.name = "Sumit";
obj.setName("Sumit");
System.out.println(obj.getName());
}
}
9. Inheritance (OOP):
Inheritance is one of the main concepts of OOP. Inheritance provides code reusability; we do not need to create redundant code as such.
Types of Inheritance:
- Single inheritance: Yes
- Multilevel Inheritance: Yes
- Multiple Inheritance: No, Java does not support multiple inheritance with classes. It handles this feature by implementing more than one interface instead of extending more than one class
- Hierarchical Inheritance (Tree Based): Yes
public class InheritanceInJava {
public static void main(String[] args) {
Three g = new Three();
g.methodOne();
g.methodTwo();
g.methodThree();
Two two = new Two(1,2,2);
D obj = new D(2);
obj.methodFour();
}
}
class One {
int a;
One(){
System.out.println("One Constructor with 1P!");
}
One(int y, int z) {
System.out.println("One Constructor with 2P!");
}
public void methodOne() {
System.out.println("FirstMethod from class One!");
}
}
class Two extends One {
int b;
Two(int x, int y, int z) {
super();
//super(1,2);
b = x;
System.out.println("Two");
}
public void methodTwo() {
System.out.println("SecondMethod!");
}
}
class Three extends Two {
int c;
Three() {
super(1,2,3);
System.out.println("Three Constructor!");
}
Three(int x, int y, int z) {
//super();
super(x,y,z);
c = x;
System.out.println("Three");
}
public void methodThree() {
System.out.println("SecondMethod!");
}
}
10. Polymorphism (OOP):
In Object-Oriented Programming (OOP), polymorphism means “many forms”. It allows objects to behave differently based on their actual type or method input.
Types of Polymorphism:
1. Compile-Time Polymorphism (It can be achieved through Method Overloading)
2. Run-Time Polymorphism (It can be achieved through Method Overriding)
class Bird{
void speak() {
System.out.println("Tweet Tweet");
}
void speak(String hungry) {
System.out.println("Tweet Tweet Tweet Tweet");
}
}
class Koel extends Bird{
void speak() {
System.out.println("Koo-Kooo");
}
}
public class Polymorphism {
public static void main(String[] args) {
Bird obj = new Koel();
obj.speak();
}
}
11. Overloading And Overriding In Java (OOP):
public class OverloadingAndOverridingInJava {
public static void main(String args[]) {
OverloadingAndOverridingInJava obj = new OverloadingAndOverridingInJava();
System.out.println("add() with 2 parameters");
System.out.println(obj.add(4, 6));
System.out.println("add() with 3 parameters");
System.out.println(obj.add(4, 6, 7));
InheritFirstOne obj2 = new InheritFirstOne();
System.out.println(obj2.add(1, 2));
//System.out.println(obj2.add(1, 2, 3));
}
}
class OverloadingAndOverridingInJava {
// Within the same class: Method overloading
int add(int a, int b) {
System.out.print("Inside, OverloadingAndOverridingInJava!: ");
return a + b;
}
int add(int a, int b, int c) {
System.out.print("Inside, OverloadingAndOverridingInJava!: ");
return a + b + c;
}
}
//Overriding in Java: Requires Inheritance!
class InheritFirstOne extends OverloadingAndOverridingInJava {
int add(int a, int b) {
System.out.print("Inside, InheritFirstOne!: ");
return a + b + 1;
}
int add(int a, int b, int c) {
System.out.print("Inside, OverloadingAndOverridingInJava!: ");
return a + b + c;
}
}
12. Interface in Java:
- An interface is a completely(100%) “abstract class”, which is used to group related methods with empty bodies.
- Multiple interfaces can be implemented at the same time in one class.
- All methods in a Java Interface are Abstract!
- It is optional to add the abstract keyword in the method declaration.
- By default, all the methods of an interface are public and abstract, so it is optional to add the public and abstract keywords.
public InterfaceInJava{
public static void main(String[] args) {
C1 obj = new C1();
obj.funcA();
obj.funcB();
System.out.println(a);
}
}
interface A1 {
void funcA();
}
interface B1 extends A1 {
void funcB();
}
class C1 implements B1 {
public void funcA() {
System.out.println("This is funcA from A1");
}
public void funcB() {
System.out.println("This is funcB from A2");
}
}
13. Abstract and Final Class in Java:
Abstract Class:
- An abstract class must be declared with the abstract keyword.
- Abstraction is a process of hiding the implementation details and showing only functionality(function declarations) to the user.
- We can not make an object of an abstract class directly, but through a derived class, it is possible. [Abstract class can not be instantiated]
- Example of an abstract method?
- Abstract Class may contain abstract and non-abstract methods
- (0-100%) Abstract methods can be there!
- In case of Interface: 100% abstract methods should be there!
Final Class:
- The main purpose of using a class declared as final is to prevent the class from being subclassed.
- If a class is marked as final, then no class can inherit any feature from the final class.
- You cannot extend a final class. If you try it gives you a compile-time error.
abstract class Bike {
int a;
String abc;
Bike() {
System.out.println("Inside Bike Constructor: " + a + "\n");
}
abstract void run(); // abstract method! (0-100%)
void display() {
System.out.println("This is display method!");
}
}
//The type Honda must implement the inherited abstract method Bike.run()
class Honda extends Bike {
void run() {
System.out.println("run method from Bike class!");
}
}
public class AbstractAndFinalClassInJava {
public static void main(String[] args) {
Honda obj = new Honda();
obj.display();
obj.run();
// can not be instantiated
Bike obj2 = new Bike();//Error
}
}
final class ABC {
public int data = 30;
String a = "Hello!";
void display() {
System.out.println(data);
}
}
class CBA extends ABC{//Error
public int data = 30;
void display() {
System.out.println(data);
}
}
14. Constructors in Java:
- It is called when an instance/object of the class is created
- At the time of calling the constructor, memory for the object is allocated in the memory
- It is a special type of method that is used to initialize the object
- Every time an object is created using the new() keyword, at least one constructor is called
- Two types of Constructors in Java:
- Parameterized Constructor
- Default Constructor
public ConstructorsInJava {
public static void main(String[] args) {
// ABC4 obj = new ABC4();
ABC4 obj1 = new ABC4(10000);
ABC4 obj2 = new ABC4(1,2,3);
// System.out.println(obj.a);
System.out.println(obj1.a);
System.out.println(obj2.a);
}
}
class ABC4 {
int a;
int b;
int c;
/*
ClassName(Constructor Parameters){
All the class and Instance variables can be initialized here!
}
*/
ABC4(int a){
this.a = a;
}
ABC4(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
void display() {
int b = 10;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
int display2() {
System.out.println(a);
//As a was a local variable so we can not access it outside the method
//System.out.println(b);
return a;
}
}
Thanks and Regards,
Sir….in the main method with command line argument snippet code….in line number 8, it should be args[0] instead of arg[0]…just a little correction but important
Thanks for the suggestion, but the given code is correct. I request you to please check the loop code again.