Tip:
Use https://www.jdoodle.com/online-java-compiler/ online compiler for practising
Java Basics:
Advantages :
- Object Oriented − In Java, everything is an Object (Except primitive data types). Java can be easily extended since it is based on the Object model.
- Platform Independent − Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
- Architecture-neutral − Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.
- Portable − Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
- Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
- Secure − With Java’s secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
- Robust − Java makes an effort to eliminate error prone situations by emphasising mainly on compile time error checking and runtime checking.
- Multi-threaded − With Java’s multi threaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
- Interpreted
- High Performance − With the use of Just-In-Time compilers, Java enables high performance.
- Distributed − Java is designed for the distributed environment of the internet.
- Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.
Compilation:
Java implementations typically use a two-step compilation process.
- Java source code is compiled down to bytecode by the Java compiler.
- The bytecode is executed by a Java Virtual Machine (JVM) by converting it to machine code.
Note:
Modern JVMs use a technique called Just-in-Time (JIT) compilation to compile the bytecode to native instructions understood by hardware CPU on the fly at runtime.
Some implementations of JVM may choose to interpret the bytecode, instead of JIT compiling it to machine code, and running it directly.
It is technically possible to compile Java down to native code ahead-of-time and run the resulting binary. It is also possible to interpret the Java code directly.
To summarise, depending on the execution environment, bytecode can be:
- compiled ahead of time and executed as native code (similar to most C++ compilers)
- compiled just-in-time and executed
- interpreted
- directly executed by a supported processor (bytecode is the native instruction set of some CPUs)
Classes and objects:
Class :
A class can be defined as a template/blueprint. Just visualise it as a vehicle chassis:

So we can create different types of cars with the base model.
we are defining basic properties of a car in our class.
let our car class be:
public class Car{
// Here we define basic properties for our car like
// Tyres, components,colour and accessory
// All our cars will have these properties, but value will be different
int tyres;
String component;
String colour;
String accessory;
// This is our constructor, a constructor is a base method that get executed while we create a object
// so when we create an object for over car class, we will update the values of tyre,component ,accessory and colour
// This property values will be different for different cars we create.
public Car(String colours,String components, String accessories){
colour=colours;
component="steering,"+components;
accessory=accessories;
tyres=4;
}
//This is a method , that allows to see the new properties of the object we created.
void printCar(){
System.out.println(colour+","+component+","+accessory+","+tyres);
}
}
Note: Class doesn’t have any memory allocated. Memory is allocated on object creation
Object :
Object is the actual implementation of a class. We have seen the class car, now will create different types of car
// We are creating an object with following values
// Colour = red
// Components = Airbags,Seat Belt,Self drive
// Accessories = Leather Seat,Stereo, AC
// tyres = 4 ( we are nto passing it as its default 4 , for all )
car ferrari = new car ("Red","Airbags,Seat Belt,Self drive", "Leather Seat,Stereo, AC");



So, an object is an instance of a class.
Methods:
A method is basically a behaviour.
A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
For instance , in my class the printcar() is the method. Its like a function but can be accessed through a object only ( ferrari.printcar() ).
Difference between instance and class variables:
| Instance Variable | Class Variable |
| Instance variables belongs to the object | Class variable belongs to class |
| Are non-static | Are static |
| Declaration does not require any keyword eg: int a; | Use static keyword: eg: static int a; |
| Can be accessed only through and object and changes will reflect only in that specific object ( eg ferrai.colour=”blue” will make colour of only ferrai blue, benz.colour remains as “white”, if colour is non- static variable) | Changes will reflect in all object even if used as part of an object ( eg ferrai.colour=”blue” will make colour of all cars as blue if colour is static variable) |
| Static methods are stored in Metaspace (variable length space ) space of heap | Non-Static methods are stored in PermGen (Fixed length space) space of heap |
Memory Management:
Locally (part of a method) declared primitive types like init, bytes, char etc are stored on the stack while static and instance (part of an object instance or class) primitive types are stored on the heap. in short Local variables are stored on stack , and instance and static variables are on the heap.
Referenced object will be created in heap but the variable will be created in stack ( For instance, car ferrari = new car() , will create memory for ferrari in stack and the actual object will be in heap. The stack variable 'ferrari' will have the reference address to heap location of actual object)
Garbage collector will take care of Heap.
Scope of static variable is global and can be accessed directly without creating an instance.
While local variables are those variables defined inside a method, their scope is local and will be destroyed once method execution completes.
Local variables cannot be static and will be created in stack.
String pool is created in Heap : String a=”new” will search for new in the string pool
Please refer: https://www.baeldung.com/java-stack-heap
Try what we learned :
public class MyClass {
public static void main(String args[]) {
car redcar = new car("red","air bags,seat belts","AC");
car whitecar = new car("white","air bags,seat belts","AC");
System.out.println("################################################################## After creating red car ##################################################################n");
redcar.printcar();
System.out.println("\n\n");
System.out.println("################################################################## After creating white car ##################################################################\n");
whitecar.printcar();
System.out.println("\n\n");
System.out.println(" ################################################################## After changing static tyre value by calling class.variable: ############################## \n ");
car.tyres=5;
System.out.println("1) ############ Red car #################\n");
redcar.printcar();
System.out.println("\n\n");
System.out.println("2)############# White car ################\n");
whitecar.printcar();
System.out.println("\n\n");
System.out.println("################################################################## After changing the Static variable through object.variabler: ################################\n");
redcar.tyres=10;
redcar.colour="blue";
System.out.println("1)) ############ Red car ############ \n");
redcar.printcar();
System.out.println("\n\n");
System.out.println("2) ) ############ White car ############ \n");
whitecar.printcar();
System.out.println("\n\n");
System.out.println("################################################################## After changing the non- Static variable color to blue for redcar through object.variabler: ########### \n");
redcar.tyres=10;
redcar.colour="blue";
System.out.println("1) ) ############ Red car ############ \n ");
redcar.printcar();
System.out.println("\n\n");
System.out.println("2) ) ############ White car ############ \n ");
whitecar.printcar();
}
static class car{
static int tyres;
String component;
String colour;
String accessory;
public car(String colours,String components, String accessories){
tyres=4;
component="steering,"+components;
accessory=accessories;
colour=colours;
}
void printcar(){
System.out.println(tyres+","+component+","+accessory+","+colour);
}
}
}