Pages

Saturday, November 5, 2022

OOPs.. Is Java a pure Object Oriented Language?

Object Oriented Programming

Object Oriented Programming or OOPs is a programming model which is based on the Objects. 

We probably have to talk about the Class before we talk about an Object. 

In simple, A class can be considered as a blueprint or template which defines attributes (or variables), members and/or functions containing the behavior of the class. A class doesn't hold or contain any data as is, an instance of the class is to be created to hold and/or process the data. 

An Object is an instance of the class, which by default contains the instance of the variables and/or members defined as part of the class. 

Any number of objects can be created from a class and each object occupies separate storage and contains different data. 

E.g.: Class is like a blueprint of a house. Object is a house constructed physically based on the blueprint. Any numbers of houses can be constructed with the same blueprint.  

Is Java a pure Object Oriented Language? 

We can say Java is a Object Oriented Language, Any functionality that is to be run needs to be defined in a class and corresponding methods/functions. 

There is one thing that we need to consider is the use of primitive data types which are not classes. Java classes/objects can directly access primitive data types making which is deviating from calling it as a Pure Object oriented language. 

There are 8 primitive data types (byte, boolean, char, short, int, float, long and double) in java. 

Saturday, October 29, 2022

Strings & String Slicing in Python

Strings


Strings in python are defined by enclosing with either single quotation marks (") or double quotation marks (")

1

2

str_hello = "Hello..."

str_hello = 'Hello...'

It does not make a difference in the way string is stored and/or used in the program based on the way it is declared (i.e., by using single quotation marks or double quotation marks). 

Multi-line strings

