• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with running a program with weird identifier  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Please I will like to employ anyone's insight into helping me resolve this issue. It is an exercise question on K&B OCA/OCAP preparation book (Chapter 1 Question 3). My main issue is that my cmd don't want to run the program becomes the class naming method is phased out starting from java 8. And also, I need to add two argument to the compiled class to get the result I need. And it seems I can't do that with an IDE.

Below is the program. Also I don't seem to understand the working of the code starting from the for loop, I assume if an increment(or decrement) is not specified, we just run for x=0.Please is my assumption right. I thought the result ought to be -A, but the book answer is A. Please help.



And the command line:
java _ - A .

Answer options:
A. -A
B. A.
C. -A.
D. _A.
E. _-A.
F. Compilation fails
G. An exception is thrown at runtime
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 15.27.1 wrote:
It is a compile-time error if a lambda parameter has the name _ (that is, a single underscore character).
The use of the variable name _ in any context is discouraged. Future versions of the Java programming language may reserve this name as a keyword and/or give it special semantics.
link

you should be able to run this program from the command line, using Java 8, just fine. It is still a valid class name, although the compiler will spit out some kind of warning.

It is indeed necessary to specify two parameters - and A, because the main method arguments are used to generate the output. You can do this from the command line easily enough, but it's also quite possible when running a program from inside an IDE.
Eclipse, for instance, has a Run menu with a Run configurations... option, if you create a new Java Application run configuration there should be a tab called Arguments that lets you specify program arguments.
I recommend you steer clear of IDE's fro the time being though, as you are presumably just starting out.

As for the output, look closely at the for loop's increment statement. What value will x have in the very first iteration, and how many iteration's will there be given the value of the termination expression if you use - A as the program's arguments?

FYI, the class and variable names are all completely horrible, but as this is a K&B exercise it's likely meant to familiarize you with what is legal, and proper naming convention is taking a back seat.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emmanuel Ekweanua wrote:Also I don't seem to understand the working of the code starting from the for loop, I assume if an increment(or decrement) is not specified, we just run for x=0.Please is my assumption right.


All three sections of the for-loop are optional. So for example you could write code like the following (but I'm not recommending this):



This for-loop does not have any loop controls at all. It simply loops forever, printing out the word "Hi";

This is equivalent to the following:



Not only is that semantically equivalent, it compiles to exactly the same bytecode with my compiler. So as you can see, there is a very close relationship between for loops and while loops.

Now in your case the code you posted is only missing the final part where you expected to see an incrementor/decrementor. In fact you aren't restricted to just those type of statement there, you could do anything you wanted. However as Jelle has pointed out you need to look very closely at the second part of the for loop to see exactly what is happening to the variable 'x'. That loop does not keep x=0, and it will terminate.
 
Emmanuel Ekweanua
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sir for the feedback.

This is what I understand about the for loop. The __A_V_.length is not explicitly stated or _A_V_ length initialized at the start of the code write-up. So I assume it to be 0 (please I am new to programming, my assumption may be wrong).

The loop will go through 2 times i.e. 0, 1. This is because of x pre-increment (++x). If it is x++, I will assume the loop will iterate just once, 0.

$ += __A_V_[x];

For x first iteration, that is, x=0.

We have $ += __A_V_[0];

Now here is my confusion:

java _ - A . From the argument, - is for 0 iteration(first iteration) while 1 iteration(second iteration), will -, equate to 0 considering x is specified as 0?

Value of - = __A_V_[0] = 0 ???

while for second iteration,

$ =__A_V_[0] + __A_V_[1];

If we say __A_V_[0] = 0

__A_V_[1] = A, Since it is not zero(0) , the argument, A holds (Jeez, I can feel my head banging )

So finally,

$ = _A_V_[1] ..

If I go with the above explanation (with many assumptions), The answer is A.

Please help me pinpoint where my logic fails in the above explanation.

Why I said it was -A,

I took __A_V_[0] = - , Not considering x is zero.

I can't still get cmd to compile the file.. I added a snapshot. It said warning but no error. But still it refuse to run.






file1.JPG
[Thumbnail for file1.JPG]
file1
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emmanuel Ekweanua wrote:Thank you Sir for the feedback.

This is what I understand about the for loop. The __A_V_.length is not explicitly stated or _A_V_ length initialized at the start of the code write-up. So I assume it to be 0 (please I am new to programming, my assumption may be wrong).


Yes, your assumption is wrong. The (very badly named) array __A_V_ is a parameter to the main method. You are right that it is never initialised inside your code, but it is definitely initialized before the main method runs. In fact this array holds an array of Strings, one for each argument you specify on the command line. The array will be initialized by the JVM before it calls your main method.

Emmanuel Ekweanua wrote:
The loop will go through 2 times i.e. 0, 1. This is because of x pre-increment (++x). If it is x++, I will assume the loop will iterate just once, 0.


No, your assumption about the meaning of ++x is not quite correct. Try compiling and running the following program to see what happens when you use ++x:




Emmanuel Ekweanua wrote:$ += __A_V_[x];

For x first iteration, that is, x=0.

We have $ += __A_V_[0];


When you have a look at the program I showed you above, you'll understand that this isn't right. I won't comment on the rest of your post just yet because it was based on your assumption above being correct. I think it's important that you figure out why it isn't correct first.

Emmanuel Ekweanua wrote:I can't still get cmd to compile the file.. I added a snapshot. It said warning but no error. But still it refuse to run.


From looking at your image* I assume that your class named '_' in in a file named Implementation. When you compile a .java file it does not produce a .class file with the same name as the file. It produces 1 or more .class files named after the classes in the file. In most cases the .java file will have a single class that has the same name as the file, but it may have a class with a different name and it may have more than one class. When you run java you need to give it the name of the class that has the main method.

So try running:

java _


* I suggest not posting images of your command line, but rather copying and pasting it into here. That makes it more usable, and keeps it visible when we are on the compose-reply screen.
 
Emmanuel Ekweanua
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mike and Jelle,

I was beginning to get the logic until I ran the code again. (I forgot about the naming of .class file, My bad)..

I ran the code using : java _

[javadoc]Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\emmanuel>cd C:\

C:\>cd code

C:\Code>javac identifier.java
identifier.java:2: warning: '_' used as an identifier
class _ {
^
(use of '_' as an identifier might not be supported in releases after Java SE
8)
1 warning

C:\Code>java _


C:\Code>[/javadoc]

The result was blank.. I could have go further with my reasoning but I want to know why my result is failing to turn up.

I greatly appreciate your efforts
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emmanuel Ekweanua wrote:
The result was blank.. I could have go further with my reasoning but I want to know why my result is failing to turn up.


Read the question from you original post again. Are you running the code like it says to do so in your original post?

Henry
 
Emmanuel Ekweanua
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So sorry, I apologize.. Forgot to add the argument..

[javadoc]Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\emmanuel>cd C:\

C:\>cd code

C:\Code>javac identifier.java
identifier.java:2: warning: '_' used as an identifier
class _ {
^
(use of '_' as an identifier might not be supported in releases after Java SE
8)
1 warning

C:\Code>java _


C:\Code>[/javadoc]
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot to add the command line arguments when I showed you how to run it.

Did you get it to run now?
 
Emmanuel Ekweanua
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope my explanation will fly now ...

I totally forgot that $ was initialized to

String $ = "" ;

On running the code using

$ += __A_V_[x]; which is same as $ = $ + __A_V_[x]

Now this is my assumption;

The First $ is always going to "" no matter the argument it takes. Since it is initialized already. And logically from the equation($ += __A_V_[x];) takes the place of the first argument which is -. In simpler term, the space is blank, so there is no container to hold the value of -.

The second argument will hold because of a visible container, __A_V_[x] . there is a variable to hold it.

I may not be able to explain it well or my reason may even be flawed but I will appreciate your expert insight to this.

Because no matter the argument I insert in the first position, the value is always blank. I believe there is no container to hold any value.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to understand what is going on you need to answer the following questions:

1) What Strings are in the array and what is its length?

2) If x = 0, what does ++x evaluate to?

3) How many times does the for loop run?

