Important Core Java Topics:
Variable is a piece of memory that can contain a data value. It is the name of memory storage. In other words, a variable is the name of the reserved area that is allocated in the memory. Variable is made up of two words which are varied and able. This means that the value of a variable can be modified.
Types of variable
The following are the three types of variables in Java:
- Local variable:
A local variable is the type of variable that is declared with the body of the method. This type of variable can only be used inside that method and the other methods in the class won’t recognize the existence of the variable. The static keyword cannot be used to define a local variable.
- Instance variable:
An instance variable is such type of variable that is declared within a class but outside the body of the method. An instance variable is shared among instances, that’s why they’re called instance variables.
- Static variable:
When a variable gets declared as a static, then it is called a static variable. It cannot be local and the users can share a single copy of static variable after creating it among all the instances of the class. A static variable only allocates memory a single time when the class loading takes place. A static variable makes the program memory efficient cause it helps to save up memory.
2. Data types
The data types are used in Java o specify the various sizes and values that can be stored in a variable. The data types can be further divided into two types:
- Primitive data types:
Primitive data types are the most basic data types that are the building blocks of data modification. The types of primitive data types are boolean, byte, char, short, int, long, float, and double.
- Non-primitive data types:
The non-primitive data types are the classes, arrays, and interfaces.
The symbols like +, -, and so on that are used in Java to carry out operations are known as operators. There are several operators that are used in Java to perform various operations such as unary operator, arithmetic operator, logical operator, shift operator, assignment operator, and so on.
4. Control Statements
- Java If-else statement
In Java, if-else statements are used to verify whether a boolean statement is true or false. There are several types of Java if statements which are:
- If statement
- If-else statement
- If-else-if ladder
- Nested if statement
- Java Switch Statement
Java Switch Statement is used in Java to verify the conditions on the basis of a single integer and enumerated value. It functions as the if-else-if ladder statement and is used to run one statement from multiple expressions.
5. Loops in Java
Loops are used to run a set of functions more than a single time when some expressions become true. The following are the type of loops used in Java:
- For Loop
A for loop provides a short and easy way to debug the stricter of the loop. It verifies the condition in one line. For loop is further divided into three types that are:
- While Loop
This loop carries out a part of a program more than once and is used in the case in which the number of iteration is not fixed.
- Do While Loop
This loop also performs the same function as the while loop but it is used in the case in which the loop must be executed at least once and the number of iteration is not fixed.
- Break Statement
The break statement is used to break the loop immediately when it is encountered. Once the loop ends, the program will move to the next portion.
- Continue Statement
The continue statement is used to make the flow of the program go on and also to move to the next iteration of the loop immediately.
- Comments Statement
The comment statements give the details about a method, class, or a variable. It also hides the code. There are three types of comments statements which are the single line, multi-line, and documentation comment.
String in Java is an object that represents a sequence of char value. It works the same way as an array of chars does. A string literal or a new keyword is used in Java in order to create a string. Double quotation marks are used to create a sequence of characters in the string literal and the JVM in heap memory creates a new string object in the new keyword method.
The string objects cannot be changed, that is, the data and the state of the string object cannot be modified after it is constructed. There are several string methods present in Java and various interfaces that get implemented by the string class.
Check out: String in Java – Everything You Need to Know
An array can be simply defined as a collection of elements that have similar features with contiguous memory. However, an array is defined in Java as an object that includes the elements with a similar data type. An array can be both used as a static field and as a local variable and are dynamically allocated.
An array is referred by a common name and is a data structure in which a fixed set of elements of the same types are kept. The base of an array is an index and its index starts from 0. The member length can be used to find out the length of an array as they are objects.
An array can include both the primitive data types and objects of a class and is used by declaring a variable to reference the array in the beginning and then by specifying the type of the array. The function of the array in Java differs from the function of the array in C/C++. An array can be passed to a method to reuse the same login on an array. One array can be copied to another by using the system class.
Arrays can be further divided into two types which are:
- Single dimensional array:
A single dimensional array is a collection of variables with the same type that is denoted by a common name.
- Multidimensional array:
A multidimensional array is an array of another array. In this type of array, each element of one array contains the reference of another array.
Check out: Array in Java with Real-Time Example
An array is used in Java to store a collection of data. It stores the variables of the same type which helps the users to easily retrieve and sort the data. This way an array optimizes the code. Any data stored in an array can be located at any index position.
However, there is a size limit to how much elements can be stored in an array. Since only a certain set of elements can be stored in an array, there is a restriction in size. But the users can use collection framework to overcome this disadvantage of the array.
Java OOPs Concepts
Object-Oriented Programming (OOP) can be defined as a programming paradigm. It depends on objects and deals with other concepts like inheritance, data binding, and polymorphism. In OOP, an object represents everything in the language and OOP implements real-world entities.
The following are the various concepts that OOP deals with:
A class is a logical entity which contains multiple objects with similar features. It does not take up memory and represent the properties of methods that are similar in a type of objects. Classes are used as a blueprint to construct a single object and it has default access.
The name of a class always starts with an initial letter. There are three types of variables that are contained in a class and they are local variables, class variables, and instance variables.
An object is simply defined as any kind of logical and physical entity that has behaviors and state. The objects in the real world such as desk, keyboard, and pen, and the objects in the software have similar characters like identity, behaviors, and state. The attributes of the object represent the state of the object and properties of an object are reflected by them.
Similarly, the methods represent the behavior of the object and the response of an object with other objects is reflected by them. The unique name of the object that sets them apart from other object is called the identity of the object and it facilitates the interaction between objects.
Objects are an instance of a class and it is located in memory and contains an address. An object communicates without the knowledge of data or code of each other.
Check out: Classes and Objects in Java With Examples
In Java, a constructor is a block of codes and is similar to a method. It is called in the case in which an instance of an object is constructed and the object allocates memory. It initializes an object and is invoked every time when a new keyword is used to construct an object.
It constructs the value when an object is constructed. The name of the constructor and the class name must always be the same and it should never have an explicit return type. There are two types of constructors in Java which are the default and the parameterized constructor.
Inheritance is the process by which an object gains properties of the parent object. It builds new classes by depending on the already creates classes. Through it, the methods and properties of a superclass can be used to construct a new class in Java. By doing so, the code can be used again and again and it also helps to achieve runtime polymorphism.
The fields and methods of the already present class can be used to construct a new class by using inheritance. It manages the information in a hierarchical order and represents a parent-child relationship which is called the IS-A relationship.
Abstraction is used in Java to hide the internal details and only show the functionality of the code to the users. It only deals with ideas and makes the users aware of the function of the object rather than the way that the object completes its functions.
- Abstract classes
An abstract class is a class that is declared with an abstract keyword. An abstract class may or may not contain all the abstract methods. Sometimes, the abstract methods are replaced with concrete methods. An abstract class lacks an object which means that it cannot be instantiated directly.
The class that contains at least a single abstract method must always be declared as an abstract class. It always includes a default constructor and may also include a parameterized constructor in some cases. It implements the interface and to use the abstract class, it must be inherited from another class.
An interface is an abstract type that is used to specify a behavior that classes must implement. In another word, it is a collection of abstract methods. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations.
It is a blueprint of a class and includes. It contains both methods and variables; however, the methods declared in an interface only include the signature but not the body.
It cannot be instantiated like an abstract class and an IS-A relationship is represented by it. When the class is not an abstract class then every method of the interface must be defined in the class. It is written in the same way as a class and it includes behavior that is implemented by a class. It deals with capabilities and specifies the function of a class but not the way that the class achieves its functions.
Polymorphism is the process through which a single task is carried out in different methods. It can be defined as the property of the object that helps them take different forms.
It is mainly used in the case in which a superclass object is used to reference a subclass. An object is called polymorphic if it can pass more than one IS-A test. Since every object of Java passes this test, all objects are polymorphic.
An object can be accessed only through a reference variable and there is only one type of reference variable. Once the declaration has been done, the type of reference variable is modifiable. Method overloading and overriding are the two processes that are used to achieve polymorphism in Java.
Overloading is the case in which one class has different methods with a similar name but different parameters. It figures out the program rapidly as it facilitates various methods to have a similar name, but different signatures. The number of input parameters or the nature of the input parameters can be used to differentiate between the signatures.
It increases the readability of the program as only one operation that has the same name as the methods have to be executed. Overloading can be carried out in Java either by changing the number of arguments or by changing the data type.
Overriding is the situation in which a subclass has a similar method as the superclass. In other words, overriding refers to the case in which a subclass provides a similar method as the superclass. It facilitates the subclass to run a particular method that has already been performed by its superclass.
Overriding helps to achieve polymorphism in Java. It specifies the methods that will be similar to all of its derivatives and that has been already provided by the superclass. It calls on the methods of any child class even without the knowledge of the type of the subclass object.
A collection in Java is a framework that provides the architecture for storage and to control the group of objects. It achieves all the operations that are performed on data such as data including searching, insertion, sorting, deletion, and manipulation.
Check out: Collections in Java with Examples
17. Garbage Collection
garbage collection is the process of destroying these unused objects.
Multithreading saves memory as they don’t take up separate memory area and the processing time is also less as they take less time while context-switching among the threads. It is mainly used in animations and games.
19. Exception handling
Exception handling is one of the most important mechanisms that are used in Java to manage the runtime errors.
20. 1. Serialization
Serialization is the process that is used to write the state of an object into a byte stream. EJB, RMI, JMS, and RMI are some of the technologies that use serialization.
20. 2. Deserialization
deserialization is the mechanism that reconstructs the object from a serialized state. It does exactly the opposite of what serialization does.
To be continued…