Java Tutorial menu

Java Variables and Data Types

A variable is a named box that stores a value your program can use and change.

Creating Variables

To create a variable, write its type, its name, an equals sign and a value:

Example

java
public class Main {
  public static void main(String[] args) {
    String name = "Ali";
    int age = 19;
    double fee = 3500.50;

    System.out.println(name);
    System.out.println(age);
    System.out.println(fee);
  }
}
Output
Ali
19
3500.5

You can change a variable later by giving it a new value:

Example

java
int marks = 70;
marks = 85;   // marks is now 85

Primitive Data Types

Java has eight basic (primitive) types. These are the ones you will use most:

TypeStoresExample
intWhole numbersint students = 30;
longVery large whole numberslong population = 20000000L;
doubleDecimal numbersdouble price = 99.99;
floatDecimal numbers, less precisefloat rate = 2.5f;
charOne character, in single quoteschar grade = 'A';
booleantrue or falseboolean passed = true;

byte and short also exist for small whole numbers, but they are used less often.

The String Type

String holds text in double quotes. It is not a primitive type; it is a class, which is why it starts with a capital S. You will learn more in the Strings chapter.

Joining Text and Variables

Use + to join text and variables in one line:

Example

java
public class Main {
  public static void main(String[] args) {
    String student = "Ayesha";
    int marks = 92;
    char grade = 'A';
    boolean passed = true;

    System.out.println(student + " got " + marks + " marks");
    System.out.println("Grade: " + grade + ", Passed: " + passed);
  }
}
Output
Ayesha got 92 marks
Grade: A, Passed: true

Naming Rules

  • Names can contain letters, digits, _ and $, but cannot start with a digit
  • Names are case sensitive: total and Total are different
  • You cannot use Java keywords like int or class as names
  • Use camelCase for variables: totalPrice, studentName

Constants with final

Add final when a value must never change:

Example

java
final int DAYS_IN_WEEK = 7;
// DAYS_IN_WEEK = 8;  would cause an error

The var Keyword

Since Java 10, var lets Java work out the type from the value:

Example

java
var city = "Karachi";   // Java knows this is a String
var count = 12;         // Java knows this is an int

Type Casting

Casting changes a value from one type to another.

  • Widening (small to big) happens automatically: int to double
  • Narrowing (big to small) must be written by hand with (type)

Example

java
public class Main {
  public static void main(String[] args) {
    int items = 9;
    double asDouble = items;        // automatic
    double average = 7.8;
    int rounded = (int) average;    // manual, the decimal part is cut off

    System.out.println(asDouble);
    System.out.println(rounded);
  }
}
Output
9.0
7

Casting a double to int does not round the number. It simply removes everything after the decimal point, so 7.8 becomes 7.

Exercise

Exercise

Which data type should you use to store the value 'B'?

Learn Java with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp