Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Syntax in method

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Syntax in method

    I've come accross some new syntax in one my books (new for me that is) that I can't seem to find any explanation for. I understand the code and what it is acheiving, I just can't find any reference to why its writen in this way.

    The following are three seperate peices of code from seperate classes however they all have 'Item' in parenthases BEFORE their respective classes, objects or methods. (ie. Item show = (Item)store.getItem(i);, return (Item)catalog.get(i);, Item temp = (Item)obj; ). Item is a class that defines a single item in a shop with a handfull of arguments (eg. price, qty etc.).

    for (int i = 0; i < store.getSize(); i++) {				
    			Item show = (Item)store.getItem(i);					
    			System.out.println("\nItem ID: " + show.getId() +
    				"\nName: " + show.getName() +
    				"\nRetail Price: $" + show.getRetail() +
    				"\nPrice: " + show.getPrice() +
    				"\nQuantity: " + show.getQuantity());
    		}

    public Item getItem(int i) {
    		return (Item)catalog.get(i);
    	}

    public int compareTo(Object obj) {	
    		Item temp = (Item)obj;	
    		if (this.price < temp.price) {
    			return 1;					
    		} else if (this.price > temp.price) {
    			return -1;
    		} else {
    		return 0;
    		}
    	}

    I have searched the API to try and find why this is but can't find it (yet...). If anybody can shed some light on this or point me in the right direction I would be very grateful.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Syntax in method

    This is called casting. Every class in java extends the class Object. In the last example the method accepts an Object as a parameter. To use the methods and variables of Item it must first be cast to an Item - as long as the passed object is of type Item (if not, a ClassCastException will be thrown)
    See the following link (casting is explained near the bottom)
    Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Syntax in method

    Thanks copeg, thats brilliant.

Similar Threads

  1. Syntax for 3D array enhanced
    By mwr76 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2011, 07:02 PM
  2. do while syntax error problem
    By derekxec in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 1st, 2011, 06:30 PM
  3. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM
  4. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 03:09 PM
  5. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 09:20 AM