Paramclasses Academy - Blog

Tutorials Details

←
Post 5 of 40
→

What is Java Basics for Android Developers: Variables, Data Types and Operators ?

What is Java Basics for Android Developers: Variables, Data Types and Operators ?

November 2, 2026

Introduction

Java is an important programming language used in Android application development. It allows developers to write application logic, process data, handle user interactions, and perform different operations.

Before building advanced Android applications, you need to understand some basic Java concepts.

In this tutorial, we will learn three important Java fundamentals:

  • Variables

  • Data Types

  • Operators

We will also understand how these concepts work through simple Java programs and practical Android-related examples.

By the end of this tutorial, you will understand how to store data, identify different data types, and perform calculations using Java.

1. What Is Java?

Java is a high-level, object-oriented programming language used to develop different types of software applications.

It is widely used in backend development, enterprise applications, and Android development.

In Android applications, Java can be used to:

  • Handle button clicks.

  • Process user input.

  • Perform calculations.

  • Validate forms.

  • Manage application data.

  • Connect applications with APIs.

  • Work with databases.

For example, in a calculator application, Java performs mathematical calculations when a user clicks an operation button.

2. What Is a Variable in Java?

A variable is a named storage location used to hold data in a program.

Think of a variable as a container.

For example, if you want to store a student's age, you can create a variable named age and assign a value to it.

Example:

int age = 20;

Here:

  • int defines the data type.

  • age is the variable name.

  • 20 is the value stored in the variable.

Now Java can use this value whenever required.

How to Declare a Variable in Java

The basic syntax is:

dataType variableName = value;

Example:

int number = 10;
String name = "Rahul";
double price = 99.50;

Here, we have created three variables:

  • number stores an integer.

  • name stores text.

  • price stores a decimal number.

Declare and Assign Variables Separately

You can also declare a variable first and assign its value later.

int marks;

marks = 85;

System.out.println(marks);

Output:

85

First, we declare the variable marks.

Then we assign the value 85.

Finally, we print its value.

Change the Value of a Variable

A variable's value can be changed during program execution.

int number = 10;

number = 25;

System.out.println(number);

Output:

25

Initially, the variable contains 10.

Later, we replace that value with 25.

The latest assigned value is printed.

3. Rules for Naming Variables in Java

Java has certain rules for naming variables.

Follow these rules when creating variable names:

  1. A variable name can contain letters, digits, underscores, and dollar signs.

  2. A variable name cannot start with a digit.

  3. Spaces are not allowed in variable names.

  4. Java keywords cannot be used as variable names.

  5. Variable names are case-sensitive.

  6. Use meaningful names to make code easier to understand.

Valid variable names:

int age = 20;
String studentName = "Amit";
double totalPrice = 450.50;
int _number = 10;

Invalid variable names:

// int 2number = 10;
// int student name = 20;
// int class = 10;

The examples above are invalid because they violate Java naming rules.

Important: Java is case-sensitive.

For example:

int age = 20;
int Age = 25;

Here, age and Age are two different variables.

4. What Are Data Types in Java?

A data type defines what kind of value a variable can store.

For example:

  • Integer values.

  • Decimal values.

  • Characters.

  • Text.

  • True or false values.

Java provides different data types to store different kinds of information.

There are two main categories of Java data types:

  1. Primitive Data Types

  2. Non-Primitive Data Types

5. Primitive Data Types in Java

Primitive data types are the basic built-in data types provided by Java.

Java has eight primitive data types.

1. int

The int data type stores whole numbers without decimal points.

Example:

int age = 22;
int marks = 85;

System.out.println(age);
System.out.println(marks);

Output:

22
85

Use int when you need to store values such as age, marks, or quantity within its supported range.

2. long

The long data type stores large whole numbers.

Example:

long population = 8000000000L;

System.out.println(population);

Output:

8000000000

The L suffix indicates a long literal.

3. float

