Supercharge your development with unmatched features:
Access a full terminal environment, run Linux commands, and manage your project’s dependencies directly within the IDE.
Browse and interact with websites directly within the IDE. Supports real-time interaction with web content without leaving the workspace.
Manage your project files and directories effortlessly within the IDE. Create, edit, rename, move, and delete files—all in one place.
Experience seamless code editing with real-time syntax highlighting, tab support, and intelligent code suggestions for a smoother development workflow.
Java is a high-level, class-based, object-oriented programming language. It is widely used for building platform-independent applications, ranging from mobile apps to enterprise systems.
JAVA_HOME
environment variable and update the PATH
variable.java -version
in the terminal.Let’s write our first Java program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save this code in a file named HelloWorld.java
, then compile and run it:
javac HelloWorld.java
java HelloWorld
In Java, variables are declared with specific data types. Here are some common data types:
public class DataTypes {
public static void main(String[] args) {
int x = 10; // Integer
float y = 3.14f; // Float
char letter = 'A'; // Character
String name = "Java"; // String
System.out.println("x = " + x + ", y = " + y + ", letter = " + letter + ", name = " + name);
}
}
Conditional statements in Java include if
, else if
, and else
.
public class ConditionalStatements {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("Positive number");
} else if (num == 0) {
System.out.println("Zero");
} else {
System.out.println("Negative number");
}
}
}
Java supports for
, while
, and do-while
loops.
public class ForLoop {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
public class WhileLoop {
public static void main(String[] args) {
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
}
}
}
Methods in Java help to modularize code.
public class Methods {
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Alice");
}
}
Arrays in Java are used to store multiple values of the same data type.
public class Arrays {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
}
}
Classes and objects are fundamental to Java's object-oriented programming.
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
Student s1 = new Student("John", 21);
s1.displayInfo();
}
}
File handling in Java is done using classes from the java.io
package.
import java.io.*;
public class FileHandling {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, Java!");
writer.close();
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
String line = reader.readLine();
System.out.println(line);
reader.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Inheritance in Java allows one class to inherit the fields and methods of another class.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Inheritance {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
Java has a rich set of libraries for various functionalities. Some commonly used libraries are: