Posts

Showing posts from September, 2020

Constructors in Java

  Constructors in Java In Java , a constructor is a block of codes similar to the method.  It is called when an instance of the class is created.  At the time of calling constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default. There are two types of constructors in Java: no-arg constructor, and parameterized constructor. Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any. Rules for creating Java constructor:- There are two rules defined for the constructor. Constructor name mu

Java static keyword

  Java static keyword The static keyword in Java is used for memory management mainly. We can apply static keyword with variables , methods, blocks and nested class .  The static keyword belongs to the class than an instance of the class The static can be: Variable (also known as a class variable) Method (also known as a class method) Block Nested class 1) Java static variable:- If you declare any variable as static, it is known as a static variable. The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading. Advantages of static variable It makes your program memory efficient (i.e., it saves memory). Example:- class Student {      int rollno;    String name;      static String college ="ITS";       Student(int r, String n)   {  rollno = r;      name = n;

Methods in Java

Image
  Methods in Java A method is a collection of statements that perform some specific task and return the result to the caller . A method can perform some specific task without returning anything.  Methods allow us to reuse the code without retyping the code.  In Java, every method must be part of some class which is different from languages like C, C++, and Python. Methods are time savers and help us to reuse the code without retyping the code. Types of Method:-      There are two types of methods in Java: Predefined Method User-defined Method Predefined Method:- In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods.  It is also known as the standard library method or built-in method.  We can directly use these methods just by calling them in the program at any point. Some predefined methods are length(), equals(), compareTo(), sqrt(), etc.  When we call any of the predefined methods in our program, a series of code

OOPs(Object-Oriented Programming )

  OOPs(Object-Oriented Programming ):- Object-Oriented Programming is a paradigm that provides many concepts, such as inheritance , data binding , polymorphism , etc. Simula is considered the first object-oriented programming language. Smalltalk is considered the first truly object-oriented programming language. The popular object-oriented languages are Java , C# , PHP , Python , C++ , etc. Object Oriented Programming Language: A language that supports all principles of an object oriented programming is known as an object oriented programming language. Object Oriented Principles: 1) Encapsulation 2) Abstraction 3) Inheritance 4) Polymorphism To use the above principles in a Java programming, we need the following language constructs: 1) Class 2) Object Variables: A variable is a container that contains data. There are three types of variables in Java: 1) Instance Variables 2) Class Variables 3) Local Variables Object:- Really world entity  Eg:- chair, bike, marker, pen, table.