4) Which elements of the array are accessed?
 
Emmanuel Ekweanua
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Just coming to understand __A_V_.length is the array length.


1) What Strings are in the array and what is its length? I believe in other words, the arguments in the array. We have two ( - and A), which makes the length two based on the arguments assigned.

2) If x = 0, what does ++x evaluate to? The x++ equates to 1

3) How many times does the for loop run? Based on the condition, x++<__A_V_.length. It looped just once, because if it did the second time, it will negate the for condition of 1(no of loop)<2(length of array)

4) Which elements of the array are accessed? Since the condition states __A_V_.length > 1, the array can only see (assess) the second element and above, of the argument which is A. The first element is -.

Please help evaluate my conclusion.

And I want to appreciate everyone effort so far. I am grateful.

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, you had all four of those just right.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emmanuel Ekweanua wrote:Ok. Just coming to understand __A_V_.length is the array length.
2) If x = 0, what does ++x evaluate to? The x++ equates to 1

3) How many times does the for loop run? Based on the condition, x++<__A_V_.length. It looped just once, because if it did the second time, it will negate the for condition of 1(no of loop)<2(length of array)


Mike has been doing a terrific job of guiding you (have a cow btw), and I don't mean to butt in, but I can't resist, so here goes.
I think a major source of confusion for you may be the pre-increment operator (++x); you don't seem to differentiate between it and the post-increment operator (x++).
The difference between them is subtle, yet important. The pre-increment operator increases the value of the operand (x in this example) by 1 and the value of the expression is that newly incremented value. With the post-increment operator on the other hand, while the operand is also incremented, the value of the expression is equal to the old value of the operand - before it was incremented. These two simple programs highlight the difference - they are both identical except for the increment operator, and they produce different outputs.
In both programs x starts out at 0. The PreIncrement program shows that when the pre-increment operator is used the new value of x - 1 - is immediately available when the expression is evaluated. The PostIncrement program shows that this is not the case when the post-increment operator is used, but the second print statement proves that x has indeed been incremented

Prints:
1
1

Prints:
0
1

The for-loop in the code you posted basically reads as: start out with x at value 0, for each iteration immidiately increment x by 1 and check if that newly incremented value is smaller than the length of array __A_V_, if it is execute the body statement, and at the end of that iteration there's nothing else to do before the next iteration - no increment expression.

If you assume you run the program with arguments "- A", then the length of array __A_V_ will be 2, with value "-" at index 0 and value "A" at index 1. The String $ starts out as an empty value. In the first iteration of the loop x is incremented to 1 during evaluation of the termination expression. As 1 is smaller than 2, the body statement is executed and String $ is appended with value "A", which is the value at index 1 of array __A_V_. The first iteration completes, there's no increment expression, the value of x remains 1. The second iteration starts and the termination expression increments x to 2, and as 2 is not smaller than 2 the loop exits immidiately without running the body statement.

What do you suppose would happend if you changed the pre-increment operator in the loop's termination expression to a post-increment operator? Try figuring it out first, then change the program and run with the same arguments as before and see what happens.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!