Ganesh Hegde

Greenhorn
+ Follow
since Apr 09, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ganesh Hegde

Hi Jonathan,

Jonathan, if I get you right, the code that you produced establishes that removing or deleting an element from a collection while iterating with the enhanced for loop throws the ConcurrentModificationException.

But what do you say about what Darryl pointed out earlier in this thread, a change in the state of an object cannot throw an exception irrespective of the type of collection the object is contained in.

Kind regards,

Ganesh
15 years ago
Hi Bill,

Well, actually I was talking about the basic iterator behaviour which the enhanced for loop uses under the hood. And I don't think the iterator's behaviour depends on the way things are added in the collection over which the iteration is being done. For example, I have modified the above code to make use of the HashSet instead of the ArrayList.



This code runs fine and produces the expected output. What I am trying to achieve in the above code is that in spite of changing the states of the objects in the set, the enhanced for loop is working fine. I am changing the state of each object in such a way that the hashcode of the object also changes.
15 years ago
Hi Darryl,

Yes Darryl, I think you are right. I was a bit confused with how people express the rule (which I stated at the onset of this thread). I have heard people say that you 'are not allowed' to modify a list when iterating with the for loop (enhanced for). But I feel it should rather be: you 'cannot' modify a list when iterating with the enhanced for. I feel this subtle difference in wording is important because as you say one is uselessly assigning a local variable 'it' with a newly created object in the code here:


Nothing's going to happen to the original list.

But if you word the rule as: you 'are not allowed' to modify a list while iterating with an enhanced for loop, then we expect some risk (an exception like the 'ConcurrentModificationException') when trying to run the above piece of code. I tried to search out the rule in both the Sun's Java tutorial and the language specification but failed to get one.

Kind regards,

Ganesh
15 years ago
Hi JavaRanch folk,

I have heard (read) many times that one must not
1. modify
2. delete
an element from a list when iterating over it using the enhanced for loop, for example http://jmonkeyengine.com/mark/?p=147. I am clear with the second point of the rule: not to delete an element from a list while iterating over it. But I never came across a suitable example illustrating the 'modify' operation on the list. Specifically I would like to know whether the following use of enhanced for loop is a valid one.



The code above runs fine and prints a list whose every element has the instance variable 'val' set to the integer 2, which is what I expected to happen. But I don't know whether this code is working fine as a special case or is it the norm. I will repeat my question here: Have I transgressed the rule to not to modify a list while iterating over it using the enhanced for loop?

Now my second question is: If the code above doesn't illustrate the wrong practice of modifying a list while iterating over it using the enhanced for loop then what does?

Thanks

Kind regards,

Ganesh

[ May 04, 2008: Message edited by: Ganesh Hegde ]
[ May 04, 2008: Message edited by: Ganesh Hegde ]
15 years ago
Hi Ranchers,

Thanks to all of you for spending your time to think along.

Warm regards,

Gaura
15 years ago
Here's what sparked off this question in me: Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS. Some virtual machines, such as the Java HotSpot virtual machine, perform additional steps at runtime to give your application a performance boost. This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code.
-The Java Tutorials

Well, my question is: Is this the sole reason why JVM was created - for making your programs portable? Can't we take the same approach without having a JVM? Why not have different compilers (that compile source code directly to machine code) for different platforms? (Of course, then we would be faced with two situations
1. we need to distribute the source code to the end users; could be a security issue - but we could encrypt the source code or restrict access to the source code except for compiling purpose.

2. the end users would need to recompile the source code - at least initially; this could delay program start up.)

I feel that the creation of the JVM was more a result of taking the 'seperation of concerns' approach than just to address the portability issue. I presume the designers of Java wanted to give the Java programmer a single interface by providing a compiler whose only job would be to compile the source code to an intermediate bytecode. And they encapsulated the task of making sense of the bytecode in the JVM. Shouldn't we also highlight this design strategy being the cause of the JVM apart from portability?
[ April 22, 2008: Message edited by: Gaura Hari ]
15 years ago
Hi Joseph,

I don't think performance will be such an issue unless your program is very memory intensive. I read something about this here.

Kind regards,

Gaura
Hi Joe,

Your suggestion worked! I was not aware that I had to start a server at the specified port before I could even instantiate a Socket object :-). Here's what I did after I saw your reply.

1. I changed the code of the class ChatServer to this


2. I wrote a small piece of code that would play the client:


Then I opened a terminal and made the jvm execute the bytecode for 'ChatServer' class. I opened another terminal and invoked jvm to execute the bytecode for 'ChatClient' class. And I met with 'Success!'.

Of course the program doesn't do anything useful. But I always start exploring by starting in a small way. I will now build on the above code so that it will do something more useful.

Very kind of you!

Warm regards,

Gaura
Hi Rob,

As far as I know, no server is listening to port 5000. I also tried using other ports like 10008, 2048, 65535. Nothing seems to make it work!

Warm regards,

Gaura
Hi,

I was trying to create a socket object using the following code:


But when i run the compiled bytecode (class file) the machine throws an exception with the following stack trace:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ChatServer.main(ChatServer.java:9)


I am running this on a machine that runs the 'Windows Vista Starter' edition. The jvm version number is 1.5.0_15.

I even tried running the above code after disabling both the windows firewall and Norton Antivirus's firewall. But to no avail.
Try this book "SCJP" by Kathy Sierra and Bert Bates. It will make your preparation an easy one.

Kind Regards,

Gaura
Hey! what is the KnB book? Could you spell out the complete title and the author's name?

Kind regards,

Gaura
15 years ago