Basic Variables

Tue 10 July 2018, in category Variable

basic, variable

Here are a set of basic variable declarations. Some languages have primitive data types, while others aren't strongly typed, while other languages still have a mix of both.

Java

byte b = 0;
short s = 0;
int i = 0;
long million = 1000000l;
long million2 = 1_000_000l;  // Underscores improve readability
long million3 = 0xF4240;  //hex
long million4 = 0b1111_0100_0010_0100_0000;  //bytes
float flt = 0.0f;
double dble = 0.0d;
char chr = 'c';
boolean bool = true;
String myString = "Hello, World!";

C#

sbyte sb = 0;
byte b = 0;
short s = 0;
ushort us = 0;
int i = 0;
uint ui = 0;
long million = 1000000l;
long million2 = 0xF4240;  //hex
ulong ul = 0l;
float flt = 0.0f;
double dble = 0.0d;
decimal dec = 0.0m;
char chr = 'c';
bool myBool = true;
string myString = "Hello, World!";

Python

i = 0  #integer
int_hex = 0xF4240  #hex
lng = 0L #long  //Only valid in Python 2
flt = 0.0 #float
complex = 3 + 4j #Complex numbers written as <real part> + <imaginary part>j  (3 + 4i)
my_str_1 = 'Hello, World!'
my_str_2 = "Hello, World!"
isTrue = True
isFalse = False

JavaScript

var i = 0;
var million = 1000000;
var bool = true;
var str = "Hello, World!";
var str2 = 'Hello, World!';
var str3 = '"Hello", World!'

Go

var i int = 0
var i2 = 0
var a, b int = 1, 2 
var i3 int 
var (
    iu uint = 0
    i8 int8 = 0
    i16 int16 = 0
    i32 int32 = 0
)
var b1 = true
var b2 bool = false
var s = "Hello, World!"
var s2 string = "Hello, World!"
var c complex64 = -5 + 12i