Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Variable

In Java, a variable is a container or a storage location in a program used to store data values. These values can change during the execution of a program. Each variable in Java has a specific data type, which determines the type of values it can hold. Here's a detailed explanation of variables in Java:

Declaring a Variable:

In Java, you declare a variable by specifying its data type, followed by the variable name. For example:

int age; // Declaring an integer variable named 'age'
double price; // Declaring a double variable named 'price'
String name; // Declaring a String variable named 'name'

Initializing a Variable:

Variables can also be initialized, i.e., given an initial value, at the time of declaration or later in the program:

int age = 25; // Initializing the 'age' variable with the value 25
double price = 19.99; // Initializing the 'price' variable with the value 19.99
String name = "John"; // Initializing the 'name' variable with the value "John"

Variable Types in Java:

  1. Primitive Data Types:

    • int: Used for integers (e.g., 1, 100, -500).

    • double: Used for floating-point numbers (e.g., 3.14, -0.5, 2.0).

    • char: Used for single characters (e.g., 'A', 'b', '$').

    • boolean: Used for true or false values.

    • long: Used for large integers.

    • float: Used for floating-point numbers with smaller precision than double.

    • short: Used for small integers.

    • byte: Used for very small integers.

  2. Reference Data Types:

    • Objects: Variables can also reference objects of various classes in Java.

    • Arrays: Variables can hold arrays of any data type.

Variable Scope:

The scope of a variable defines where in a program the variable can be accessed or modified. In Java, variables can have different scopes:

  • Local Variables: Variables declared inside a method. They are only accessible within that method.

  • Instance Variables (Non-Static Variables): Variables declared within a class but outside any method. They are associated with instances of the class and have a separate copy for each object.

  • Class Variables (Static Variables): Variables declared with the static keyword. They are shared among all instances of a class and are initialized only once, at the start of the execution.

Usage of Variables:

Variables are used for various purposes in Java programming, such as storing user input, performing calculations, controlling loops, and interacting with objects. They are fundamental building blocks in programming, allowing developers to manage and manipulate data within their applications.

Understanding variables and their types is crucial for writing efficient and organized Java code. Proper use of variables ensures that data is stored, manipulated, and accessed accurately throughout the program.

PreviousFirst Java Program : Hello WorldNextData Type

Last updated 1 year ago