1

I was trying to print a string and int on a same line. But I get an error. I know my way around this error but why does the line System.out.println("This is a string: %i", c2.a);gives error whereas the line System.out.println("This is class method" + c2.a ); gives the correct output. Below is my code.

public class MyClass
{
  private int a;
  public double b;

  public MyClass(int first, double second)
  {
    this.a = first;
    this.b = second;
  }

  // new method
  public static void incrementBoth(MyClass c1) {
    c1.a = c1.a + 1;
    c1.b = c1.b + 1.0;
  }

  //pass by valuye therefore no change
  public static void incrementA(int a)
  {
    a = a+1;
  }

  public static void main(String[] args)
  {
    MyClass c1 = new MyClass(10, 20.5);
    MyClass c2 = new MyClass(10, 31.5);
    // different code below
    incrementBoth(c2);
    incrementA(c1.a);
    System.out.println("This is a object passing: %i",c2.a);
    System.out.println("This is object passing: " + c2.a );
    System.out.println("This is pass by value: %d",c1.a);
  }
}

My other question is does the line incrementBoth(c2) changes value of c2 because here whole object is passed to the method rather than passing by value in incrementA(c1.a)

Rahul Khanna
  • 59
  • 1
  • 2
  • 8
  • Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). - please ask one question at a time only. – Timothy Truckle Feb 18 '17 at 10:44
  • Because println() accepts a single argument, not two. https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-java.lang.String-. The error message tells you exactly that. You should read it. printf() takes several arguments: https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#printf-java.lang.String-java.lang.Object...-. So, in short, read error messages, and read the javadoc. – JB Nizet Feb 18 '17 at 10:44
  • 1
    Regarding your second question, http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 would be a good read. – fvu Feb 18 '17 at 10:46

2 Answers2

5

You need to use the printf method and not println.

println is used to print primitive types, Strings and objects as they are. Also, println takes only one argument as input. That is the reason you are getting an error when you pass multiple arguments to it in your code.

printf on the other hand is used to format and then print the formatted string to the Standard output / error. This is what you should use in your code above for formatting the output.

Here's a reference to the tutorials.


Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
  • This gives me an `Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'`. and no output is given. Why is that so? – Rahul Khanna Feb 18 '17 at 10:55
  • https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax – fvu Feb 18 '17 at 10:56
  • You need to use `%d` not `%i`. Like this: `System.out.println("This is a object passing: %d",c2.a);` – anacron Feb 18 '17 at 10:59
  • thanks @fuv would read it. But does my question really deserves -1. It strange to see that . – Rahul Khanna Feb 18 '17 at 10:59
0

Try:

int x = 3;
System.out.println("This is my number" + x);

The output should be:

This is my number 3