The float data type stores decimal numbers with single precision.

Example:

float price = 99.50f;

System.out.println(price);

Output:

99.5

The f suffix indicates a float literal.

4. double

The double data type stores decimal numbers with double precision.

Example:

double salary = 25000.75;

System.out.println(salary);

Output:

25000.75

In Java, decimal literals are treated as double by default.

5. char

The char data type stores a single character.

Example:

char grade = 'A';

System.out.println(grade);

Output:

A

A char value is written inside single quotation marks.

6. boolean

The boolean data type stores only two possible values:

  • true

  • false

Example:

boolean isLoggedIn = true;

System.out.println(isLoggedIn);

Output:

true

It is useful for conditions such as checking whether a user is logged in.

7. byte

The byte data type stores small whole numbers.

Its range is -128 to 127.

Example:

byte number = 100;

System.out.println(number);

Output:

100

8. short

The short data type stores whole numbers within the range -32,768 to 32,767.

Example:

short marks = 1500;

System.out.println(marks);

Output:

1500

6. What Are Non-Primitive Data Types?

Non-primitive data types are used to represent objects and more complex data.

Some common examples include:

  • String

  • Array

  • Class

  • Interface

  • Object

String

String is used to store text.

Example:

String studentName = "Rahul";

System.out.println(studentName);

Output:

Rahul

Unlike char, String can store multiple characters.

For example:

char grade = 'A';

String name = "Rahul Kumar";

Array

An array stores multiple values of the same declared element type.

Example:

int[] marks = {80, 85, 90, 95};

System.out.println(marks[0]);

Output:

80

Array indexing starts from zero.

Therefore, marks[0] accesses the first element.

Class and Object

A class is a blueprint used to create objects.

An object is an instance of a class.

Example:

class Student {

    String name = "Rahul";
    int age = 20;

}

public class Main {

    public static void main(String[] args) {

        Student student = new Student();

        System.out.println(student.name);
        System.out.println(student.age);

    }
}

Output:

Rahul
20

Here, Student is a class and student is an object created from that class.

7. What Are Operators in Java?

Operators are special symbols used to perform operations on values and variables.

For example, operators can perform mathematical calculations, compare values, and check conditions.

Java provides several types of operators.

Let's understand the most important ones.

8. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Common arithmetic operators include:

  • + Addition

  • - Subtraction

  • * Multiplication

  • / Division

  • % Modulus

Example:

int a = 20;
int b = 10;

System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);

Output:

30
10
200
2
0

Let's understand each operation:

Addition (+): Adds two values.

Subtraction (-): Subtracts one value from another.

Multiplication (*): Multiplies two values.

Division (/): Divides one value by another.

Modulus (%): Returns the remainder after division.

For example:

int result = 10 % 3;

System.out.println(result);

Output:

1

Because 10 divided by 3 leaves a remainder of 1.

9. Assignment Operators

Assignment operators are used to assign or update variable values.

The most common assignment operator is =.

Example:

int number = 10;

Here, = assigns the value 10 to the variable.

Other assignment operators include:

int a = 10;

a += 5;
System.out.println(a);

a -= 3;
System.out.println(a);

a *= 2;
System.out.println(a);

a /= 4;
System.out.println(a);

Output:

15
12
24
6

Let's understand:

  • a += 5 means a = a + 5.

  • a -= 3 means a = a - 3.

  • a *= 2 means a = a * 2.

  • a /= 4 means a = a / 4.

These operators make code shorter and easier to write.

10. Relational Operators

Relational operators compare two values.

They return either true or false.

Common relational operators include:

  • == Equal to

  • != Not equal to

  • > Greater than

  • < Less than

  • >= Greater than or equal to

  • <= Less than or equal to

Example:

int a = 20;
int b = 10;

System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);

Output:

false
true
true
false
true
false

For example:

int age = 20;

System.out.println(age >= 18);

Output:

true

This comparison can be used to check whether a user meets an age requirement.

