X

We would like to inform you that the domain chat-to.dev, as well as the associated project, is available for purchase. Those interested in negotiating or obtaining more information can contact us at contact@chat-to.dev.

We would like to thank everyone who has followed and supported the project so far.

1712341239

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😉

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Donate
[2026 © Chat-to.dev]