Definition of: “String, Array, Constructor, Access Specifiers, This & Super Keyword” in Java.

Introduction: java

In this article I will provide definition and some more important rule related to the term In Java, This one article will help you to crack any interview related to java for fresher as well as experience candidates can also go through this article.

String:

String is non-primitive data type as well as String is a Predefined class present in ‘java.lang’ Package.

  • Memory size is not fixed.
  • String is use to store collection of characters.
  • String class is final class can’t be inherited to other classes.
  • At the time of String declaration, initialization, object creation takes place.
  • String objects are immutable in nature/ can’t be change.
  • object creation of String can be done in 2 ways.
    1. Using new keyword
    2. without using new Keyword.

String object is stored inside of String pool area which present inside heap area. Objects creation of String can be done in two ways.
1. Using new keyword
example: String str = “XYZ”; Stored in constant pool area
2. without using new Keyword.
example: String str = new String(“XYZ”);

String pool are classified into two types:
a) Constant pool area: during object creation if we do not use new keyword then object creation takes place in constant pool area.
Duplicate data is not allowed.
b) Non-constant pool area: if we use new keyword then object creation takes place in non constant pool area.
Duplicate data is allowed.

Why Strings are immutable?

for String object, String constant pool area concept is available, multiple reference pointing same object. by using one reference if we allowed to change content then remaining references affected to prevent this immutability required.

String class Method:

  • length()
  • toUpperCase
  • toLowerCase
  • equals()
  • equalsIgnoreCase
  • contains()
  • isEmpty()
  • charAt()
  • indexOf()
  • lastIndexOf()
  • startsWith()
  • endsWith()
  • subString(1)
  • subString(1,2)
  • concat()
  • replace()
  • split()
  • trim()

Array:

It is a collection of similar type of data stored at contiguous memory location.

  • it is homogeneous in nature.
  • Array declaration needs to be done with size.
  • Array cannot be expanded.
  • Array follows indexing that is memory cell starts from zero.
  • information stored is called as element.

Example:
String arr[] = {“Harry”, “Jon”, “Ron”, “Anna”};
int arr[] = {1,2,3,4,5,6,7,8,9,10};

Constructor:

Constructor is a special member of class, According to java language each and every class should have constructor.

  • it is used to initialize data member of a class and to load non static members of a class into object.
  • the constructor is called when an object of a class is created.
  • constructor name should be same as class name.
  • it does not have any return type.
  • constructor overloading is possible.

Types:
1. Default Constructor:
if constructor is not declared in java class by programmer then at the time of compilation compiler will provide constructor for class called as default constructor.
The constructor provided by compiler at the time of compilation is known as default constructor.
2. User defined Constructor: if programmer declaring constructor in java class then it is considered to be user defined constructor.

Access Specifier/Modifier:

it is used to represent scope of member of a class like variable, methods, constructors.

Private: if we define any member of a class as Private, then we can access that member only within class.
Public: if we define any member of a class as Public then we can access that member throughout the project.
Default: Scope will be only within the package.
Protected: scope will be within package also when there is inheritance performed with class from different package then also we can access members.

This Keyword:

Every class have unique reference number, this keyword as well as object of a class represent unique reference number of a class.

  • whenever name of instance variable and local variable are same then JVM confused which one is local and which one is instance variable. so to avoid this we use this keyword.
  • it is also used to call parameterized as well as non parameterized Constructor of its own class.

Super Keyword:

Super keyword refers to object of super class, whenever super class and sub class variable and method name are same then JVM confused which one to call so to avoid this we should use super keyword. used to access global variable from different class.(definition)

Automation Testing Selenium Interview Questions Top 5 IT Skills