Posts

Showing posts from January, 2023

Java Features

The important features of Java language are Simple : As the word says - "Java is simple".That means it easy to learn java.You don't need any prerequisites like knowledge in any other language.It is simply like a nursery level programming language.Most of the complex and confusing features of other languages (C,C++) like pointers,multiple inheritance are removed from java Platform Independent : It means that if we write java program once, we can run on any platform (Windows,Linux,Mac) without performing any change. ie, Java follows Write Once Run Anywhere principle(WORA). Architecture Neutral : Any changes in your system like upgrading operating systems,processor and system resources will not force any changes in java programs.This is because java programs never communicate with the platform directly. Portable : Just like your mobile number is portable from one service provider to another without changing your mobile number,we can carry java byte code from any platform t...

Java is purely object oriented or not

             When compared to other object oriented programming languages like C++, java is more object oriented.But there are some object oriented features like operator overloading,multiple inheritance (indirectly implemented) etc  which are not supported by java.So we cannot say that java is purely object oriented.The recommended answer is NO.

Java is Strongly typed

 In java every variable and every expression has some type.Each and every datatype is clearly defined.Every assignment will be checked by the compiler for type compatibility.Because of the above reasons we can conclude that java is strongly typed language. Example:  byte b = false; // compile time error: incompatible types                                                                                                    found: boolean                                                                ...

How to define a class in java

Image
  Class A class is the blueprint of the objects that are derived from it. A class consists of data members and methods.For example: Data members: Company Name Location Package in Lacs Method Work() Writing a class      A class can be written as follows public class DreamCompany { public String companyName; public String location; public String packageInLacs; public void work() { // work in the company } } Function definition is also given as and when function is declared. Class declaration only specifies the template/blueprint and does not allocate any memory to the data members. Memory is allocated only when objects are created from this class. Class declaration will be done only once. We can create multiple objects from the same class in a java program Main method in java Every java application must contain a main method whose signature looks like this: class HelloWorld { public static void main(String[] args) { System.out.println("Hello...

Creating objects

Declaration : It is similar to a variable declaration.A variable declaration with a variable name and object type eg: DreamCompany microsoft; It does not allocate any memory unlike primitive data types It just creates reference of type ‘DreamCompany’ and name ‘microsoft’ pointing to nothing Instantiation : Allocating memory to the variable(using the ‘new’ keyword) eg: microsoft = new DreamCompany(); ‘new’ operator allocates memory for the object and returns a reference to that memory Initialization: initializing the object with some values. microsoft.companyName =“Microsoft”; microsoft.location =“Hyderabad”; microsoft.packageInLacs =“25”; We can also combine the two statements into one as follows: DreamCompany microsoft = new DreamCompany();

Access modifiers in Java

  As the name suggests,access modifiers help to restrict the scope of a class,variable,method or data member.There are four types of access modifiers available in java: Public Protected Private Default: no keyword required Public access modifier ‘ public’ keyword implies making the data member,functions available to everyone All classes in all packages will be able to access the public data members and invoke the public function  It is the least restrictive access modifier Consider the following program package OopsusingJava; public class JavaCourse { public int lecturesCompleted; public void learnJava() { System.out.println(“Started Java Programming”); } } And we have another class ‘JavaStudent’ which is inside another package SemesterTwo: Package SemesterTwo; Import OopsusingJava.JavaCourse; public class JavaStudent { public void wantToLearnJava() { JavaCourse bestJavaCourse = new JavaCourse(); bestJavaCourse.learnJava(); } } What will be the output ...

Constructors

 Once we create objects, compulsorily we have to initialize it. Otherwise the objects are not in a position to respond properly. Constructors are specially designed methods for initializing instance variables/objects. After the creation of objects immediately the constructor will be executed and initialization is performed. Rules for constructors Name of the constructor and the name of the class must be same. There is no return type for constructors, not even void. ( If return type is given, it is treated as a normal method. ie, you can give the name of the class as the name of a normal method,but strictly it is not recommended.) The access modifiers applicable for constructors are public, <default>, protected and private. Example:                          class test               {          String name;          ...