march articles code review

This commit is contained in:
Mayank Agarwal
2023-03-18 09:37:04 +05:30
parent 44bb31c1d6
commit 5f09e78f92
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
import java.util.*;
public class JavaContinue {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
}
public static void breakExample(String[] args) {
int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
outer: for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] == 5) {
System.out.println("Found 5 at index [" + i + "][" + j + "]");
break outer;
}
}
}
}
}

View File

@@ -0,0 +1,42 @@
import java.util.*;
public class JavaFinal {
public static void main(String[] args) {
javaInstanceOfOperator();
}
public static void javaInstanceOfOperator() {
String str = "Hello, World!";
boolean result = str instanceof String;
System.out.println(result);
}
class Animal {}
class Dog extends Animal {}
public void checkInstance() {
Animal animal = new Dog();
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
System.out.println("Woof!");
}
}
public void javaInstanceOfInterface() {
Thread thread = new Thread();
if (thread instanceof Runnable) {
System.out.println("Thread implements Runnable");
}
}
public void typeCastingWithInstanceOf() {
Object animal = new Cat();
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
} else if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.meow();
}
}
}