Java Basics
-
Java is a versatile and widely used programming language known for its portability and ease of use. It's widely used for developing web and mobile applications, enterprise software, and more.
public class HelloWorld { public static main(String[] args) { System.out.println("Hello, Java!"); }}
-
Variables in Java store data, while data types define the kind of data a variable can hold. Common data types include int, double, and String, among others.
int age = 30; double price = 19.99; String name = "Amit Aryan";
-
Operators in Java perform various operations, including arithmetic, comparison, and logical operations. These are essential for manipulating data and making decisions in your programs.
int a = 10; int b = 5; int result = a + b; // result will be 15
-
Control structures like if-else, switch, and loops (for, while, do-while) help in controlling the flow of your Java programs, allowing you to make decisions and repeat actions as needed.
if (condition) { // Code to execute when the condition is true } else { // Code to execute when the condition is false }
-
Functions (methods) are reusable blocks of code that perform specific tasks. They improve code organization and make it easier to maintain and understand.
public int add(int a, int b) { return a + b; }
-
Arrays are used to store multiple values of the same type in Java. They are fundamental for working with collections of data.
int[] numbers = {1, 2, 3, 4, 5};
-
Object-Oriented Programming (OOP) is a paradigm in Java that focuses on creating objects and classes. This concept helps in organizing and modeling real-world entities.
class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } }
-
Inheritance allows one class (subclass or derived class) to inherit attributes and methods from another class (superclass or base class). It facilitates code reuse and extensibility.
class Vehicle { String brand; public Vehicle(String brand) { this.brand = brand; } } class Car extends Vehicle { int speed; public Car(String brand, int speed) { super(brand); this.speed = speed; } }
-
Polymorphism enables objects of different classes to be treated as objects of a common superclass. This promotes flexibility and dynamic method invocation in Java programs.
class Animal { public void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { public void sound() { System.out.println("Dog barks"); } } class Cat extends Animal { public void sound() { System.out.println("Cat meows"); }
-
Sometimes, while executing programs, we get errors that influence the normal working of the program. Such errors are called exceptions and the method used to deal with these exceptions is known as exception handling.
Exception -Handeling types and their code-snippet
//1.************ try..catch************ public class TryCatch { public static void main(String[] args) { try { int result = 36/0; } catch(ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } } } //2.*******Multiple catchpublic class MultipleCatch******** public static void main(String[] args) { try { int result = 36/0; } catch(NullPointerException e1) { System.out.println("Error: " + e1.getMessage()); } catch(ArithmeticException e2) { System.out.println("Error: " + e2.getMessage()); } } //3.********try... finally*********** public class TryFinally { public static void main(String[] args) { try { int result = 36/0; } finally { int result = 36/6; System.out.println("Finally block result:" + result); } } } //4.********try.....catch....finally********* public class TryCatchFinally { public static void main(String[] args) { try { int result = 36/0; } catch(ArithmeticException e) { System.out.println("Error: " + e.getMessage()); } finally { int result = 36/6; System.out.println("Finally block result:" + result); } } } //5.********throws*********** public class Throws { public static void example() throws ArithmeticException { int result = 36/0; } public static void main(String[] args) { try { example(); } catch (ArithmeticException e) { System.out.println(e); } } } //6.********throw********** public class Throw { public static void example() { throw new ArithmeticException("divide by 0"); } public static void main(String[] args) { example(); } }