Java_Ignou
Java_Ignou
Java
Literals in Java
In Java, literals are fixed values that appear directly in the code. They
represent the actual values that are assigned to variables.
1.Integer literals: Represent whole numbers, like 10, 0, -5. They can be
expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base
2).
•Example: int x = 42;
2.Floating-point literals: Represent numbers with decimal points. They
default to double unless specified as float by adding an "f" or "F" at the end.
•Example: double pi = 3.14159;, float rate = 2.5f;
Example:
class Car {
String model; // Instance variable
int year;
}
Variables in Java
• Static Variables (Class Variables):Declared with the static keyword
inside a class but outside methods.
• Shared by all instances of the class, meaning they belong to the class
rather than individual objects.
• Initialized only once at the start of the program execution.
Example:
class Car {
static int numberOfCars; // Static variable
}
Operators in Java
• In Java, operators are symbols used to perform operations on
variables and values. They are essential for manipulating data and
performing calculations in a program.
• Arithmetic operators
Operators in Java
• Unary Operators - Unary operators operate on a single operand.
Operators in Java
• Assignment Operators - Assignment operators assign values to
variables.
Operators in Java
• Relational (Comparison) Operators - These operators compare two
values and return a boolean result (true or false).
Operators in Java
• Ternary Operator ?:
• The ternary operator is a concise way to evaluate a condition and
return one of two values.
Method overloading
• Method Overloading allows multiple methods to have the same name
but differ in the number or type of parameters.
• It’s a form of polymorphism (specifically, compile-time or static
polymorphism), where the compiler determines which method to invoke
based on the arguments passed.
• Example:
• // Calculator class with overloaded add() methods
class Calculator
{
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
Method overloading
// Method to add three numbers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method to add four numbers
public int add(int a, int b, int c, int d) {
return a + b + c + d;
}
}
Method Overloading
public class Main {
public static void main(String[] args) {
// Create an object of Calculator class
Calculator calc = new Calculator();
// Calling the overloaded add() methods
System.out.println("Sum of 2 numbers: " + calc.add(10,
20)); // Adds 2 numbers
System.out.println("Sum of 3 numbers: " + calc.add(10, 20, 30));
// Adds 3 numbers
System.out.println("Sum of 4 numbers: " + calc.add(10, 20, 30, 40));
// Adds 4 numbers
}
}
Method overriding
• Method overriding in Java is a feature that allows a subclass (child
class) to provide a specific implementation for a method that is
already defined in its superclass (parent class).
• When a method in a subclass has the same name, return type, and
parameters as a method in its superclass, the subclass's version of the
method "overrides" the superclass's version.
• Key Points of Method Overriding - Same Method Signature: The
method in the subclass must have the same name, return type, and
parameters as the method in the superclass.
• Example - Consider a scenario with a superclass Animal and a subclass
Dog, where both have a sound() method, but Dog overrides it to
provide a more specific sound.
Method overriding
// Superclass
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
Method overriding
public class Main {
public static void main(String[] args) {
// Creating an object of the superclass
Animal a = new Animal();
a.sound(); // Calls Animal's sound method
// Constructor
public Animal(String name) {
this.name = name;
}
// Method
public void eat() {
System.out.println(name + " is eating.");
}
}
Inheritance
// Subclass
// Constructor
public Dog(String name) {
super(name); // Calls the superclass (Animal) constructor
}
// Additional behavior
public void bark() {
System.out.println(name + " is barking.");
}
}
Inheritance
public class Main {
equals() Compares two strings. Returns true if the strings are boolean
equal, and false if not
replace() Searches a string for a specified value, and returns a new String
string where the specified values are replaced
replaceAll() Replaces each substring of this string that matches the String
given regular expression with the given replacement