Java Basics

Here's a quick overview of some basic language elements in Java:

Variables

A variable is a name that represents a value. In Java, you must declare a variable with a specific data type, such as int, float, or String. For example:

int age = 25;
float price = 19.99;
String name = "John";

Here's an example program that uses some of these elements:

public class HelloWorld {
  public static void main(String[] args) {
    String name = "John";
    int age = 25;
    float price = 19.99f;

    System.out.println("Hello, " + name + "!");
    System.out.println("You are " + age + " years old.");

    if (price > 10) {
      System.out.println("The price is too high!");
    } else {
      System.out.println("The price is affordable.");
    }

    for (int i = 1; i <= 10; i++) {
      System.out.println(i);
    }
  }
}

This program declares some variables, uses a conditional statement to check the price, and uses a for loop to print out the numbers 1-10. When you run this program, it will print out the following output:

Hello, John!
You are 25 years old.
The price is too high!
1
2
3
4
5
6
7
8
9
10