What is Class and Object ?
Class :- A class in Java is essentially a collection of variables and functions that define the
structure (data) and behavior (actions) of an object.
For Example :- Class Car: A collection of variables (model, year, color) and functions (start(),
stop(), display_Info()).
Object :- An instance of the class, created using the new keyword, which holds its own
specific values for the variables. Using object we can access methods of class.
For Example :- Car mycar = new Car();
Function :- In Java, a function is called a method. It is a block of code that performs a
specific task.
Syntax :-
Simple Example:
Access Modifiers in Java
Access modifiers in Java control the visibility and accessibility of classes, methods, variables,
and constructors.
Constructor in Java :- A constructor in Java is a special method that is automatically
called when an object is created. It is used to initialize the object's state (set values for its
variables).
Key Points:
1. Constructor Name: It must have the same name as the class.
2. No Return Type: Constructors don't have a return type, not even void.
3. Automatic Call: When you create an object, the constructor is called automatically.
Types Of Constructor
Data Types & Variables In Java
In Java, data types define the type of data a variable can hold. They are essential for
specifying what kind of data will be stored in a variable.
1. Declaring Variables
To declare a variable in Java, you specify the data type followed by the variable name.
Syntax : Datatype Variablename;
For example: int number; Datatype ==> int, Variablename ==> number
String text; Datatype ==> String , Variablename ==> text
2. Categories of Data Types
Data types in Java can be categorized into two main types:
Primitive Data Types
Non-Primitive Data Types
Primitive Data Types Declaration :
Example :-
Output:
Non-Primitive Data Types Declaration:
Example :-
Output:
Java Operators: Arithmetic Operators
Example :-
Output:
Java Operators: Relational (Comparison) Operators
Example :-
Output:
Java Operators: Increment/Decrement Operators
Example :-
Output:
Output: