The situation insists you to issue more than one command (native or external) through Java Runtime Environment via (Runtime.getRuntime().exec("Cmd")).
Ideally speaking, there has to be a way to separate/distinguish the commands from one another.
Few search results in Google suggested to use a semicolon in between the commands as 'cmd1;cmd2' in exec() method.
I tried the same way but it did NOT work out. During my trial-and-effort, the white space character did the trick :)
Runtime.getRuntime.exec("pwd date");
But it did work in Unix flavours but NOT in windows :( and i am still finding out a way to resolve.
Hope to get it resolved soon..
Cheers...
teknicallyspeaking
discussing some technical aspects covering tools, frameworks, technologies etc (the areas where i m involved and getting experienced) - feel free to discuss!
Friday, September 18, 2009
Thursday, November 06, 2008
java.io.UnsupportedEncodingException: Cp850
Wow.. A nice exception with which I learnt and found a new thing yesterday.
As being asked my one of my colleagues as she was facing this error soon after she launched the application in IE (the browser, Internet Explorer). It was a Struts based application and We digged around all the possibilities by looking at the flow to find out where the encoding has been specified.
I was sure that it has to deal with the encoding settings at the client side (obviously thats what the meaningful Exception says! lol..). The research continued with applicationResources.properties of struts, other application specific properties file, web.xml (to specify the encoding if any) etc., But no luck!!
The entire stack trace is below (fyi).
java.io.UnsupportedEncodingException: Cp850
sun.io.Converters.getConverterClass(Unknown Source)
sun.io.Converters.newConverter(Unknown Source)
sun.io.ByteToCharConverter.getConverter(Unknown Source)
java.lang.StringCoding.decode(Unknown Source)
java.lang.String.(Unknown Source)
java.lang.String.(Unknown Source)
...... [here the application specific code goes -- PRIVACY and CONFIDENTIALITY :)]
Well, she found out the solution with the googling that the "Issue was with the browser (IE 7) " and it was a bug in it. She got the application up and running in IE 6 :) Cool.
Thought of posting it here so that it may be useful for others whoever faces this issue :).
As being asked my one of my colleagues as she was facing this error soon after she launched the application in IE (the browser, Internet Explorer). It was a Struts based application and We digged around all the possibilities by looking at the flow to find out where the encoding has been specified.
I was sure that it has to deal with the encoding settings at the client side (obviously thats what the meaningful Exception says! lol..). The research continued with applicationResources.properties of struts, other application specific properties file, web.xml (to specify the encoding if any) etc., But no luck!!
The entire stack trace is below (fyi).
java.io.UnsupportedEncodingException: Cp850
sun.io.Converters.getConverterClass(Unknown Source)
sun.io.Converters.newConverter(Unknown Source)
sun.io.ByteToCharConverter.getConverter(Unknown Source)
java.lang.StringCoding.decode(Unknown Source)
java.lang.String.
java.lang.String.
...... [here the application specific code goes -- PRIVACY and CONFIDENTIALITY :)]
Well, she found out the solution with the googling that the "Issue was with the browser (IE 7) " and it was a bug in it. She got the application up and running in IE 6 :) Cool.
Thought of posting it here so that it may be useful for others whoever faces this issue :).
Labels:
Exceptions,
Java,
Tips
Monday, September 08, 2008
Editing the shell script in Windows OS
Are you one among them who happened to edit the shell script in Windows OS? You may have various reasons to do that.
You should have got the script working very well in the appropriate linux (or any other flavour) box. But just before doing the final operation (be it checking it into CVS (version control system) or sending to the client waiting at onsite) you might have thought of giving a final/finishing touch like adding some documentation etc.,
Are you asking, "wats up?" or "what's a big deal.. huh?". Wait a minute. All is fine until you execute the script back in the linux box again.
Boooom.. The linux OS spits out an error message saying that "/bin/bash^M: bad interpreter: no such file or directory". What had happened? Wondering..? Surprised? Shocked rather??
Well, here it goes. The way the Windows OS deals with the file saving option is quite different than that of Linux OS. Once you are done with the so called final/finishing touch and when you save the file, some control characters are getting added to the shellscript (.sh file). That's what the culprit is and hence the error when executing it again in linux. The Bash Shell is unable to deal with such special character ('Control M' -- is referred as "^M").
It is assumed that Bash is the default and widely used Shell. The shell being used and the actual error message might vary as per your system's default shell and the way it is customized.
What and Where is the rescue?
There has to be a way. Isn't it? Yes, of course. There are two options.
(1) You can open the file in Linux OS (using your favorite editor like vi, emacs etc.,) and can delete such characters what you find at the end. I am not very sure of this as to what extent those characters are explicitly visible to the end user.
(2) But the other excellent alternative is to use the 'dos2unix' command wherein the usage goes as follows.
'dos2unix' [eg., dos2unix myShellScript.sh]
This do2unix command converts the file in the DOS (for Microsoft environment) to the Unix properitary format. While doing so, it removes the special/control characters and it ultimately cleans the file to be used in Linux versions :).
Note/Word of Caution: Never edit the Linux related files in non-linux OS (Windows)! :)
Hope this helps to solve the tactics and saves you from the critical moment!
You should have got the script working very well in the appropriate linux (or any other flavour) box. But just before doing the final operation (be it checking it into CVS (version control system) or sending to the client waiting at onsite) you might have thought of giving a final/finishing touch like adding some documentation etc.,
Are you asking, "wats up?" or "what's a big deal.. huh?". Wait a minute. All is fine until you execute the script back in the linux box again.
Boooom.. The linux OS spits out an error message saying that "/bin/bash^M: bad interpreter: no such file or directory". What had happened? Wondering..? Surprised? Shocked rather??
Well, here it goes. The way the Windows OS deals with the file saving option is quite different than that of Linux OS. Once you are done with the so called final/finishing touch and when you save the file, some control characters are getting added to the shellscript (.sh file). That's what the culprit is and hence the error when executing it again in linux. The Bash Shell is unable to deal with such special character ('Control M' -- is referred as "^M").
It is assumed that Bash is the default and widely used Shell. The shell being used and the actual error message might vary as per your system's default shell and the way it is customized.
What and Where is the rescue?
There has to be a way. Isn't it? Yes, of course. There are two options.
(1) You can open the file in Linux OS (using your favorite editor like vi, emacs etc.,) and can delete such characters what you find at the end. I am not very sure of this as to what extent those characters are explicitly visible to the end user.
(2) But the other excellent alternative is to use the 'dos2unix' command wherein the usage goes as follows.
'dos2unix
This do2unix command converts the file in the DOS (for Microsoft environment) to the Unix properitary format. While doing so, it removes the special/control characters and it ultimately cleans the file to be used in Linux versions :).
Note/Word of Caution: Never edit the Linux related files in non-linux OS (Windows)! :)
Hope this helps to solve the tactics and saves you from the critical moment!
Labels:
Linux,
OS,
ShellScript,
Tips
Sunday, March 09, 2008
dealing with windows services in java
&&tWell, had been thinking to get the control of windows services in a Java application so that programatically I can manage them (start/stop etc).
My main intention to start off this was basically to check the status of the service whether it is up and running. If not, start it before you accomplish the relevant task. The triggering factor was to check the 'MySQL' service as i sometimes keep certain services as of type 'Manual' instead of 'Automatic' so that these services may not take up considerable amount of time when the Windows OS gets booted up.
At times, when running a J2EE web application used to get the SQLException saying that 'could not obtain a connection', used to panic all of a sudden. Then recollecting our duty as having forgotten to start the particular service :)
As such, i am able to get the service up and bring it down through the application. Yet to get to know the status of a particular service and conditionally run the service :)
First, i remember i can get the external process running inside a Java appliation is via Runtime class (java.lang.Runtime) and its 'exec()' method which takes a String argument which says the OS process to run. A couple of years back i have invoked the IE browser via the same and recollected those stuff.
I tried executing the same as
Runtime runtime = new Runtime().exec("iexplore.exe www.javaranch.com");
The second argument is the actual argument to the application/process to be executed.
It was giving a different error saying that "java.io.IOException: CreateProcess iexplore.exe www.javaranch.com error=2". Had searched a bit in JavaDoc of the Java API in Process, Runtime classes, but really could not get anything.
Beforehand, i wanted to try with some other executables like Notepad etc., to my surprise it was working fine. for example the below command
runtime.exec("Notepad.exe C:\path.txt");
perfectly works fine and opens the path.txt inside the notepad application. Have checked the same with "Calc.exe" as well.
I have concluded to certain extent that the way we invoke this particular executable is errenous. I have tested the same by invoking the executable in the command prompt. It gave the infamous error
'iexplore.exe' is not recognized as an internal or external command,
Found out that the path was missing to this executable. That means, the location to the 'iexplore.exe' was not added into the path. Though i checked the same by adding the location to IE.exe in PATH and it worked fine, but it won't be appropriate to do so just for this testing purpose. So reverted back and tried executing the following:
runtime.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe www.javaranch.com");
What a surprise? It worked fine!
Then i moved on to my required, 'services.msc' stuff in the same way. I got the same error but with different error code even when i gave it with full path :(. For the invocation of
runtime.exec("C:\\WINDOWS\\system32\\services.msc");
the error what i got was, "java.io.IOException: CreateProcess: C:\WINDOWS\system32\services.msc error=193". I am sure that the path to 'System32' was not mandatory at all. This error made me get into google and some good links helped me out in finding a way of what this error 193 means, 'this is not a valid Win32 application'. Also could get some other invocations and out of which what i tried was successful. It is below.
runtime.exec("cmd.exe /c services.msc");
Actually, you have to invoke the Command Prompt (cmd.exe) first and then pass the actual service/exectuable to be invoked as an argument to command prompt. The middle argument '/c' is for 'carrying out the command and terminates". And i thought it would work even if it is not present, but it failed :(. Need a clarification here. :). But it holds good when you type it in 'Run prompt'. The other option to verify it is '/K' which stands for 'carries out the command specified by the string but remains'.
The next question was, how to get the particular service up? I tried by passing the name of the service as an argument as follows.
runtime.exec("cmd.exe /c services.msc MySQL");
but it dint give any fruitful results :(. After a while, though why don't i directly execute the particular executable which the service is made run. Of course, the services is also a shortcut or link pointing to the executable placed elsewhere. Tried it out. it worked but with two drawbacks..
1. The java executable was getting hung. :(
2. Was not having a way to stop the executable rather than killing the process!
Finally when continued searching in google, somewhere i read about the way to start a service. That's through 'NET START '. Here is the link. Got succeeded as well :)
runtime.exec("net start MySQL");
runtime.exec("net stop MySQL");
Obviously these are invoking the MySQL Service in the services.msc only. As of now i am able enuf to do till this phase. Let me complete the rest and post it here..
to set the color of the windows cmd prompt
One among them is, to have the preferred font, foreground and background colors in the Comman Prompt and with a shortcut to the command prompt on the Quick Launch bar.
Just today when i was searching for a help command for another utility (services.msc), got to know about this favorite-and-regular routine of mine.
Windows has a command utility named Color with an argument to set the foreground and background colors for the window being opened.
I bet no one can beat the Microsoft's help/documentation. so for your info, just copy-pasting the information available in the help!
Command : color help
Sets the default console foreground and background colors.
COLOR [attr]
attr Specifies color attribute of console output
Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground. Each digit
can be any of the following values:
0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.
The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.
Example: "COLOR fc" produces light red on bright white.
My preference goes as : "Color 0a" :P
Note: It just sets the colour for the current window being opened! :)
Another Info about the 'DefaultColor' registry value: it is present in the HKEY_USERS/.DEFAULT/Software/Microsoft/CommandProcessor directory in 'regedit' (Registry Editor).
Labels:
Command,
Command Prompt,
OS,
Tips
Monday, January 21, 2008
Getters and Setters for better encapsulation
This is an interesting topic in the beginner's level. Well, at the end of this discussion you would have an answer for these questions.
Let's take you have a Person object and whose size object is what you are trying to access or set.
The plain version of the class would look like as follows:
The following program produces the output as:
By looking at the above code, you don't seem to get anything strange! Yes so do I :).
Let's have a different scenario on the same class.
Now did you see something strange?
If not, look at the two lines carefully.
They are perfectly valid and legal to the compiler and runtime, they are syntactically correct! But logically?
Why these occurred? Because the variables were of public during declaration.
That's why they are allowed outside and any legal value is allowed in the program without any harm.
How we go ahead in preventing this scenario?
Before the setters/getters were in picture or came into existence, the variables were declared public and given outside access without any harms.
In that case,
Just to have a better control on this value being accessed, there came a rescue in the form of Getters and Setters. As the name indicates, Getter is used to get/obtain a value and Setter is to set a value back. They are also called as Accessor and Mutators.
What if you allowed the access through a method and NOT directly?
For which you need to have two things.
Following the same, the code will initially look like
But had you been exposing your code through this way, it would be very easy in a later point when you realize that setting a negative value is a sin! you should NOT allow that, you have an easy way to get rid of it.
Just modify/update the setter method as below :
This way, you really don't break the existing code. As such the users would still continue using your setAge() method to set an age.
Here dealing with the invalid values and the condition to confirm the same may definitely based on the user and requirement. You can either set a default value or throw an exception or just let the user know about invalid value and continue the program normally.
A Quick Summary of encapsulation goes:
Encapsulation reduces coupling between classes, allowing it to make changes to classes without having to change its clients (whoever invokes the methods of this class). This makes maintenance and extension of a software system much easier and less costly.
Remember that for most systems, much more effort is put into maintenance than into initial development, so proper management of code dependencies pays manyfold.
/* 2-arg Constructor */
public Person(int age,String name)
{
this.age = age;
this.name = name;
}
/*Overridden toString() for our Person class*/
public String toString()
{
return "[Person] : age="+this.age+", name="+this.name;
}
- What are Getters and Setters?
- What way they are beneficial?
- Are they the means of achieving Encapsulation?
- Summary of this encapsulation
Let's take you have a Person object and whose size object is what you are trying to access or set.
The plain version of the class would look like as follows:
class Person
{
public int age;
public String name;
public static void main(String[] args)
{
Person obj1 = new Person();
obj1.age = 5;
obj1.name = "Raghavan";
System.out.println("obj1.age="+obj1.age);
System.out.println("obj1.name="+obj1.name);
}
}
The following program produces the output as:
obj1.age=5
obj1.name=Raghavan
By looking at the above code, you don't seem to get anything strange! Yes so do I :).
Let's have a different scenario on the same class.
class Person
{
public int age;
public String name;
public static void main(String[] args)
{
Person obj1 = new Person();
obj1.age = -5;
obj1.name = "";
System.out.println("obj1.age="+obj1.age);
System.out.println("obj1.name="+obj1.name);
}
}
Now did you see something strange?
If not, look at the two lines carefully.
obj1.age = -5;
obj1.name = "";
They are perfectly valid and legal to the compiler and runtime, they are syntactically correct! But logically?
- Can any person have a negative age?
- Can a person's name be empty?
Why these occurred? Because the variables were of public during declaration.
That's why they are allowed outside and any legal value is allowed in the program without any harm.
How we go ahead in preventing this scenario?
Before the setters/getters were in picture or came into existence, the variables were declared public and given outside access without any harms.
In that case,
class Person
{
public int age;
public static void main(String[] args)
{
Person myObj = new Person();
myObj.age = 5;
System.out.println("myObj.age : "+myObj.age);
Person myObj2 = new Person();
/* Check here! */
myObj.age = -2; //Ouch! a syntactically correct but logically incorrect value
System.out.println("myObj2.age : "+myObj2.age);
}
}
Just to have a better control on this value being accessed, there came a rescue in the form of Getters and Setters. As the name indicates, Getter is used to get/obtain a value and Setter is to set a value back. They are also called as Accessor and Mutators.
What if you allowed the access through a method and NOT directly?
For which you need to have two things.
- First "protect" your variable from outside access. Means, declare them to be of private.
- Then provide getter and setter for your private variable. As the variable is pricate, the getter and setters should be public.
Following the same, the code will initially look like
class Person
{
private int age;
/* Getter Method */
public int getAge() {
return this.age;
}
/* Setter Method */
public void setAge(int newAge) {
this.age = newAge;
}
public static void main(String[] args)
{
Person myObj = new Person();
//myObj.age = 5; // can't access directly!
myObj.setAge(5);
System.out.println("myObj.age : "+myObj.getAge());
Person myObj2 = new Person();
/* Check here! */
myObj.SetAge(-2); //Ouch! a syntactically correct but logically incorrect value
System.out.println("myObj2.age : "+myObj2.getAge());
}
}
But had you been exposing your code through this way, it would be very easy in a later point when you realize that setting a negative value is a sin! you should NOT allow that, you have an easy way to get rid of it.
Just modify/update the setter method as below :
...
public void setAge(int newAge) {
/* introducing a new check for Age */
if(newAge <=0){ /*definitely age should NOT be zero! */
System.err.println("Invalid age!");
return;
}
this.age = newAge;
}
This way, you really don't break the existing code. As such the users would still continue using your setAge() method to set an age.
Here dealing with the invalid values and the condition to confirm the same may definitely based on the user and requirement. You can either set a default value or throw an exception or just let the user know about invalid value and continue the program normally.
A Quick Summary of encapsulation goes:
Encapsulation reduces coupling between classes, allowing it to make changes to classes without having to change its clients (whoever invokes the methods of this class). This makes maintenance and extension of a software system much easier and less costly.
Remember that for most systems, much more effort is put into maintenance than into initial development, so proper management of code dependencies pays manyfold.
/* 2-arg Constructor */
public Person(int age,String name)
{
this.age = age;
this.name = name;
}
/*Overridden toString() for our Person class*/
public String toString()
{
return "[Person] : age="+this.age+", name="+this.name;
}
Monday, November 19, 2007
tasklist - cmd in windows similar to ps cmd in linux
The people who have worked in Unix and its flavours, would have definitely come across a command called 'ps' (which stands for 'process status'). As such the 'ps' command gives you the list of processes running and the pid (process id) etc.,
There is a command to achieve the same in Windows OS too. That is 'tasklist'. Just execute in the command prompt. Here it gives the "Image Name", "PID", "Session name", "Session#", "Mem Usage".
Image Name -> the name of the process running
PID -> as you know, the process id (unique id which the OS refers to)
Session name -> it shows 'Console' but have to check what it really means
Session # -> should be a session id and for console it gives '0'.
Mem usage -> the amount of memory each task/process occupies.
There is a command to achieve the same in Windows OS too. That is 'tasklist'. Just execute in the command prompt. Here it gives the "Image Name", "PID", "Session name", "Session#", "Mem Usage".
Image Name -> the name of the process running
PID -> as you know, the process id (unique id which the OS refers to)
Session name -> it shows 'Console' but have to check what it really means
Session # -> should be a session id and for console it gives '0'.
Mem usage -> the amount of memory each task/process occupies.
Here is the sample screenshot of the same.
It looks like the CUI version of the 'task manager' :)
NOTE: It may not work in all versions of Windows. As such i have tested in Windows XP. I believe it must definitely work in 2000 also. Not sure about the previous versions of 98,95 etc., will check and update later.
NOTE: It may not work in all versions of Windows. As such i have tested in Windows XP. I believe it must definitely work in 2000 also. Not sure about the previous versions of 98,95 etc., will check and update later.
Labels:
Command,
Command Prompt,
OS,
Tips,
Windows
Thursday, September 13, 2007
Pass by Reference for object references holds good really?
Pre-requisite: Well, If you want to refreshwith the concepts of "Pass-by-value Vs Pass-by-reference", you may please refer this previous entry in this blog.
With respect to Java, it "passes everything by value". I repeat, "everything" by value only.
Generally, we may think that passing by values will hold good only for primitive types and values but not for the object references.
This everything includes even the references to objects.
Lets say, you have a method which takes an object as a parameter as follows
public void doChange(MyClass myClassObj)
{
//do something..
}
And you call that method from some other piece of code as follows
//calling method
public void method1()
{
MyClass myClassObj = new MyClass();
doChange(myClassObj);
}
The reference variable myClassObj is being assigned to an object of class MyClass in Heap. Just like primitive variables holding bit patterns to represent a value, reference variables also store the bit patterns to reach an object.
Once you call the method doChange() by passing the reference variable "myClassObj" to it, the value of the bit patterns held by "myClassObj" is being copied and passed into the called method doChange(). In the called method doChange(), the copied-and-sent bit pattern is received in the same name as that of the original reference variable "myClassObj". But the compiler treats that as a different one just like primitives.
Lets say the received reference variable as "myClassObjLocal" for easy understanding. In this stage, both the original refernece variable "myClassObj" and the received reference variable inside the doChange() method "myClassObjLocal" (to compiler) point to the same object in Heap.
Now if you change the state of the object being pointed by, it will reflect to both the reference variables because both of them point to the same object in Heap. Whereas,if you change the received reference variable "myClassObjLocal" to point to a new object (reassign a different object), it does NOT reflect back to the original object because the bit pattern to the "myClassObjLocal" alone gets changed!
If you are clear with the above paragraphs, lets go to an example to make it more clear.
Lets take a small example.
class TestObjRef
{
int intValue;
public TestObjRef()
{
intValue = 1;
}
public static void changeStateOfObject(TestObjRef obj)
{
obj.intValue = 2;
}
public static void changeReference(TestObjRef obj)
{
obj = new TestObjRef();
obj.intValue = 9;
}
public static void main(String[] args)
{
TestObjRef obj1 = new TestObjRef();
System.out.println("obj1.intValue (1) = "+obj1.intValue);
changeStateOfObject(obj1);
System.out.println("obj1.intValue (2) = "+obj1.intValue);
changeReference(obj1);
System.out.println("obj1.intValue (3) = "+obj1.intValue);
}
}
Running the above code produces the following output:
Output:
obj1.intValue (1) = 1
obj1.intValue (2) = 2
obj1.intValue (3) = 2
changeStateOfObject Method
This is because, changeStateOfObject() method just changes the value of the variable "intValue" which definitely constitutes the state of object. And as such, both the "obj1" in main() method and "obj" in changeStateOfObject() method point to the same object. Means, they both hold the same bit patterns to reach a single object of TestObjRef class in heap.
That's why the output in the second line shows the changed state of object "2" as the value of "intValue" property.
changeReference() Method
If you look at the changeReference() method, by the time of receiving the argument, both the "obj1" reference variable in main() method and received reference variable "obj" in changeReference() method both hold the same bit patterns to reach the same and single TestObjRef class in Heap.
But inside the method, the received "obj" reference variable is reassigned to a newly created object of TestObjRef class. In this case, only the bit pattern of the "obj" reference variable inside the changeReference() method is changed and the change is NOT reflected back to the originally sent reference variable "obj1".
That's why the third line in the output still shows the value of "intValue" as "2" since the changed value "9" is only reflected in the received local reference variable "obj" in changeReference() method.
Note :
The received argument (object reference variable) is treated local to the method. That means, the scope of the variable is only local to the called method just like primitives and the variable cannot be accessed outside the method.
Labels:
Java,
pass-by-reference,
Pass-by-value
Pass by value Vs Pass by Reference
We all would have read the two terms "pass-by-value" and "pass-by-reference" especially when you deal with methods (which are also called as functions).
Let's just have a quick glance at both the terms with a small example.
The definition of these terms goes as follows.
Pass-by-value: A copy of the value is being passed from the calling method to the called method.
What exactly do you mean by copy of the value? In general, we tend to pass the variable for the value to be passed. In such case, whatever the value the variable holds is being copied and passed across. That means, you are taking a xerox of the o riginal value and pass the xeroxed copy to the called method.
What are the effects?
Since you have only passed the xeroxed copy and NOT the original value, whatever changes you do in the called method, the change is not reflected back to the original variable.
A Sample program for pass-by-value:
void changeValue(int a)
{
int b = ++a;
printf("changeValue -> a, b contain: %d,%d",a,b);
}
int changeAndReturn(int a)
{
int b = ++a;
printf("changeAndReturn -> a,b contain : %d,%d",a,b);
return b;
}
int main()
{
int i = 1;
changeValue(i);
j = i;
printf("\ni and j is : %d,%d\n",i,j);
printf("\ncalling changeAndReturn()..");
changeAndReturn(i);
j = i;
printf("\nNow i and j contain: %d,%d",i,j);
}
Output:
changeValue-> a, b contain: 2 2
i and j is: 1,1
calling changeAndReturn()..
changeAndReturn -> a,b contain: 2 2
Now i and j contain: 1 2
Explanation:
Let's see how exactly the values are dealt. In the main() method, you are calling changeValue() method as
changeValue(i);
At this time, the value of i (which is 1) is being copied (Remember! xeroxed) and the xeroxed copy is what getting passed to the changeValue() method. Inside the changeValue() method, you get (receive) the passed value in the variable name "a". Note: Even if you have received the passed argument as the same name "i" in the function, the compiler would treat both of them differently!
Soon after you take a xerox copy of the value of the variable and use it for calling functions, the connection between the copied value and the original value is cut. There is no relation between those two values and /or variables.
Inside the method changeValue(), we do the following steps:
(i) receive the value passed in the variable whose name is 'a' (implicit)
(ii) declare an int variable named 'b'
(iii) increment the received value in its variable 'a' and assign the incremented value to 'b'
Since because the original value ('i' in main) does NOT hold any relationship with its xeroxcopy ('a' here), the changes you do in the called function (incrementing the value in changeValue()) does NOT reflect back.
That's why in the main after the function call returned, the printf statement prints the value both i and j contain 0. Once the function call is returned, the present focus is in main() function and now we are assigning the value of 'i' to 'j' which is 0 only. This clearly shows that there is NO effect on the variable 'i' even after calling the function changeValue() which does some change to it actually.
Just to have an effect, we have one more method called changeAndReturn() which changes the value and returns it. In the calling function (main()), we get the returned value and assign it to 'j'. That's why the second set of output shows the incremented value '2' for both 'i' and 'j'.
Pass-by-reference: The memory address of the variable is being passed instead of making a copy of the value. This holds good for C, C++ languages (as far as i know as such).
What are the effects?
Since you pass the memory address itself to the function and you obtain the value from the received memory address inside the called function, you get the full control over the value. That means, whatever change you do, it will be reflecting in the original variable in the calling function eventhough you do NOT return either the memory address or value back.
It is because, you have played around with the memory directly instead of values and in both the calling and called functions, the values are obtained and manipulated with respect to memory only. So the change is reflected back.
Let's see a sample program for pass-by-reference:
Sample Program for pass-by-reference:
void changeValue(int *int_ptr)
{
//doing an increment operation on the 'dereferenced' pointer
int b = ++(*int_ptr);
printf("changeValue -> a, b contain: %d,%d",a,b);
}
int main()
{
int i = 1;
int *int_ptr = &i;
printf("\ncalling changeValue()..");
//Note: we are passing the pointer to the function! (the 'address')
changeValue(int_ptr);
//assigning the value of 'i' to 'j'
int j = i;
printf("\ni and j is : %d,%d\n",i,j);
}
Output:
changeValue-> a, b contain: 2 2
calling changeValue()..
i and j is: 2,2
Explanation:
If you carefully look at the implementation and way of calling the function changeValue(), it actually receives a pointer to an integer! that means, we are passing the 'memory address' of an integer variable.
Inside the function, we do the following steps:
(i) receive the address of the integer variable in the name 'int_ptr' (the same name as in the calling function, here main(). But remember! for the compiler, they both are different!)
(ii) we declare an integer variable 'b'
(iii) we dereference the pointer variable ('dereferencing' means obtaining the value present in the location where the pointer is pointing to)
(iv) we increment the value being obtained by the dereferencing operation
Note: here the value is incremented directly in the memory location
(v) and assign the incremented value to the variable 'b'
One important thing is that, we do NOT return any value back to the calling function. But the changes happend in the memory location directly in the called function (changeValue()). Once the control comes back to the calling function (main()), we are still accessing the value in the same memory location through the original variable 'i' and assigning to 'j'.
That's why we see both i and j having the value '2'.
Let's just have a quick glance at both the terms with a small example.
The definition of these terms goes as follows.
Pass-by-value: A copy of the value is being passed from the calling method to the called method.
What exactly do you mean by copy of the value? In general, we tend to pass the variable for the value to be passed. In such case, whatever the value the variable holds is being copied and passed across. That means, you are taking a xerox of the o riginal value and pass the xeroxed copy to the called method.
What are the effects?
Since you have only passed the xeroxed copy and NOT the original value, whatever changes you do in the called method, the change is not reflected back to the original variable.
A Sample program for pass-by-value:
void changeValue(int a)
{
int b = ++a;
printf("changeValue -> a, b contain: %d,%d",a,b);
}
int changeAndReturn(int a)
{
int b = ++a;
printf("changeAndReturn -> a,b contain : %d,%d",a,b);
return b;
}
int main()
{
int i = 1;
changeValue(i);
j = i;
printf("\ni and j is : %d,%d\n",i,j);
printf("\ncalling changeAndReturn()..");
changeAndReturn(i);
j = i;
printf("\nNow i and j contain: %d,%d",i,j);
}
Output:
changeValue-> a, b contain: 2 2
i and j is: 1,1
calling changeAndReturn()..
changeAndReturn -> a,b contain: 2 2
Now i and j contain: 1 2
Explanation:
Let's see how exactly the values are dealt. In the main() method, you are calling changeValue() method as
changeValue(i);
At this time, the value of i (which is 1) is being copied (Remember! xeroxed) and the xeroxed copy is what getting passed to the changeValue() method. Inside the changeValue() method, you get (receive) the passed value in the variable name "a". Note: Even if you have received the passed argument as the same name "i" in the function, the compiler would treat both of them differently!
Soon after you take a xerox copy of the value of the variable and use it for calling functions, the connection between the copied value and the original value is cut. There is no relation between those two values and /or variables.
Inside the method changeValue(), we do the following steps:
(i) receive the value passed in the variable whose name is 'a' (implicit)
(ii) declare an int variable named 'b'
(iii) increment the received value in its variable 'a' and assign the incremented value to 'b'
Since because the original value ('i' in main) does NOT hold any relationship with its xeroxcopy ('a' here), the changes you do in the called function (incrementing the value in changeValue()) does NOT reflect back.
That's why in the main after the function call returned, the printf statement prints the value both i and j contain 0. Once the function call is returned, the present focus is in main() function and now we are assigning the value of 'i' to 'j' which is 0 only. This clearly shows that there is NO effect on the variable 'i' even after calling the function changeValue() which does some change to it actually.
Just to have an effect, we have one more method called changeAndReturn() which changes the value and returns it. In the calling function (main()), we get the returned value and assign it to 'j'. That's why the second set of output shows the incremented value '2' for both 'i' and 'j'.
Pass-by-reference: The memory address of the variable is being passed instead of making a copy of the value. This holds good for C, C++ languages (as far as i know as such).
What are the effects?
Since you pass the memory address itself to the function and you obtain the value from the received memory address inside the called function, you get the full control over the value. That means, whatever change you do, it will be reflecting in the original variable in the calling function eventhough you do NOT return either the memory address or value back.
It is because, you have played around with the memory directly instead of values and in both the calling and called functions, the values are obtained and manipulated with respect to memory only. So the change is reflected back.
Let's see a sample program for pass-by-reference:
Sample Program for pass-by-reference:
void changeValue(int *int_ptr)
{
//doing an increment operation on the 'dereferenced' pointer
int b = ++(*int_ptr);
printf("changeValue -> a, b contain: %d,%d",a,b);
}
int main()
{
int i = 1;
int *int_ptr = &i;
printf("\ncalling changeValue()..");
//Note: we are passing the pointer to the function! (the 'address')
changeValue(int_ptr);
//assigning the value of 'i' to 'j'
int j = i;
printf("\ni and j is : %d,%d\n",i,j);
}
Output:
changeValue-> a, b contain: 2 2
calling changeValue()..
i and j is: 2,2
Explanation:
If you carefully look at the implementation and way of calling the function changeValue(), it actually receives a pointer to an integer! that means, we are passing the 'memory address' of an integer variable.
Inside the function, we do the following steps:
(i) receive the address of the integer variable in the name 'int_ptr' (the same name as in the calling function, here main(). But remember! for the compiler, they both are different!)
(ii) we declare an integer variable 'b'
(iii) we dereference the pointer variable ('dereferencing' means obtaining the value present in the location where the pointer is pointing to)
(iv) we increment the value being obtained by the dereferencing operation
Note: here the value is incremented directly in the memory location
(v) and assign the incremented value to the variable 'b'
One important thing is that, we do NOT return any value back to the calling function. But the changes happend in the memory location directly in the called function (changeValue()). Once the control comes back to the calling function (main()), we are still accessing the value in the same memory location through the original variable 'i' and assigning to 'j'.
That's why we see both i and j having the value '2'.
Labels:
C,
C++,
Command Prompt,
pass-by-reference,
Pass-by-value
Subscribe to:
Posts (Atom)
Blog Archive
Links
About Me
- Raghavan alias Saravanan M
- ஏதோ பிறந்தோம், ஏதோ வாழ்ந்தோம் என்றிருப்பதல்ல வாழ்க்கை! எப்படி வாழ்ந்தோம் என்பதும் ஒரு அங்கம். வாழக் கிடைத்த வாழ்க்கையில், ஒரு சிலருக்காவது வசந்தத்தின் முகவரியை அறிமுகம் செய்தோமேயானால் அதுவே வசீகரத்தின் வனப்பைக் கூட்டும்!