DEV Community

Cover image for How to Create Variables using any Language
somos
somos

Posted on

How to Create Variables using any Language

What Are Variables?

Variables can be used to store information that can be manipulated and referenced in a computer program. They provide a way to label data with descriptive names so that our code can be understood better.

Variables can be thought of as containers that hold information. They label and store data that can be used later throughout your program.

Variables in Java

In Java there are different types of variables:

  • String - stores a string of text characters (i.e. "Hello".) String values are surrounded by double quotes
  • int - stores whole numbers (integers) with no decimal point (123 or -123.)
  • float - stores floating decimal point numbers (i.e 19.99 or -19.99.)
  • char - stores single characters (i.e 'a' or 'B'.) Char values are surrounded by single quotes
  • Boolean - stores values with two states: true or false

To create a variable, you must specify the type and assign it a value:

type variables = value;

Java Variables Example

JavaScript Variables

JavaScript gives you seven different data types:

  • undefined
  • null
  • boolean
  • string
  • symbol
  • number
  • object

You can create a variable in JavaScript by putting the keyword var in front of it.

var myName;

You can store a value inside the variable using the assignment operator.

var myName;

myName = "Tom";

But it is mostly common to initialize a variable in the same line you declare it.

var myName = "Tom";

JavaScript Variable Examples

Variables with C

With C and C based languages like C++ and C#, there are different types of variables defined with different keywords.

  • int - Whole numbers, without decimals (i.e 123 or -123)
  • double- stores numbers with floating decimal points (i.e 19.99 or -19.99)
  • char - stores single text characters (i.e 'a' or 'B')
  • bool - stores values with two states true or false

To declare a variable in C, you must specify the type and assign it a value.

type variable = value;

Cpp Variable Examples

Python Variables

Unlike many of the other programming languages, Python has no command for declaring a variable. It is created the moment you first assign a value to it.

Python Variable Examples

A variable name can be short (x and y) or descriptive (age, carName, total_volume.)

Here are some common rules for Python variables:

  • Must start with a letter or the underscore character
  • Cannot start with a number
  • Can only contain alpha numeric characters and underscores (A-Z, 0-9, and _ )
  • Are case-sensitive (age, Age, and AGE are all different variables)

Python Variable Examples

What's Important?

All syntax aside the most important part of learning variables, and any other part of programming is to be sure to understand the logic and methodology behind it. You shouldn't just memorize the syntax as if you were studying for a vocabulary test.

If you understand the logic behind what you are coding, it is easy to figure out how to solve any issue using your programming knowledge.

Top comments (1)

Collapse
 
somos profile image
somos

Thanks! Glad you enjoyed it, I'll look into those languages next!