Java Basics

### Java Basics Java is a versatile and widely-used programming language that is particularly known for its portability across platforms, robust security features, and rich API libraries. Below are the fundamental concepts and components that form the basics of Java programming. #### 1. **Variables and Data Types** Variables are used to store data which can be manipulated throughout the program. Java is a strongly-typed language, meaning every variable must be declared with a data type. - **Primitive Data Types**: byte, short, int, long, float, double, char, boolean. - **Non-Primitive Data Types**: String, Arrays, Classes, Interface. Example: ```java int age = 25; double salary = 50000.50; char grade = 'A'; boolean isEmployed = true; String name = "John Doe"; ``` #### 2. **Operators** Operators are special symbols that perform operations on variables and values. - **Arithmetic Operators**: `+`, `-`, `*`, `/`, `%` - **Relational Operators**: `==`, `!=`, `>`, `<`, `>=`, `<=` - **Logical Operators**: `&&`, `||`, `!` - **Assignment Operators**: `=`, `+=`, `-=`, `*=`, `/=`, `%=` Example: ```java int a = 10; int b = 20; int sum = a + b; // sum is 30 boolean isEqual = (a == b); // isEqual is false ``` #### 3. **Control Statements** Control statements manage the flow of a program. - **Conditional Statements**: if, else if, else, switch - **Looping Statements**: for, while, do-while Example: ```java // if-else statement int number = 10; if (number > 0) { System.out.println("Positive number"); } else { System.out.println("Negative number"); } // for loop for (int i = 0; i < 5; i++) { System.out.println("i: " + i); } // switch statement int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other day"); } ``` #### 4. **Arrays** Arrays are used to store multiple values in a single variable. Example: ```java int[] numbers = {1, 2, 3, 4, 5}; String[] names = {"John", "Jane", "Doe"}; for (int i = 0; i < numbers.length; i++) { System.out.println("Number: " + numbers[i]); } for (String name : names) { System.out.println("Name: " + name); } ``` #### 5. **Methods** Methods are blocks of code that perform a specific task and are used to avoid code duplication. Example: ```java public class Main { public static void main(String[] args) { int result = add(5, 10); System.out.println("Sum: " + result); } public static int add(int a, int b) { return a + b; } } ``` #### 6. **Object-Oriented Programming (OOP)** Java is an object-oriented programming language, which means it is based on objects and classes. - **Class**: A blueprint for creating objects. - **Object**: An instance of a class. - **Inheritance**: Mechanism to acquire the properties and behaviors of a parent class. - **Polymorphism**: Ability to take many forms. Achieved through method overloading and overriding. - **Encapsulation**: Wrapping of data and methods into a single unit. - **Abstraction**: Hiding complex implementation details and showing only the essential features. Example: ```java // Class definition class Animal { void eat() { System.out.println("This animal eats food."); } } // Inheritance class Dog extends Animal { void bark() { System.out.println("Dog barks."); } } // Polymorphism class Cat extends Animal { @Override void eat() { System.out.println("Cat eats fish."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited method dog.bark(); // Dog-specific method Animal animal = new Cat(); animal.eat(); // Polymorphism } } ``` #### Conclusion Mastering these basics of Java will provide a solid foundation for further exploring advanced concepts and frameworks. Java's syntax is straightforward, making it easier to understand and apply the principles of object-oriented programming effectively.

Comments

Popular posts from this blog

Advanced Java Concepts

Welcome

Java Introduction