Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

JAVA BASICS

CONTENTS
1. INTRODUCTION
2. BASIC JAVA CODE
3. JVM, JRE, JDK?
4. FEATURES
5. JAVA vs C/C++
6. GARBAGE COLLECTION
1. INTRODUCTION
Java is a popular high-level, class-based object oriented programming language originally
developed by Sun Microsystems and released in 1995. Currently Java is owned by Oracle.
● Object-Oriented Programming Language
Object:- An Object can be defined as an instance of a class. An object contains an address and
takes up some space in memory.

Class:- Collection of objects is called class. A class can also be defined as a blueprint from which
we can create an individual object. Class doesn't consume any space.

Inheritance:- When one object acquires all the properties and behaviors of a parent object, it is
known as inheritance. It provides code reusability.

Polymorphism:- If one task is performed in different ways, it is known as polymorphism. In Java,


we use method overloading and method overriding to achieve polymorphism.

Abstraction:- Hiding internal details and showing functionality is known as abstraction.

Encapsulation:- Binding (or wrapping) code and data together into a single unit are known as
encapsulation. A java class is the example of encapsulation.
2. BASIC JAVA CODE
Parameters
1. In Java, any line starting with // is a comment.
2. In Java, every program begins with a class definition. The class keyword is used to declare
classes in Java. In the program, HelloWorld is the name of the class and the name of the
class should match the filename in Java.
3. public static void main(String[] args) { ... } :- This is the main method. The Java
compiler starts executing the code from the main method. public: It is an access specifier and
means this function is visible to all. static: It is a keyword used to make a function static. To
execute a static function we do not have to create an Object of the class. The main() method
here is called without creating any object for class. void: It is the return type, meaning this
function will not return anything. String[] args: This is used to signify that the user may opt to
enter parameters to the Java Program at command line.
4. System.out.println("Hello, World!"); :- It is a print statement. It prints the text Hello,
World! to standard output (our screen). The text inside the quotation marks is called String
in Java. The print statement is inside the main function, which is inside the class definition.
Here, System is a class, out is an object of the PrintStream class, println() is a method of the
PrintStream class.
3.1 What is JVM?
JVM (Java Virtual Machine) is an abstract machine that enables our computer to run a Java
program.

When we run the Java program, Java compiler first compiles our Java code to bytecode. Then, the
JVM translates bytecode into native machine code (set of instructions that a computer's CPU
executes directly).
3.2 What is JRE?
JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java
Virtual Machine (JVM), and other components that are required to run Java applications. JRE is the
superset of JVM.
3.3 What is JDK?
JDK (Java Development Kit) is a software development kit required to develop applications in Java.
When we download JDK, JRE is also downloaded with it.

In addition to JRE, JDK also contains a number of development tools (compilers, JavaDoc, Java
Debugger, etc).
4. JAVA FEATURES
● SALIENT FEATURES
Platform Independent

Compiler converts source code to bytecode and then the JVM executes the bytecode
generated by the compiler. This bytecode can run on any platform be it Windows, Linux, or
macOS which means if we compile a program on Windows, then we can run it on Linux and
vice versa. Each operating system has a different JVM, but the output produced by all the OS
is the same after the execution of the bytecode. That is why we call Java a
platform-independent language.

Portable

As we know, Java code written on one machine can be run on another machine. The
platform-independent feature of Java in which its platform-independent bytecode can be taken
to any platform for execution makes Java portable.
5. Java Garbage Collection
In java, garbage means unreferenced objects.

Garbage collection is the process of reclaiming the runtime unused memory automatically. In other
words, it is a way to destroy the unused objects.

To do so, free() function in C and delete() in C++ is used. But, in Java it is performed
automatically.

Advantages of Garbage Collection in Java:-

● It makes Java memory efficient because garbage collector removes the unreferenced objects
from heap memory.
● It is automatically done by the garbage collector (a part of JVM) so we don't need to make
extra efforts.
6. Java vs C/C++
JAVA C/C++

1. Platform-independent; Java bytecode 1. Platform dependent; should be compiled


works on any operating system. for different platforms.

2. It can run on any OS hence it is portable. 2. It is platform-dependent. Hence, it is not


portable.

3. Java is both Compiled and Interpreted 3. It is a Compiled Language.


Language.
THANK YOU!

You might also like