system.out.println()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hoggy
    New Member
    • Jan 2008
    • 9

    #1

    system.out.println()

    hello
    i am just a beginner of java but iwant know if i can declare a variable of any datatype and output its value simaltaneously using sytem.out.print ln()
    if no ihave another doubt
    how this is possible
    for(int i=0;i<10;i++);
    please tell me
    hanks in advance
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Assuming you're working with primitives (int, double, boolean, etc), this is as simple as putting the value in System.out.prin tln().

    [CODE=java]
    System.out.prin tln(4); //Should print 4
    [/CODE]

    For objects, if the toString() is defined, you can call an unnamed constructor in System.out.prin tln() and print that way.

    [CODE=java]
    System.out.prin tln(new Integer(4)); //Prints 4
    [/CODE]

    As to your loop question, that works because the for loop is a builtin Java construct that works with that syntax. I'm not sure how else to explain this to you...

    Comment

    • hoggy
      New Member
      • Jan 2008
      • 9

      #3
      thanks
      but
      can i write
      system.out.prin tln(int num=10 +num)?
      num is not declared anywhere before this statement in the program

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by hoggy
        hello
        i am just a beginner of Java but i want know if i can declare a variable of any datatype and output its value using Sytem.out.print ln()
        Tip: bookmark the API and learn to look things up there: http://java.sun.com/javase/6/docs/api/

        Using it you would see that System.out is of type System.out.Prin tStream
        And that println is overridden to take a boolean, car, char[], double, float, int, long, String or Object. So pass it anything, but realize that if you are using the Object version and passing it a non-null reference, it will use the object toString method, which may or may not be what you want.

        More generally, why not just try it and see what happens? It's faster that posting a question in the forum.

        Originally posted by hoggy
        how this is possible
        for(int i=0;i<10;i++);
        please tell me
        hanks in advance
        I don't understand your question. Are you asking why that is legal syntax? It's beacuse of the empty statement.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by hoggy
          thanks
          but
          can i write
          system.out.prin tln(int num=10 +num)?
          The best way to find out is to try it and see.

          Comment

          • hoggy
            New Member
            • Jan 2008
            • 9

            #6
            have u tried the print statement before if not please try it and tell me please dont mind
            and about the loop statement
            if i write
            for(int i=0;i<10;i++)
            {
            sytem.out.print ln("i is" +i);
            }
            it is possible in c++ bcoz there is no restriction that variables must be daeclared all at once it follows bottom to top approach then my line
            system.out.prin tln must be right please tellme
            .good night

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by hoggy
              have u tried the print statement before if not please try it and tell me please dont mind
              and about the loop statement
              if i write
              for(int i=0;i<10;i++)
              {
              sytem.out.print ln("i is" +i);
              }
              it is possible in c++ bcoz there is no restriction that variables must be daeclared all at once it follows bottom to top approach then my line
              system.out.prin tln must be right please tellme
              .good night
              Don't post "can I do this" posts. Just try it and see. If you don't have access to a compiler at the moment, then wait until you can compile it.

              Comment

              Working...