11. Logical Operators

Logical operators are used to combine or reverse conditions.

Java provides three main logical operators:

  • && Logical AND

  • || Logical OR

  • ! Logical NOT

Logical AND (&&)

Returns true when both conditions are true.

Example:

int age = 22;
boolean hasId = true;

System.out.println(age >= 18 && hasId);

Output:

true

Both conditions are true, so the final result is true.

Logical OR (||)

Returns true when at least one condition is true.

Example:

int age = 16;
boolean hasPermission = true;

System.out.println(age >= 18 || hasPermission);

Output:

true

The second condition is true, so the final result is true.

Logical NOT (!)

Reverses a boolean value.

Example:

boolean isLoggedIn = false;

System.out.println(!isLoggedIn);

Output:

true

The NOT operator changes false into true.

12. Increment and Decrement Operators

Increment and decrement operators increase or decrease a variable's value by one.

Increment (++)

Example:

int number = 10;

number++;

System.out.println(number);

Output:

11

The increment operator increases the value by one.

Decrement (--)

Example:

int number = 10;

number--;

System.out.println(number);

Output:

9

The decrement operator decreases the value by one.

Difference Between ++a and a++

Both operators increase the value by one, but their behavior differs when used inside an expression.

Post-increment:

int a = 5;

int result = a++;

System.out.println(result);
System.out.println(a);

Output:

5
6

The current value is used first, and then a is increased.

Pre-increment:

int a = 5;

int result = ++a;

System.out.println(result);
System.out.println(a);

Output:

6
6

Here, a is increased first, and then its new value is used.

13. Type Casting in Java

Type casting means converting a value from one data type to another.

Java supports two common types of casting.

Widening Casting

Widening casting converts a smaller compatible primitive type into a larger type.

It is generally performed automatically.

Example:

int number = 100;

double result = number;

System.out.println(result);

Output:

100.0

Here, an int value is converted into a double.

Narrowing Casting

Narrowing casting converts a value into a smaller or less precise compatible type.

It generally requires explicit casting.

Example:

double price = 99.99;

int result = (int) price;

System.out.println(result);

Output:

99

The decimal part is removed.

Important: Narrowing can cause data loss or overflow, so use it carefully.

14. Practical Java Program: Student Information

Now let's create a practical Java program using variables and data types.

In this example, we will store student information and display it.

public class Main {