Above declaration is applicable when the string is of a single line. If we have a string that has multiple lines, multi-line string can be defined by enclosing with either three single quotation marks (''') and double quotation marks (""").

1

2

3

4

5

6

7

multi_string = """Hello World,

This is a multi-line string.

enclosed in double quotation marks"""

 

multi_string = '''Hello World,

This is a multi-line string.

enclosed in single quotation marks'''

Accessing part of the string by using index

String is similar to an array of characters (there is no character data type in Python) and Index starts from 0.

1

2

3

str_hello = "Hello..."

print(str_hello[0]) # H

print(str_hello[1]) # e


Similarly data can be accessed using negative index (i.e., -1 for the last position) 

1

2

3

str_hello = "Hello..."

print(str_hello[-1]) # .

print(str_hello[-4]) # o


Below are the index positions of a string from above example. 

H e l l o . . .

0 1 2 3 4 5 6 7

-8-7-6-5-4-3-2-1

Looping through the string

We mentioned that the string is like an array of characters and we can access the specific character using it's index. 

So, how to loop through all the characters of a string? There are two ways to achieve this.
  • Retrieve string length and loop through the range and access character by using index.
  • Loop through the string directly. 
So, How to calculate the length of the string? we can use 

1

2

3

string = "Hello World"

for i in range(len(string)):

   print(string[i])


In this example, we are looping through the range and accessing the character by using index. 

1

2

3

string = "Hello World"

for char in string:

   print(char)


In this example, for loop repeats for every character in the string and character can be accessed by the variable specified (char in this example) through out the loop.

What is string slicing? 

By using the index we can only access one character at a time. We can use slicing to retrieve portion of the string. 

For slicing, we need to provide the starting index (included) and ending index (not included in result). 

1

2

3

4

5

6

7

8

'''

Hello World

012345678910

'''

 

string = "Hello World"

print(string[0:5]) # Hello

print(string[2:5]) # llo


In the above example (Line - 7), we are passing index '0' (as the starting position) and index '5' (as the ending position, not included) which returns the part of the string 'Hello'. Value in the index 5 (' ') is not included in the result. 

In Line - 8, we are passing index '2' and '5', which would return the portion of the string from 2 to 4 (both included).

In the cases like Line - 7 where we are retrieving the porting of the string from beginning till the specified index, we won't really need to include index '0'. 

1

2

string = "Hello World"

print(string[:5]) # Hello


Leaving the start index would consider the string from the beginning. 

Similarly if we leave off end index, it would return the string from start index till end. 

1

2

string = "Hello World"

print(string[6:]) # World


So, what happens if we don't mention both start and end index? It returns the full string. 

1

2

string = "Hello World"

print(string[:]) # Hello World


Check if a character/string present in the string

We have seen how to access a character or a portion of string from a string. Now, Can we check if a character or a string is part of the string or not? Yes, we can do this by using 'in' and 'not in'.

1

2

3

string = "Hello World"

print('H' in string) # True

print('H' not in string) # False


In the above example,
  • Line - 2: We are checking if 'H' is in the string, which returns True if present and False if not present. 
  • Line - 3: We are checking if 'H' is not in the string, which returns False if present and True if not present. 
Similarly, we can use a string to check if it is part of another string. 

1

2

3

string = "Hello World"

print('Hello' in string) # True

print('Hello' not in string) # False


It is highly unlikely we use 'in' and 'not in' operator the way we have described (this is only for better understanding). 

Most likely we would be using this in a condition. 

1

2

3

4

5

string = "Hello World"

if 'Hello' in string:

   print('Hello is present in the string')

else:

   print('Hello is not present in the string')


1

2

3

4

5

string = "Hello World"

if 'Hello' not in string:

   print('Hello is not present in the string')

else:

   print('Hello is present in the string')



We have seen how to define a string & multi-line string, accessing a portion of the string (string slicing) and checking if a character or string is part of the string or not. Hope this has been a bit of help in understanding the use of strings in Python.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Thursday, October 27, 2022

View Binding Directory Entries from SQL - IBM i

View Binding Directory Entries


Binding directory is an object that holds the list of modules and/or service programs that may be required for the compilation of ILE programs. 

In other words, Binding directory contains modules and/or service programs whose procedures might be used across different ILE programs and are required for compiling the ILE programs. 

Can these programs be compiled without the use of binding directory? The answer is Yes. If a program is using procedures from one service program (say SRVPGM1), corresponding service program to be added against the parameter BNDSRVPGM when creating the program using CRTPGM. 

So, Why is binding directory needed? In the above scenario, we are only talking about one program using procedure from one service program so it is easy to mention service program name while creating a program. Say if we have many procedures across different service programs (most common), It becomes hard to remember and adding all the service program names while compiling. 

Binding directory provides the option to add all the modules and service programs whose procedures could be used in various programs. And, binding directory name can either be added in the H spec of the RPGLE source or against BNDDIR parameter in the compilation command (CRTPGM, CRTBNDRPG...). 

How do we view the entries in binding directory? Traditionally, DSPBNDDIR (Display Binding Directory) command is used to display the entries in binding directory. 

DSPBNDDIR BNDDIR(<library>/<binding-directory>) 

Same information can now be viewed from the SQL by running a SELECT query on the view BINDING_DIRECTORY_INFO (system name - QSYS2/BNDDIR_INF). 

This view contains the same information as it is displayed using DSPBNDDIR. 
  • BINDING_DIRECTORY_LIBRARY - The library which contains the binding directory.
  • BINDING_DIRECTORY - The binding directory name.
  • ENTRY_LIBRARY - The library containing entry (i.e., service program or module). Can contain the special value *LIBL.
  • ENTRY - The name of the binding directory entry.
  • ENTRY_TYPE - The object type of the binding directory entry. Valid values are *MODULE and *SRVPGM.
  • ENTRY_ACTIVATION - The activation control of the bound service program. Valid values are *DEFER (Activation of the bound service program may be deferred until a function it exports is called) and *IMMED (Activation of the bound service program takes place immediately when the program or service program it is bound to is activated). Contains the null value if ENTRY_TYPE is *MODULE.
  • ENTRY_CREATE_TIMESTAMP - The timestamp when the object was created. Contains the null value if the entry timestamp is not available.
There are two major advantages of using the SQL view (BINDING_DIRECTORY_INFO) over the command DSPBNDDIR. 
  • There is no provision for filter to check for a specific entry in the binding directory in DSPBNDDIR. So one has to page down and find out to see if the specific entry is  present in binding directory. 
  • One has to run command DSPBNDDIR on multiple binding directory to find out all the binding directories in which a specific entry is added. 
Both of these can be easily achieved with the use of SQL view. 

Check for the entries starting with TEST in a specific binding directory. 

SELECT * FROM QSYS2.BINDING_DIRECTORY_INFO
WHERE BINDING_DIRECTORY_LIBRARY = 'TESTLIB'
AND BINDING_DIRECTORY = 'TESTBNDDIR' 
AND ENTRY LIKE 'TEST%';

Check for the binding directory which has entry TESTSRVPGM and entry library TESTLIB. 

SELECT * FROM QSYS2.BINDING_DIRECTORY_INFO
WHERE   ENTRY_LIBRARY = 'TESTLIB'
AND   ENTRY LIKE 'TESTSRVPGM'
AND   ENTRY_TYPE = '*SRVPGM';

Similarly, we can run the query based on the entry activation, entry create timestamp or any other fields as required.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Tuesday, July 26, 2022

Numeric data types in Python

Numeric Data types

There are three different data types to store numeric data. 
  • int
  • float
  • complex

int (integer)

Data type 'int' is used to hold the whole numbers (i.e., no decimal positions). This number can either be positive or negative. 

1

2

3

4

5

6

7

# int - used to hold whole numbers

# Positive numbers

positive_number = 1


# Negative numbers

negative_number = -2


It is possible that an integer data can be stored in a string and we might need to convert it to the int type. We can do that using built-in method int(). 

1

2

3

4

# String to int

number_as_string = "10"

number = int(number_as_string)

 


int() method can also be used to convert floating point numbers to integer. This removes any decimal values and moves whole number to integer variable. 

1

2

3

4

5

6

7

# Float to int

float_value = 10.0

number = int(float_value) # 10

 

float_value = 10.50

number = int(float_value) # 10

 


float (floating point numbers)

Data type 'float' is used to hold the floating point numbers (i.e., numbers with at least one decimal point). This number can either be positive or negative. 

1

2

3

4

5

6

7

# float - used to hold floating point numbers

# positive floating point number

positive_float = 10.123

 

# negative floating point number

negative_float = -10.123

 


We can also specify floating point numbers with 'e' to indicate power of 10. 

1

2

3

4

# float with 'e' to indicate power of 10

float_value = 1.245e10

print(float_value) # 12450000000.0

 


In a similar way, if we have large floating point value and when we print the data, it returns the data with power of 10. 

1

2

3

4

# large floating point value

float_value = 1234567890123456789.012345

print(float_value) # 1.2345678901234568e+18

 


Result of a division operation is always returned as float, even though there are no decimal places in the result. 

1

2

3

result = 10/5

print(result) # 2.0

 


Built-in float() method can be used to convert floating point number from string or int. 

1

2

3

4

5

6

7

8

9

10

# string to float

float_as_string = "12.34"

float_value = float(float_as_string)

print(float_value) # 12.34

 

# int to float

integer_value = 12

float_value = float(integer_value)

print(float_value) # 12.0

 


complex

Data type 'complex' is used to hold imaginary numbers (indicated by 'j'). This can be a combination of real numbers and imaginary number (multiplied by 'j'). value of 'j' is equal to square root of -1. 

1

2

3

4

5

# complex values

imaginary_value = 1j

 

real_and_imaginary = 4 + 1j

 


Like any other numbers, we can do addition or subtraction of complex numbers. 

1

2

3

4

5

6

7

8

# complex values

imaginary_value = 1j

real_and_imaginary = 4 + 1j

 

# Addition and subtraction of complex values

res1 = imaginary_value + real_and_imaginary # (4+2j)

res2 = real_and_imaginary - imaginary_value # (4+0j)

 


Built-in complex() method can be used to convert string, int or float values to complex. 

1

2

3

4

5

6

7

8

9

10

# string, int or float to complex

int_value = 10

complex_value = complex(int_value) # (10+0j)

 

float_value = 10.0

complex_value = complex(float_value) # (10+0j)

 

string = "10+j"

complex_value = complex(string) # (10+1j)

 


We have seen different numeric data types (int, float and complex) and how to convert the data from one type to another numeric data type. Hope this has been helpful in understanding a bit about numeric data types. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

OOPs.. Is Java a pure Object Oriented Language?

Object Oriented Programming Object Oriented Programming or OOPs is a programming model which is based on the Objects.  We probably have to t...