Search Jobs

Ticker

6/recent/ticker-posts

Most Important Program In Java for Interview

Java is a versatile programming language used in various domains, from web development to mobile apps and more. Here are some 

important Java programs and concepts to get you started:


1. Hello World:


public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}


This is the simplest Java program, printing "Hello, World!" to the console.


2. Variables and Data Types:

Java supports various data types, including int, double, char, boolean, and more. Here's an example:


int age = 30;

double price = 19.99;

char grade = 'A';

boolean isJavaFun = true;


3. Conditional Statements (if-else):


int number = 10;

if (number > 0) {

    System.out.println("Positive");

} else if (number < 0) {

    System.out.println("Negative");

} else {

    System.out.println("Zero");

}


4. Loops (for, while, do-while):


For Loop:


for (int i = 0; i < 5; i++) {

    System.out.println("Iteration " + i);

}


5. While Loop:


int count = 0;

while (count < 5) {

    System.out.println("Count: " + count);

    count++;

}


6. Functions (Methods):



public static int add(int a, int b) {

    return a + b;

}


public static void main(String[] args) {

    int result = add(5, 3);

    System.out.println("Result: " + result);

}


7. Arrays:


int[] numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.length; i++) {

    System.out.println("Element at index " + i + ": " + numbers[i]);

}


8. Classes and Objects:


class Person {

    String name;

    int age;


    public void introduce() {

        System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");

    }

}


public static void main(String[] args) {

    Person person1 = new Person();

    person1.name = "Alice";

    person1.age = 25;

    person1.introduce();

}


9. Exception Handling (try-catch):


try {

    int result = 10 / 0; // This will throw an ArithmeticException

} catch (ArithmeticException e) {

    System.out.println("An error occurred: " + e.getMessage());

}



10. Object-Oriented Programming (OOP):

Java is known for its strong support for OOP principles. You can create classes, objects, and use inheritance and polymorphism.


class Animal {

void makeSound() {

System.out.println("Some sound");

}

}


class Dog extends Animal {

@Override

void makeSound() {

System.out.println("Bark");

}

}


public static void main(String[] args) {

Animal animal = new Dog();

animal.makeSound(); // Outputs "Bark"

}


11. Encapsulation, Inheritance, and Polymorphism:

Java supports encapsulation with private, protected, and public access modifiers. Inheritance allows you to create subclasses, and polymorphism allows you to work with objects of different classes through a common interface or base class.


12. Collections Framework:

Java provides a rich set of data structures through the Collections Framework, including ArrayList, HashMap, HashSet, LinkedList, etc., for managing collections of objects efficiently.


13. File I/O:

You can read and write files in Java using FileReader, FileWriter, BufferedReader, BufferedWriter, and other classes.


try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {

String line;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

} catch (IOException e) {

e.printStackTrace();

}


14. Exception Handling (try-catch-finally):

Java allows you to handle exceptions gracefully with try-catch blocks, and the "finally" block ensures cleanup code is executed, whether an exception occurs or not.


15. Multithreading:

Java supports multithreading for concurrent programming. You can create and manage threads using the Thread class or by implementing the Runnable interface.


class MyRunnable implements Runnable {

public void run() {

// Code to run concurrently

}

}


public static void main(String[] args) {

Thread thread1 = new Thread(new MyRunnable());

Thread thread2 = new Thread(new MyRunnable());

thread1.start();

thread2.start();

}


16. JavaFX (for GUI):

JavaFX is a platform for creating desktop applications with graphical user interfaces (GUIs). It provides tools for building interactive and visually appealing applications.


17. JDBC (Java Database Connectivity):

You can connect Java applications to databases using JDBC, allowing you to interact with relational databases like MySQL, PostgreSQL, and Oracle.


Annotations:

Java annotations are metadata that can be added to classes, methods, and fields to provide additional information to the compiler or runtime environment.


Lambda Expressions:

Java 8 introduced lambda expressions, which simplify the use of interfaces with a single abstract method (functional interfaces) and make code more concise.


List<String> names = Arrays.asList("Alice", "Bob", "

Charlie");

names.forEach(name -> System.out.println("Hello, " + name));

Post a Comment

0 Comments