java basics
java basics
Java Development Kit (JDK): Download the latest JDK from Oracle’s website (or use
an OpenJDK distribution). Once installed, set your JAVA_HOME environment variable and
add the bin folder to your system’s PATH.
Integrated Development Environment (IDE): While you can compile Java code using
the command line, many beginners find it easier to work with an IDE such as Eclipse,
IntelliJ IDEA, or NetBeans.
Class Declaration: Every program is defined within a class. In this example, the class is
HelloWorld.
Main Method: The JVM starts execution from the main method. It must be declared as
public static void main(String[] args).
Primitive Data Types - specify the size and type of variable values
byte: 8-bit integer.
short: 16-bit integer.
int: 32-bit integer.
long: 64-bit integer.
float: Single-precision 32-bit floating-point.
double: Double-precision 64-bit floating-point.
char: 16-bit Unicode character.
boolean: true or false.
Example:
Reference Types
Example:
5. Operators
Java supports several types of operators:
Arithmetic Operators: +, -, *, /, %
Assignment Operators: =, +=, -=, etc.
Comparison Operators: ==, !=, >, <, >=, <=
Logical Operators: &&, ||, !
Example:
int a = 10;
int b = 3;
int sum = a + b; // 13
boolean result = (a > b) && (b < 5); // true
6. Control Flow Statements
Conditional Statements
Loops
Example:
java
.
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + i);
}
int j = 0;
while (j < 5) {
System.out.println("Iteration " + j);
j++;
}
7. Methods
Methods (or functions) are blocks of code that perform a specific task. They can accept
parameters and return values.
Example:
java
.
public class Calculator {
// A method that adds two integers and returns the result
public int add(int x, int y) {
return x + y;
}
Example:
java
.
public class Person {
// Attributes
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
Inheritance
java
.
public class Student extends Person {
int studentId;
Polymorphism
Refers to the ability of a variable, function, or object to take on multiple forms. For instance, a
method in a superclass can be overridden in a subclass.
Encapsulation
Encapsulation means keeping the data (fields) and the code (methods) safe from outside
interference and misuse. This is often achieved by using private fields and public getter/setter
methods.
Abstraction
Abstraction hides complex implementation details and shows only the necessary features of an
object.
java
.
package com.example.myapp;
You can import classes from other packages using the import statement:
java
.
import java.util.ArrayList;
public class Example {
ArrayList<String> list = new ArrayList<>();
}
java
.
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("This block executes regardless of an
exception.");
}
}
}
java
.
import java.util.Scanner;
scanner.close();
}
}