java why is long value in if statement validated wrong - java

In the below code the if statement evaluates to true but i can see its false.
Clearly there is a logic here to explain this but i cannot remember it.
2 movies showing the phenomenology:
http://www.youtube.com/watch?v=DFcRfPErfik
http://www.youtube.com/watch?v=NEC04-kLQBE
The line "holderCompleated.textInfo.setT..." will run even do diffHours==1
look at the picture, diffHours=1 and Expression windows show false
a summery for this question could be:
(dont be affraid to ask even the the silliest question :))
here's a picture when i enter the if statment

something strange going on. im adding the Log.d(TAG,... inside the if statement and the debugger is stepping over it. Also have a Log.d(TAG,.. just before the if statement and that one is ok.
Maybe the clean-up didn't work and you're running some old code. Did you try to restart Eclipse? And then maybe delete and re-import some projects?
This would also explain why you see the debugger entering the if statement: it isn't! The running code is just not synchronized with the source code.

Related

Eclipse Breakpoint System.out

typically I set a breakpoint in my Java application when I want to observe the run.
However sometimes I just want to know if a method is called or not. Therefore a breakpoint does not help me, and I insert a "systrace" statement
System.out.println("method signature");
I thought it would be a nice feature If I could set a breakpoint and when the breakpoint is reached to just print out the systrace message and continue the run.
Do you know if this is possible?
You have to make it a conditional breakpoint with a following condition:
System.out.printf(Thread.currentThread().getStackTrace()[1].getMethodName() + "\n") == null
Works fine in my Eclipse. I'm using printf to make printing code evaluate to boolean. Not sure if there's a way to automate inserting this code into a breakpoint.

Eclipse Won't Start Program

So I coded up a simple test program for an algorithm in Eclipse 3.7.2. When I went to go run it, I was met with some gray bar that appeared on the top portion of the console. It reads: <terminated> test[Java Application]C:\Program Files\Java\jre6\bin\javaw.exe. Anyone know what's causing this?
Your program is executing properly. The problem is in the logic of your program which is never allowing it to reach the print statement.
When you do
if(s==original)
return;
This statement s==original always returns true in your case since this operator will compare the two objects.
You need to rethink your logic here and google about what == operator does in Java.
Also, on another note, instead of using an array String[] s = {"a","b","c"}, why dont you use a string String s = "abc";
You Need to go to Control Panel > Windows Firewall and select Restore defaults but if you don't want to lose other settings, you could try Advanced Settings and find eclipse in there.

Inspect function of eclipse tricks me like a dummy, why does it seem to be that tricky while debugging?

Eclipse is awesome for writing java programs but today I find that it's awesome to trick new coders like me. #_#
I write a snippet as following,
public static void main(String[] args) {
for(int i=0;i<3;i++){
System.out.println("i = " + i);
}
}
then I add a breakpoint at the line of "System.out.print....", click "Debug" button, eclipse goes to the "Debug" perspective and the line of breakpoint is highlighted, then I move the cursor over variable "i", its value is "0" as expected.
And then, I select the "i++" and click "Inspect"(or press "Ctrl+Shift+I") once, I move the cursor over variable "i", its value changed to "1". I repeat "Inspect" again, the value of i changed to "2"......(its value will add by 1 every time I clicked the "Inspect")!!
Why does this happend!? I ONLY want to watch the value of "i" for debug propuse, DO NOT want to really change its value until I step into next statement. I think that "Inspect", as well as "Display" are only for viewing the variable/expression, they should not impact the value, but in this case, it doesn't work as I expect to.
Could anyone tell me what went wrong?
My eclipse version info:
Version: Indigo Service Release 2
Build id: 20120216-1857
If you inspect an expression, eclipse has to execute that expression so you can get the value. Therefore, if you inspect i++, eclipse adds one to i.
Think about it this way: If instead of i++, you inspected myFunction(i), would you expect eclipse to execute the function "myFunction" to get the value? It's the same with i++.
If you are concerned about displaying/showing values while debugging and do you want to be sure not affecting the value, you should select the variable or expression and use the "Watch" option.
This will track the variable/expression value without executing, just updating the new value each time this is run. I think is the most secure way.
As Pablo mentioned, it has to evaluate the code in order to tell you what value it returns. You could instead put a watch on "i+1" and that would give you the value you want without the side effect you don't want.
Basically, you need to be aware of any side effects of anything you launch, whether from the "main" code or from the debugging tools. As Erhannis mentioned, this is very useful at times for modifying values while debugging your code. (For example, you can verify that a tweak/fix is indeed helpful before actually tweaking your code.)
You were expecting "an isolate area" but this would be extremely hard to do, especially in an object-oriented context where many objects are linked to many other objects. Running the whole thing in parallel might sometimes work, but you'd lose the tweaking ability above. And in any context, you'd run into tons of problems with fighting over resources; e.g. both copies trying to read/write a particular file such as a log file. Also, the two execution paths could diverge, leading to incorrect/misleading watch values.
So, preventing such side effects is not really a feasible option here and would rarely be useful anyway. Just expect that the watches can both reflect and affect the code execution.

Search and replace if statements without braces to include braces

I've been recently using sonar for code analysis. When I go thorough violation drilldown, I found many java files with if statement defined without braces (thousands of places). Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.
I'm currently using intelliJ.
Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.
I don't know if there is a tool to do this automatically. (There probably is ...) But assuming that such a tool exists, I'm not convinced it would be the right approach.
Step back for a moment and consider why the code analysis has reported this as a problem. A lot of people (like #pst and me) think that the braces always should be there, even though various style guides don't insist on this. There is a good reason for this ... apart from "it looks ugly". Consider these example code snippets:
if (i == 1)
i++;
doSomething();
while (i < 1)
i++;
doSomething();
If you don't read those carefully, your eyes will trick you into thinking that doSomething(); is called conditionally ... due to the incorrect indentation. And it happens.
(Aside: labelling someone as "inept" for misreading that code is not helpful. If you are desperately trying to fix a show-stopper bug and you've been working for 14 hours straight, then you are likely to miss this kind of thing. And not because you are inept. Once you've been in that situation a couple of times, the lesson sinks in ...)
OK, now suppose that you run an automatic tool to add the braces. What you will get is this:
if (i == 1) {
i++;
}
doSomething();
while (i < 1) {
i++;
}
doSomething();
It means exactly the same thing as the original code. BUT ... what if the original code was actually a bug? What if the programmer intended the doSomething() calls to be conditional?
In short, by adding the braces automatically, we've obscured the original programmer's intention, and made these bug(s) harder to track down.
Bottom line - I think it would be prudent to manually review each of these occurrences ... rather than just "fixing" them automatically. Indeed, I'd argue that if you don't have the time or patience to review them manually, it would be better to leave the code alone. It would be better to turn off the warning ... IMO.
I can reformat the code to make intelliJ do the thing for me, but I need to go through all the files and reformat it. Yes, I might turned off the check but wondering if there is a good tool to do the task. I've good set of tests to check whether it introduce bugs during the process.
If you are sure that you have some ways to test that you will not introduce bugs then use the IntelliJ Reformat Code feature.
Just make sure that the Code Style you have in IntelliJ is in line with your company's policies. Otherwise you will force your style on everybody else too.
To force braces just mark them as Always on the Wrapping and Braces tag in the Code Style settings dialog in IntelliJ.
Mark the source folder in the project view and press Ctrl-Alt-L. A dialog pops up and there you can chose All files in directory <...>.
Then press Run and see what happens. If you are not satisfied then just revert from your VCS.
IntelliJ IDEA has an inspection for this as well, and it has a quick fix to automatically add the braces.
Invoke Analyze | Run Inspection by Name and enter inspection name Control flow statement without braces. Run it on the desired part of your project. In the inspection results you can apply the quick fix Add braces to statement.
Note that this inspection will also report control flow statement other than if, like for example while statements. Invoking the quick fix will also add braces to those statements.

How can I debug my code?

I asked about some non-working code I had at Code works extremely slowly and does not print output.
As you can see in the answer to that question, deks debugged my code and also provided the output of the fib() function. How could I do that by myself? I really know very little about debugging.
To debug, get yourself and IDE like NetBeans or Eclipse. Then instead of clicking run, click on line number then click debug. When the execution gets to this point, it stops, and you can examine the variables that are currently visible. To make sure they have values they should have. For example, if n should be between 1 and 10, and it is 100, then you know something is wrong. Fine where you set n, and add more breakpoints. These are the points were the execution will stop. You can also click step over, to execute the next line, then stop, or step into, which will execute the next line, except it will step into any functions that you used.

Categories

Resources