Contact Us Sitemap
Main Menu
Home
Who Are You Like?
- - - - - - -
Books
Movies
Music
Restaurants
Games
- - - - - - -
Hiking
Articles
More Articles
Blog
- - - - - - -
Java
PHP
PSP
Joomla!
CafePress Designs
- - - - - - -
Free Downloads
Web Links
Galleries
Paypal Donate
Subscribe to RSS
RSS Feed
Who's Online
Statistics
Visitors: 767475
Login Form





Lost Password?


Hwy777.com
Blog Directory & Search engine
Home arrow Java arrow Studying for the Java Programmer certification

Studying for the Java Programmer certification Print
Written by Mike Noel   
Thursday, 11 August 2005
Page 4 of 4

Chapter 10: Fundamental Classes

Math.ceil() and Math.floor() return doubles

The Math.ceil() and Math.floor() methods both do types of rounding. The first method returns the smallest value that is greater than or equal to the argument and is equal to a mathematical integer. The second method finds the integer value that is less than or equal to the argument. Since these methods return mathematical integer values it is tempting to think that the returned data type is an integer. It's not. These methods are defined to return a double (the same type as the argument). One advantage of having these methods return double is that is makes is easier to use these methods in expressions without forcing type conversions. For example, consider the following code fragment:
  double x = 3.5;
  double y = 2.0;

  double z = Math.floor(x) / y;
Since the floor() method returns a double value there is no problem with this code. The variable z will end up with the value 1.5. If floor() returned an int value this code would cause a compilation error. The int value for one of the operands of the / operation would force the entire expression to be an int. When trying to assign this value to the variable z the compiler would throw and error. To fix it an explicit cast would be needed but then the final value for z would be 1 instead of 1.5.

Interned Strings

In Java string values are handled by the String class. String literals in code (charactes enclosed in double quotes) are converted to a String object before used. All string literals in a program with the same value are handled by a single String object. This is called interning. This means that all string literals with the same value (almost) will share the same object reference. This can lead to some subtle errors. Consider the following code:
public class Main {

    static public void test(String s1,String s2) {
	if (s1 == s2)
	    System.out.println("Equal");
	else
	    System.out.println("Not Equal");
    }
    static public void main(String args[]) {
	String s1 = "my string";
	String s2 = "my string";
	String s3 = new String("my string");
	
	test(s1,s2);
	test(s2,s3);
	test(s1,s3);
    }
}
All three String references refer to the same value, "my string". In the case of s1 and s2 they are both initialized to the same String object since they are both initialized with string literals. The third String reference, s3, however, is a new object due to the new operator. When this program is run the result is:
Equal
Not Equal
Not Equal
String interning can lead to the false assumption that using == will be adequate for comparing Strings. While this will work if all of your Strings are initialized from string literals, in the general case it won't always work. It is best to use the equals() String method to check for equality.

Chapter 11: Collections and Maps

This chapter covers the standard collections (lists, sets, and maps) that come with the Java core libraries.

Building collections from other collections

All of the built-in collections have constructors that allow a new collection object to be created using an instance of a different collection object. That is, a new HashMap can be created using an ArrayList in the constructor. This interopability is handy for converting between different collection types. Since some collections allow duplicates and other don't it is possible to attempt to create a restricted collection (such as a Set) starting with a non-restricted collection (such as a List). The following code illustrates this:
import java.util.ArrayList;
import java.util.HashSet;

public class Main {

    public static void main(String args[]) {

	ArrayList list = new ArrayList();
	list.add("apple");
	list.add("apple");
	HashSet hs = new HashSet(list);
    }
}
In this case the restricted collection ignores the duplicate element. No errors or exceptions are generated.
Comments
Add NewSearchRSS
Anonymous IP:125.23.120.20 | 2006-10-10 09:09:43
Anonymous IP:125.23.120.20 | 2006-10-10 09:11:39
deepz - Wrong info IP:202.142.69.15 | 2006-11-21 09:34:58
See the last topic of first page which talks about operators. The second one
raazju - Help me Registered | 2007-05-16 01:25:53
good and thank you
JavaStudent - New Edition is already out Registered | 2007-05-18 13:12:57
Thanks for quick overview. The new edition of same book is already out. Check out Khalid Mughals site
Only registered users can write comments!


Last Updated ( Tuesday, 11 July 2006 )
 

Copyright 2004 - 2008 Mike Noel. All rights reserved.
This Site is powered by Joomla!.