- To check two numbers for equality in Java, we can use the Equals() method as well as the == operator.
- Firstly, let us set Integers. Integer val1 = new Integer(5); Integer val2 = new Integer(5); Now, to check whether they are equal or not, let us use the == operator.
public class Example {
public static void main(String[] args) {
int x = 20;
int y = 23;
if(x==y) {
System.out.println("Both are equal");
}
else {
System.out.println("x and y are not equal");
}
}
}
- The == operator, known as the equality operator, is used to compare two strings in Java.
- Using user-defined function : Define a function to compare values with following conditions :
- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
- i.e.(string1 == string2) it returns 0.
- if (string1 < string2) it returns a negative value.
public class StringComp {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s1 ==s2);
System.out.println(s1 ==s3);
}
}
- Comparing objects is an essential feature of object-oriented programming languages.
- By comparing two objects, the value of those objects isn't 1. Rather, it's their memory addresses in the stack that are different, since both objects are created using the new operator. If we assigned a to b, then we would have a different result
- In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects.
public class Netflix {
public static void main(String[] args) {
String s1 = "name";
String s2 = "name";
if(s1 == (s2)) {
System.out.println(s1);
}
}
}
- A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
- A for loop is useful when you know how many times a task is to be repeated.
public class Vehicles {
public static void main(String[] args) {
String[] cars = {"Audi", "BMW", "Nissan"};
for(String car : cars);
}
}
- The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true.
- As soon as the Boolean condition becomes false, the loop automatically stops.
- The while loop is considered as a repeating if statement.
class WhileLoop {
public static void main(String[] args) {
int i = 1;
while(i <= 6) {
System.out.println(i);
i++;
}
}
}
- A nested loop is a (inner) loop that appears in the loop body of another (outer) loop.
- The inner or outer loop can be any type: while, do while, or for.
- For example, the inner loop can be a while loop while an outer loop can be a for loop.
for (int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++)
System.out.println(i + " " + j);
System.out.println();
}
- Everything in Java is associated with classes and objects, along with its attributes and methods.
- For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
- A Class is like an object constructor, or a "blueprint" for creating objects.
- Classes are required in OOPs because: It provides template for creating objects, which can bind code into data.
- It has definitions of methods and data. It supports inheritance property of Object Oriented Programming and hence can maintain class hierarchy.
- A constructor in Java is a special method that is used to initialize objects.
- The constructor is called when an object of a class is created.
- In Java, constructors can be divided into 3 types: No-Arg Constructor. Parameterized Constructor. Default Constructor.
public String getName() {
return name;
}
public static void main(String[] args) {
Data d = new Data("Java");
System.out.println(d.getName());
}