We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept
why is string is immutable in java?
  • In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified.
  • Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object.
  • Java String objects are immutable as String objects are generally cached in the String pool.
  • Since String literals are usually shared between multiple clients, action from one client might affect the rest.
  • It enhances security, caching, synchronization, and performance of the application.

Java Concepts •  Added 2 months ago

What is the difference between this() and super() in Java?

In Java, super() and this(), both are special keywords that are used to call the constructor.

this:

  1. this() represents the current instance of a class
  2. Used to call the default constructor of the same class
  3. Used to access methods of the current class
  4. Used for pointing the current class instance
  5. Must be the first line of a block

super:

  1. super() represents the current instance of a parent/base class
  2. Used to call the default constructor of the parent/base class
  3. Used to access methods of the base class
  4. Used for pointing the superclass instance
  5. Must be the first line of a block

Java Concepts •  Added 2 months ago

What is the difference between equals() and == in Java?
  • Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
  • ''=='' or double equal operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects.
  • public boolean equals(Object o) is the method provided by the Object class.
  • The default implementation uses == operator to compare two objects.
  • For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Java Concepts •  Added 2 months ago

What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as final variable,final method, final class

Java Concepts •  Added 2 months ago

What is the difference between a local variable and an instance variable?

Local Variable: a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class wont be even aware of that variable.

Instance Variable: a variable which is bounded to its object itself. These variables are declared within a class, but outside a method. Every object of that class will create its own copy of the variable while using it. Thus, any changes made to the variable wont reflect in any other instances of that class and will be bound to that particular instance only.

Java Concepts •  Added 2 months ago

Can you explain the signature of main method in Java

Signature of main() method:

public static void main(String[] args)
{
   // code comes here
}

Public: It is an Access modifier, which specifies from where and who can access the method

Static: by making class as static,JVM initialize main method with out creating any object for that

Main: The point from where the program starts its execution

Void: It is a keyword and used to specify that a method doesn’t return anything

String[] args: It stores Java command line arguments and is an array of type java.lang.String class

Java Concepts •  Added 2 months ago

What are the differences between the constructors and methods?

Constructor:

  • A constructor is used to initialize the state of an object.
  • A constructor is invoked implicitly.
  • A constructor must not have a return type.
  • The Java compiler provides a default constructor if you don't have any constructor in a class.
  • The constructor name must be same as the class name.

Method:

  • A method is used to expose the behavior of an object.
  • A method must have a return type.
  • The method is invoked explicitly.
  • The method is not provided by the compiler in any case.
  • The method name may or may not be same as class name.

Java Concepts OOP •  Added 2 months ago

What is a constructor?

A constructor is a "kind of" method in java which is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. A constructor definition is similar to a method definition, except that the constructor should have same name as the class and doesn't contain a return type.

public class MyClass {
    public MyClass() {
      // class level definitions
    }
}

Java Concepts •  Added 2 months ago

What is the difference between an object-oriented programming language and object-based programming language?

Object-Oriented Languages (OOP) follow all the concepts of OOPs whereas, Object-based languages don't follow all the concepts of OOPs like inheritance and polymorphism.

Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the inbuilt objects, for example, JavaScript has window object.

Examples for Object Oriented Languages include Java, C# whereas Object-based languages include VB etc.

Java Concepts OOP •  Added 2 months ago

What is the difference between JDK, JRE, and JVM?

Java Virtual Machine (JVM) is an abstraction layer between a Java application and the underlying platform. it is acts as a “virtual” machine, to the bytecodes comprising the program.

JRE stands for Java Runtime Environment,which provides run time enviorment to run java applications. It is the combination of JVM and set of libraries to which will be used to run application.

JDK stands for Java Development Kit. It is a software development environment, used to develop Java applications and applets. It contains JRE(Java Runtime Environment) and development tools to develop applications.

Java Concepts •  Added 2 months ago

What are the types of access specifiers in Java?

In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable.

There are four access specifiers in java.

Public - The classes, methods, or variables which are defined as public, can be accessed by any class or method.

Protected - Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.

Default - Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.

Private - The private class, methods, or variables defined as private can be accessed within the class only.

Java Concepts •  Added 2 months ago