sábado, 2 de noviembre de 2013

PRINT FOR CONSOLE IN JAVA (System.out)

In Java there are some objects that exist by default in any development environment (call
Eclipse , NetBeans , BlueJ , etc. . ) . One is the object called System.out . This object has
a method called println that we can print anything on screen in a console window .

The basic syntax is : System.out.println ( " Message to display " ) ;

Note that the first S of System.out is capitalized. If the write tiny get a compilation error . Recognize this because any small error in writing ( simple change of a letter) in the name of an object , variable, method, etc. . can lead to errors. Also, sometimes BlueJ will point the location of the error but sometimes it will not and you'll exactly look patiently. Also keep in mind that the spaces within the quotes feature , ie the result of write ( " Message to display " ) is not the same as writing                      ( "       Message to display " ) .

If we include variables concatenated using the + symbol like this: System.out.println ( " Price is " + price + " euros" ) The symbol + is used to concatenate strings, or variables that represent text.

What happens if we introduce a concatenation a number or a variable that is not a string? Default Java will convert what we concatenated to text. Eg
System.out.println ( " Price is " + 42 + "dollars " ) ;
The display will show as: The price is 42 euros. Note that we have included the necessary spaces
before and after the number to avoid screen will appear that the price is de42euros .

If we want to print a blank line write this: System.out.println ( ) The same result is obtained by writing System.out.println ("" ) ; Writing this is valid : System.out.println ( " My name is" + "John" ), and that after all we are concatenating two strings , albeit more logical write ( " My name is John " ) ;

0 comentarios:

Publicar un comentario

deja un comentario