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 5 of 5

Thread: I am a Java newbie!! Please Help me!!

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am a Java newbie!! Please Help me!!

    Hi~ I am new here
    I was just following a tutorial from a book
    and I found something that I can't understand

    Here's my source code:

    public class Airplane2 {
    private String nameOfAirp = "C10111";
    private int fuel = 10000;
    private int goPerL = 10; //Km/Liter

    public Airplane2(String nameOfAirp, int fuel, int goPerL){
    this.nameOfAirp = nameOfAirp;
    setFuel(fuel);
    this.goPerL = goPerL;
    }

    public Airplane2(String nameOfAirp, int fuel){
    this(nameOfAirp, fuel, 10);
    }

    public Airplane2(String nameOfAirp){
    this(nameOfAirp, 10000, 10);
    }

    public Airplane2(){
    this("C10111", 10000, 10);
    }

    public void setFuel(int fuel){
    if(fuel < 1000){
    System.out.println("Lack of fuel!!");
    this.fuel = 0;
    return;
    }
    this.fuel = fuel;
    }

    public int getFuel(){return fuel;}
    public int getGoPerL(){return goPerL;}
    public String getNameOfAirp(){return nameOfAirp;}

    public String toString(){
    String s = "";
    if(fuel > 999){
    s = "Name of the plane: " + nameOfAirp;
    s += " Flight distance: " + goPerL*fuel;
    }
    else{
    s = "This plane cannot depart!!";
    }
    return s;
    }
    }

    public class Airplane2Main {
    public static void main(String[] args){
    Airplane2 air948 = new Airplane2("Eagle Five", 6000, 11);
    System.out.println(air948.toString());
    Airplane2 air947 = new Airplane2("Center Five");
    System.out.println(air947);
    Airplane2 air949 = new Airplane2();
    System.out.println(air949);
    }
    }


    I am curious about the texts.
    I thought there should be air947.toString and air949.toString like air948.toString
    but there was no compile error.
    Why is that???


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: I am a Java newbie!! Please Help me!!

    In your Airplane2 class you have a toString() method so when you try to print the object of that class then it automatically going to call the to string method for that object. So if you have toString() in your cass it doesn't matter that whether you print air948.toString() or just 948.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a Java newbie!! Please Help me!!

    Quote Originally Posted by aprabhat View Post
    In your Airplane2 class you have a toString() method so when you try to print the object of that class then it automatically going to call the to string method for that object. So if you have toString() in your cass it doesn't matter that whether you print air948.toString() or just 948.
    But why not other methods like getFuel(), getPerL(), getNameOfAirp()??
    Is there any rule for automatically activating only toString() method??

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: I am a Java newbie!! Please Help me!!

    you can say that it's a kind of rule.
    Implementing toString() method in java is done by overriding the Object’s toString() method. The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object. In your overridden method you build a string that contain the plane details.
    So if you remove the toString() method from your class then air948.toString() will going to call the toString() of Object class that is the super class for all the classes and print the string representation of your object.
    In java most of the classes like Integer, Float etc has override the toString() of Object. For more details on toString() read class hierarchy and their method implementation from "JavaDocs".

  5. The Following User Says Thank You to aprabhat For This Useful Post:

    GregBrannon (December 31st, 2013)

  6. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: I am a Java newbie!! Please Help me!!

    Quote Originally Posted by swkang7229 View Post
    Is there any rule for automatically activating only toString() method??
    For the code:
    System.out.println(air949);
    the compiler chooses the void println(Object) method in java.io.PrintStream. And this method firstly invokes toString() on the object to get a string to print.

    Again, the invocation of toString() is "implicit" when you use string concatenation.

    String s = "obj:" + air949;

    This is translated by the compiler in:

    String s = new StringBuffer().append("obj:").append(air949).toString();

    and the last append choosen is the append(Object) which in turn invokes toString() on the object.

    That's all.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. java newbie
    By imjham in forum Member Introductions
    Replies: 1
    Last Post: June 26th, 2013, 01:57 PM
  2. I'm a newbie in Java
    By __Black_Wolf__ in forum Member Introductions
    Replies: 1
    Last Post: July 24th, 2012, 05:57 PM
  3. ..java newbie..^_^
    By dord_zen in forum Member Introductions
    Replies: 1
    Last Post: May 29th, 2012, 05:16 PM
  4. hello~ i am newbie here and to java
    By puyunk in forum Member Introductions
    Replies: 1
    Last Post: March 4th, 2011, 06:57 AM
  5. java newbie help..
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 30th, 2010, 05:54 AM