How to declare variables in java

In Java, you declare variables by specifying the data type followed by the variable name. Here's how you can declare variables in Java. ```java // Syntax: dataType variableName; // Declaring a variable of type int int age; // Declaring a variable of type double double salary; // Declaring a variable of type String String name; // You can also initialize variables at the time of declaration int x = 10; double pi = 3.14; String message = "Hello, World!"; ``` You can also declare multiple variables of the same type on the same line: ```java // Declaring multiple variables of the same type on the same line int a, b, c; double d, e, f; ``` Remember, when declaring variables, you must specify the data type. This tells Java what kind of data the variable can hold. ```java public class VariableExample { public static void main(String[]args) { //declare and initialize variables int age = 25; double height =5.9; String name = "John Doe"; //print the value of variables System.out.println("Name: "+ name); System.out.println("Age: "+ age); System.out.println("Height: "+ height); //perfom operations with variable int birthYear = 1995; int currentYear = 2024; int calculatedAge = currentYear - birthYear; //print the calculated age System.out.println("Calculated age: "+caluculatedAge) } } ``` join our community to post, comment and vote. thanks­čśë

To comment this publication you need to be logged in.