File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain this Assertions problem

 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In the above code what does assert statement do ....???
In order to see how does assert statment exhibit its functianality is something that i dont understand???
 
Swati Pawar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private double calcSpeed(double distance, double time) {
assert distance>=0.0; // Check if distance is grater than or equal to 0.0,
// then continue else throw assertionException
assert time >0.0 : "Time is not a positive value:" +time;
// If time > 0.0 then continue to the next line else throw
// assertionException. It will print "Time is not a positive value:" +
// time with the exception
double speed =distance/time;
assert speed >=0.0; // If speed is greater than or equal to 0.0 then return
// speed else will throw AssertionException
return speed;
}

Assertions are used for debugging the program. Its like if condition. If condition is true it will continue to execute code else will throw Exception.

Whatever I know about assertions, I am telling you from that. Please correct me if I am wrong.
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, you have to enable assertion at runtime using -enableassertions, or -ea, switch. If you don't enable assertions at runtime, then assert statements will be ignored by the JVM. For SCJP, you must know what is the correct use of assertions. Like assertion can be used for validating parameters to private methods but should not be used for validating parameters of public methods. A full list of correct use of assertion is given in K&B book. You can also look at this article to get some idea about this...
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit and Swati I have understood this question
 
akaash singh
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

I am preparing for scjp too. So for learing purpose i tried out sonali's cod .I gave the arguments 12.0 & 0 to the calcSpeed method and ran it without enabling the assertions . I was expecting an exception but the output has me doubting whatever progress i have made in java so far .

Here's what i got :

C:\java>java -ea Speed
Exception in thread "main" java.lang.AssertionError: Time is not a positive valu
e:0.0
at Speed.calcSpeed(Speed.java:12)
at Speed.main(Speed.java:5)

This is ok , because of the assertions . but have a look at the next one

C:\java>java Speed
Speed (km/h):Infinity


someone please explain , what point am i missing . Cause i am panicking now . Shudnt it give an Arithmetic Exception . or is there something wrong with my compiler/jvm.
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you mean line 15 where the argument "12.0" and "0.0" would be effectively dividing by zero.
Yes, at first glance I'd expect to get some ArithemticException too. After all, that's what my calculator does.
I'm studying for the SCJP too, so we can panic together.

Right then, no "Divide by 0!" squeal from the JVM. Why?
Well, if you changed line 15 to use integers you do get an ArithmeticException. Phew! Must be something special about doubles then or more correctly, floating point arithmetic versus integer arithmetic.
Integers cannot store/represent infinity. floating points can represent positive and negative infinity.
So if you do divide by zero with integers, there's no way to return the answer; exception!
But with floating points infinity can be returned. That behaviour would appear to be the IEEE standard for floating point divide by zero and that's what Java uses.
There are probably deep mathematical reasons for treating integers and float points in such a different way.

I had to Google most of that, and I pray to the Java gods that that kind of horrible nastiness is not on the exam!
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also preparing for SCJP...If this question comes in exam ..............
Really ...dont know what to say as the right option in doubles..............kind of afraid
 
akaash singh
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey jason
in retrospect of the a++ and b++ trouble and now this, i think had better stop tinkering with code now , lest it frightens us off into skipping the test altogether .. dont know wat to say ..

 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is a basic thing and you must know about such things for the exam. Java follows IEEE floating point standards and that defines that a positive number divided by zero is positive infinity. You must have some basic knowledge about NaN and Infinity values for the exam. BTW, that's why everyone says that you should code a lot, you find strange things while you practice ...
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

I went back to Sierra&Bates and looked for this "topic". Other than mentioning the "strictfp" keyword I could not find any information on the particulars of floating point versus integer arithmetic. Maybe I've not have enough coffee this morning....

If this information is required for the exam could you possibly give provide a link or a reference to where it is in Sierra&Bates so I can read about it? Thanks.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,

See this link and you will get the answer for fJava follows IEEE floating point standards and Infinty as the correct output.....

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.4

This is for Java Language specification...and it has complete details...for the topic you are looking for.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic