Class and Object. - mehnazchaudary9595/java-learning GitHub Wiki

WHAT IS A CLASS IN JAVA ?

A class--the basic building block of an object-oriented language such as Java--is a template that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in procedural languages such as C.

A class can contain any of the following variable types.

** ### Local variables:** Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

** ### Instance variables:** Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

### Class variables:

Class variables are variables declared with in a class, outside any method, with the static keyword. In the Java language, the simplest form of a class definition is

class name {
    . . .

WHAT IS AN OBJECT ?

An object can be a variable, a data structure, or a function. In the class-based object-oriented programming paradigm, "object" refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures Objects are key to understanding object-oriented technology. examples of real-world objects: your dog, your desk, your television set, your bicycle.. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). DIFFERENCE BETWEEN AN OBJECT AND A CLASS?

      ### OBJECT                                                   ###   CLASS

Object is an instance of a class. Class is a blueprint or template from which objects create Object is a physical entity. Class is a logical entity. Object is created many times as per requirement Class is declared once. Object allocates memory when it is created. Class doesn't allocated memory Object is a real world entity such as pen,laptop Class is a group of similar objects.

DIFFERENCE BETWEEN PRIMATIVE AND NON PRIMATIVE?

PRIMATIVE ...

The term "data type" and "primitive data type" are often used interchangeably. Primitive data types are predefined types of data, which are supported by the programming language. For example, integer, character, and string are all primitive data types. Programmers can use these data types when creating variables in their programs. For example, a programmer may create a variable called "lastname" and define it as a string data type. The variable will then store data as a string of characters.

NON PRIMATIVE...

Non-primitive data types are not defined by the programming language, but are instead created by the programmer. They are sometimes called "reference variables," or "object references," since they reference a memory location, which stores the data. In the Java programming language, non-primitive data types are simply called "objects" because they are created, rather than predefined. While an object may contain any type of data, the information referenced by the object may still be stored as a primitive data type.