Question

Can anyone explain, what exactly happens in the following line?

java.lang.System.out.print("string" + i);

It will print "string" followed immediately by value of integer i, but what is happening here?

What is the significance of every part of this line?

Was it helpful?

Solution

java.lang is the package, which isn't needed with the correct import statement. System is a class in the java.lang package. out is a static public field (requires no bound instance, like a global variable) in the System class which has type PrintStream.

A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

This PrintStream is connected behind the scenes to an OutputStream which can either be connected to IDE output or console output (via Java Native Interface).

The arguments to the print method "string" + i are concatenated using the + operator into a big string, which can be directly fed to the print method. i is a primitive int type so is temporarily converted in memory to String during concatenation. As for string concatenation optimisation behind the scenes:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class (§20.13) or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive objects, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

OTHER TIPS

"string" + i is performing String concatenation, creating a new String with the contents of string123, for example.

java.lang.System.out.print (typically abbreviated to System.out.print) then writes that String to the "standard output" - typically the console running the program.

From the String javadoc:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

So "string" + i gets turned into:

new StringBuilder("string").append(i).toString();

and then is passed to System.out.print()

java.lang.System is qualified name of System class that resides in java.lang package. You're requesting its method print which supposes to get String value as the argument. Being called the method sends the resulting object in the brackets (cast to String ) to the default output (that is usually the console).

It is a method, that will show your message on the command line and there is another way to show your message in window, write:

JOptionPane.showMassageDialog(null,"your message");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top