    public static void main(String[] args) {

        String name = "Rahul";
        int age = 20;
        double marks = 85.5;
        char grade = 'A';
        boolean isPassed = true;

        System.out.println("Student Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + marks);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + isPassed);

    }
}

Output:

Student Name: Rahul
Age: 20
Marks: 85.5
Grade: A
Passed: true

Understanding This Program

First, we create variables to store student information.

The String variable stores the student's name.

The int variable stores the age.

The double variable stores decimal marks.

The char variable stores a single grade character.

The boolean variable stores whether the student has passed.

Finally, System.out.println() displays all the information.

This example demonstrates how different data types can be used together in one program.

15. Practical Android Example: Displaying Variable Values

Now let's understand how Java variables can be used inside an Android application.

Open your Android Studio project and create a TextView in your XML layout.

<TextView
    android:id="@+id/tvResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"/>

Now open MainActivity.java.

Add the following code inside the onCreate() method after setContentView():

TextView tvResult = findViewById(R.id.tvResult);

String studentName = "Rahul";
int studentAge = 20;
double studentMarks = 85.5;

String result = "Name: " + studentName
        + "\nAge: " + studentAge
        + "\nMarks: " + studentMarks;

tvResult.setText(result);

Also add this import if Android Studio has not added it automatically:

import android.widget.TextView;

When you run the application, the TextView will display:

Name: Rahul
Age: 20
Marks: 85.5

Understanding the Android Code

First, we use findViewById() to access the TextView from XML.

Then we create three variables to store student information.

Next, we combine their values into a String.

Finally, setText() displays the generated text inside the TextView.

This is a simple example of using Java variables in an Android application.

16. Common Mistakes Beginners Should Avoid

Here are some common mistakes when working with Java variables and operators.

Mistake 1: Using an undeclared variable

Incorrect:

number = 10;

Correct:

int number = 10;

You must declare a local variable before using it.

Mistake 2: Assigning an incompatible value

Incorrect:

int age = "Twenty";

Correct:

int age = 20;

The assigned value must be compatible with the variable's data type.

Mistake 3: Using double quotation marks for char

Incorrect:

char grade = "A";

Correct:

char grade = 'A';

A char literal uses single quotation marks.

Mistake 4: Confusing = and ==

int a = 10;

Here, = assigns a value.

System.out.println(a == 10);

Here, == compares two values.

Mistake 5: Forgetting integer division

int result = 5 / 2;

System.out.println(result);

Output:

2

Because both operands are integers, the fractional part is discarded.

If you need a decimal result, use a floating-point operand:

double result = 5.0 / 2;

System.out.println(result);

Output:

2.5

17. Practice Tasks

Now try these exercises yourself.

Task 1: Create a variable named name and store your name in it.

Task 2: Create an integer variable named age and print its value.

Task 3: Create two integer variables and calculate their sum.

Task 4: Create a double variable to store a product price.

Task 5: Create a boolean variable named isLoggedIn.

Task 6: Use relational operators to check whether a number is greater than 50.

Task 7: Use logical AND to check whether a student has marks greater than 40 and attendance greater than 75.

Task 8: Create a Java program that stores student name, age, marks, and grade, then displays all values.

Try to write these programs yourself before checking the examples again.

18. Conclusion

In this tutorial, we learned the basic concepts of Java programming that are important for Android development.

We understood what variables are, how to declare and assign values, and how to use primitive and non-primitive data types.

We also explored arithmetic, assignment, relational, logical, increment, and decrement operators.

Finally, we created practical Java examples and learned how to display variable values inside an Android application using TextView.

These concepts are the foundation of Java programming. Understanding them will make it easier to learn conditions, loops, methods, classes, and objects in upcoming tutorials.

19. Frequently Asked Questions (FAQs)

Q1. What is a variable in Java?

A variable is a named storage location used to store data that can be accessed and modified during program execution.

Q2. How many primitive data types are available in Java?

Java provides eight primitive data types: byte, short, int, long, float, double, char, and boolean.

Q3. What is the difference between int and double?

int stores whole numbers, while double stores decimal numbers with double precision.

Q4. What is the difference between char and String?

char stores a single character, while String stores a sequence of characters.

Q5. What is the difference between = and == in Java?

The = operator assigns a value, while == compares primitive values for equality.

Q6. What is the modulus operator in Java?

The modulus operator (%) returns the remainder after division.

Q7. What are logical operators in Java?

Logical operators combine or reverse boolean conditions. The main operators are AND (&&), OR (||), and NOT (!).

Q8. What is type casting in Java?

Type casting is the process of converting a value from one data type to another compatible data type.

Q9. Why are variables important in Android development?

Variables are used to store application data such as usernames, passwords, prices, marks, user input, and API responses.


20. SEO Details

SEO Title: Java Basics for Android Developers: Variables, Data Types and Operators

Meta Description: Learn Java basics for Android development, including variables, primitive data types, Strings, arithmetic operators, logical operators, and practical Java examples.

Focus Keyword: Java Basics for Android Developers

Related Keywords:

  • Java Variables Tutorial

  • Java Data Types

  • Java Operators Explained

  • Java Programming for Beginners

  • Android Development Using Java

  • Primitive Data Types in Java

  • Java Arithmetic Operators

  • Java Logical Operators

  • Java Practical Programs

Suggested URL Slug: java-basics-variables-data-types-operators

←
Post 5 of 40
→