-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Java Fundamentals
- Java is a statically typed language.
- This means that every variable, parameter, and return value has a type.
- The type of a variable determines the size and layout of the variable's memory; the range of values that can be stored within that memory;
- The compiler will enforce these restrictions, and generate error messages if you violate them.
- Java requires four things to declare a variable in the program :
- The type of the variable
- The name of the variable
- The value of the variable
- semicolon to terminate the statement
-
Similarly, we can declare other variable's classes, interfaces, methods, etc.
//DataType variableName = value; int studentId = 10035;
- Java is an object-oriented language. Everything in java revolves around classes and objects.
- Creating a class is like building a skeleton of an Object.
- All the data members and member functions of a class are defined inside a class.
class Student {
// class body
}- An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
- An interface is defined by using the interface keyword.
interface Classroom {
// interface body
}- A class members are the variables and methods defined inside the class.
- A class can have any number of methods and variables.
- A class is useful for organizing the code and implementing OOP Concepts.
- Generally, there are two types of class members:
- Data members
- Member functions
class Student {
int studentId; //data member
String studentName; // data member
void display() { // member function
System.out.println("Student Id : " + studentId);
System.out.println("Student Name : " + studentName);
}
}Constructors
- A constructor is a special type of method that 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:
class Student {
int studentId;
String studentName;
Student(int id, String name) { // constructor
studentId = id; // set studentId
studentName = name; // set studentName
}
void display() {
System.out.println("Student Id : " + studentId);
System.out.println("Student Name : " + studentName);
}
}There are different types of constructors in Java:
- Default constructor (no parameters)
- A default constructor is a constructor that does not have any parameters.
- Java compiler automatically creates a no-arg constructor during the execution of the program.
class Student { int studentId; String studentName; } class TestStudent { public static void main(String[] args) { Student s1 = new Student(); // default constructor called } }
- No-arg constructor
- A no-arg constructor is a constructor that does not have any parameters.
- We can also create a no-arg constructor explicitly.
class Student { int studentId; String studentName; Student() { // no-arg constructor studentId = 1001; studentName = "Rishu"; } } class TestStudent { public static void main(String[] args) { Student s1 = new Student(); // default constructor called } }
- Parameterized constructor
- A parameterized constructor is a constructor that has parameters.
- We can create a parameterized constructor explicitly.
class Student { int studentId; String studentName; Student(int id, String name) { // parameterized constructor studentId = id; studentName = name; } }
- Copy constructor
- A copy constructor is a constructor that creates an object by copying an existing object.
- We can create a copy constructor explicitly.
class Student {
int studentId;
String studentName;
Student(int id, String name) { // parameterized constructor
studentId = id;
studentName = name;
}
Student(Student s) { // copy constructor
studentId = s.studentId;
studentName = s.studentName;
}
}Deconstructors/Destructors
- A destructor is a special type of method that is used to destroy objects.
- The destructor is called when an object of a class is destroyed and free up memory.
class Student {
int studentId;
String studentName;
Student(int id, String name) { // parameterized constructor
studentId = id;
studentName = name;
}
void display() {
System.out.println("Student Id : " + studentId);
System.out.println("Student Name : " + studentName);
}
protected void finalize() {
System.out.println("Object is destroyed");
}
}The access modifiers in Java specify the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and classes by applying the access modifier to them. There are three types of access modifiers in Java:
-
Default
- The access level of a default modifier is only within the package. It cannot be accessed from outside the package.
- If you do not specify any access level, it will be the default.
- We can apply the default modifier on the class, method, and constructor.
- We cannot apply the default modifier on the interface, variable, and nested interface.
-
defaultkeyword is not used in Java.
-
Private
- The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
- We can apply the private modifier on the variable, method, and constructor.
- We cannot apply the private modifier on the class, interface, and nested interface.
-
privatekeyword is used in Java to make anything private.
-
Public
- The access level of a public modifier is everywhere. It can be accessed from outside the class.
- We can apply the public modifier on the variable, method, and constructor.
- We can apply the public modifier on the class, interface, and nested interface.
- Public access modifier is used with all those classes, methods, and variables that you want to be visible to all other classes.
-
Protected
- The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed outside the package.
- We can apply the protected modifier on the variable, method, and constructor.
- We cannot apply the protected modifier on the class and interface.
-
protectedkeyword is used in Java to make anything protected.
In Java Initialization Blocks are used to initialize the state of an object. There are two types of initialization blocks in Java:
-
Instance Initialization Block
- The instance initialization block is invoked for each object creation.
- It is invoked after the parent class constructor
-
Static Initialization Block
- The static initialization block is invoked only once when the class is loaded.
- It is invoked before the main method at the time of class loading.
- It is used to initialize the static data member. because a constructor cannot be used to initialize the static data member without applying the condition.
Arrays in Java are used to store multiple values of the same type under one variable name. Array uses contiguous memory locations to store its elements. There are two ways to declare an array in Java:
- using
newkeyword
int[] arr = new int[5]; // declaring and initializing an array
arr[0] = 10; // initializing array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
arr[5] = 60; // ArrayIndexOutOfBoundsException- in one line
int[] arr = {10, 20, 30, 40, 50};In Java, all classes, interfaces, and enums are expected to written in Pascal case. Variables in Java are to be written in lowerCamelCase, and constant variables are in snake case.
-
Pascal case is a naming convention where each word in the identifier begins with a capital letter. For example,
PascalCase. -
Camel case is a naming convention where the first letter of the first word is lowercase and the first letter of each subsequent word is capitalized. For example,
camelCase. -
Snake case is a naming convention where each word in the identifier is separated by an underscore. For example,
SNAKE_CASE.