Results 1 to 2 of 2

Thread: Operator OverLoading Code

  1. #1

    Operator OverLoading Code

    I am new to Java. if this question is simple one then don't scoled me.
    operator oveloading is there or not in java?

    if it is not there then why the following code is behaving differently?

    System.out.println(1+2); (adding)
    System.out.println("Hai"+1+2); (concatinating)
    System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)

    if operator overloading is there then please explain.


  2. #2
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    Re: Operator OverLoading Code

    Quote Originally Posted by vinodjallapally View Post
    I am new to Java. if this question is simple one then don't scoled me.
    operator oveloading is there or not in java?

    if it is not there then why the following code is behaving differently?

    System.out.println(1+2); (adding)
    System.out.println("Hai"+1+2); (concatinating)
    System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)

    if operator overloading is there then please explain.
    -----
    hi

    this follows a simple rule of '+' operation of java.
    The rule is " if either operand is string then it converted the number value to stirng and concates with the string value, addtion only occurs only when both the operands are number".

    1)System.out.println(1+2); (adding)

    Reason: both the operands are number

    2) System.out.println("Hai"+1+2); (concatinating)

    + is haveing left associativy so the output will be "Hai12"
    Execution order is as below:
    "Hai"+ 1 = "Hai1" then
    "Hai1"+2 = "Hai12"


    3) System.out.println(1+2+"Hai"+1+2); (first adding & then concatinating)

    Execution order:
    1+2 = 3 (both are number)
    3 + "Hai" = "3Hai"
    "3Hai"+1 = "3Hai1"
    "3Hai1" + 2 = "3Hai12"

    i think this will clear your doubts


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact