aspose file tools*
The moose likes Beginning Java and the fly likes System.out.println confusion with the quotation marks and + sign Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.println confusion with the quotation marks and + sign" Watch "System.out.println confusion with the quotation marks and + sign" New topic
Author

System.out.println confusion with the quotation marks and + sign

Kwasi Roberts
Greenhorn

Joined: May 28, 2011
Posts: 4
On the System.out.println line of code I get confused with the quotation marks and + sign and how to properly use a single quotation or double quotation. Can someone please explain when and how to use it properly and what does the code mean?
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

Hi Kwasi,

Using one System.out.println or print we can print multiple Strings and primitive types.
As a example

the output : string1-string2--1-2--c--3-4.

You can see in above example I have set to print multiple data types in a single print method on line 6. And I have used + to connect them each other. Therefor we can use + to connect various data types like strings and primitives to a single string.
And You can see in above example I have used '-' and "--" to define dashes. By using single quotations we can add only single characters.
As I use '-' represent single dash in above example. And if we try to define multiple symbols and characters like '--' in a single quotation it will be a reason to a complier error.
So to define multiple characters we have to use double quotations. Like I used in the example "--".


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1


luck, db
There are no new questions, but there may be new answers.
David Freitag
Greenhorn

Joined: May 28, 2011
Posts: 13

The System.out.println part of java is used to print strings to a console.

If you are working with strings you will all ways be using double quotes, or ""s.

The '+' sign is used to concatenate strings... thats just a big word meaning, to add them together.
Example:

will print out HelloWorld.
if you are going to print out a bunch of variables concatenated together to form one long string, you need to start with a string..
Example:

this will output:
Output: 25, 32, 50
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Actually it's not a function and it's not really System.out.println();

There is the System class, which represents the computer you are using. It has a static field called out, which is a PrintStream. So when you write System.out, you get your hands on out which you can find a bit more about here.

If you look up System.out, as I said, you find it is a PrintStream, and that link tells you it is println methods. You can click on those and find out more details. Alternatively, you can click one of the underlined PrintStream links, and you find lots more about it, and other methods you can use. So it's not simply System.out.println(). It's three things:
  • get the System class.
  • Get its "out" field.
  • Get that to use its "println" method.


  • . . . and welcome to the Ranch
    David Freitag
    Greenhorn

    Joined: May 28, 2011
    Posts: 13

    I didn't mean function as in a method or constructor, i just meant that it is a part of java..

    And thanks ;]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    Methods and constructors aren't functions either. A function gives one output (so a void method can't be a function), and really it should always be the same output for the same input. So a method like Math#log represents a function, and you find from the Java™ tutorials that a Map models a function.
    David Freitag
    Greenhorn

    Joined: May 28, 2011
    Posts: 13

    You are missing the point, i modified my post to reflect exactly what i was trying to say.. sorry for any confusion.
    Kwasi Roberts
    Greenhorn

    Joined: May 28, 2011
    Posts: 4
    David and Campbell your words were both very helpful. I understood what both of you were saying to help me. Thanks a million.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    David Freitag wrote:You are missing the point, i modified my post to reflect exactly what i was trying to say.. sorry for any confusion.
    don't modify posts which have been answered, otherwise the answers appear to be nonsense.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39408
        
      28
    Kwasi Roberts wrote: . . . Thanks a million.
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: System.out.println confusion with the quotation marks and + sign