Programming Knowledge for Software Testers
Tutorial 6: Data Types in Programming
What is Data Type?
Data Type is a classification of the type of data that a Variable or Constant or Method can hold in computer programming.
Data types specify the different sizes and values that can be stored in the variable.
Ex: Character, Number, Number with decimal values, Boolean / Logical data, String, Date, Currency etc…
I am goin to use three different programming langaues to explain this concept:
1) Java
2) Python
3) VBScript
1. Java
i. Java supports explicit declaration of Data Types
ii. Two types of Data Types in Java
a. Primitive Data Types
b. Non Primitive / Reference Data Types
a) Primitive Data Types
1. Integer Data Types - byte, short, int long
2. Relational Data Types - float, double
3. character Data Type - char
4. Conditional Data Type - boolean
Example:
byte a=100;
short b=500;
int c=12345;
long d=98987878787l;
float e=12.34f;
double f=34567.87678;
char g='y';
boolean h=true;
b) Non Primitive / Reference Data Types
String, Array etc...
iii) Data Type Conversion
2) Python
i) Python supports Implicit declaration of Data Types
ii) Python Data Types: Python has the following data types built-in by default, in these categories:
a) Numbers: int, float, complex
b) Text: str
c) Boolean Type: bool
list
tuple,
set
dictionary etc...
Example 1:
a=100
print (a) #100
print (type(a))#int
b=12.34
print (b)#12.34
print (type(b))#float
c=12j
print(c)#12j
print (type(c))#complex
d="India"
print (d)#India
print (type(d))#str
e=True
print(e)#True
print(type(e))#bool
f = ["mango", "apple", "banana"]
g = ("mango", "apple", "banana")
h = {"mango", "apple", "banana"}
print (type(f))#list
print (type(g))#tuple
print (type(h))#set
Example 2:
a=100
print (a) #100
print (type(a))#int
a=12.34
print (a)#12.34
print (type(a))#float
a=12j
print(a)#12j
print (type(a))#complex
a="India"
print (a)#India
print (type(a))#str
3) VBScript
VBScript doesn’t support Explicit declaration of Data Types.
In VBScript only Data type is Variant, It can hold any type of data, based on usage of data VBScript internally considers Data sub types.
Example:
Dim val
val = "India"
MsgBox val ' India
Msgbox (VarType(val)) '8 for String
val = 100
MsgBox val ' 100
Msgbox (VarType(val)) '2 for Integer
val = 12.34
MsgBox val '12.34
Msgbox (VarType(val)) '5 for Double
val = #10/10/2010#
MsgBox val ' 10-10-2010
Msgbox (VarType(val)) '7 for Date
val = True
MsgBox val ' True
Msgbox (VarType(val)) '11 for Boolean
Note: Using VarType Function we can chheck data type....