Welcome to the basics of Java! Java is a popular programming language known for its "write once, run anywhere" philosophy. This guide will cover the fundamental concepts of Java programming.
Getting Started
Before diving into Java, make sure you have the following:
- Java Development Kit (JDK)
- Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse
Hello World
The "Hello World" program is a classic way to get started with any programming language. Here's a simple Java program to display "Hello, World!" on the console:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Variables
In Java, variables are used to store data. Here's an example of declaring and initializing a variable:
int number = 5;
Data Types
Java has several data types, including:
- Primitive Types:
int
,float
,double
,char
,boolean
- Reference Types:
String
,Array
,Class
,Interface
Control Structures
Control structures allow you to control the flow of your program. Here are some common ones:
- If-Else: Conditionally execute code blocks
- For Loop: Iterate over a sequence of values
- While Loop: Continue executing a block of code as long as a condition is true
Functions
Functions are reusable blocks of code that perform a specific task. Here's an example of a function that adds two numbers:
public static int add(int a, int b) {
return a + b;
}
Object-Oriented Programming (OOP)
Java is an object-oriented programming language. This means you can create objects that represent real-world entities. Here are some key concepts:
- Classes: Blueprints for creating objects
- Objects: Instances of classes
- Inheritance: Creating new classes based on existing ones
- Polymorphism: Allowing objects of different classes to be treated as instances of a common superclass
Resources
For more information on Java, check out the following resources: