Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding equals method

 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,


can you explain to me above preceding code,How it will compare objects and also i"m getting compile time error when i adding Glob at the end(Like type.equals("Pro")?"Prof":"Tech":"Glob";)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surya.raaj prakash wrote:

can you explain to me above preceding code,How it will compare objects


It won't. It'll throw a NullPointerException.
If you were to initialise your type variable, then it will compare it's value with the value "Pro" and if they are the same it will assign the value "Prof" to your val variable. If they are not the same, it will assign the value "Tech" to your val variable.

surya.raaj prakash wrote:i"m getting compile time error when i adding Glob at the end(Like type.equals("Pro")?"Prof":"Tech":"Glob";)


That's because that is not valid Java syntax. What did you think that code would do ?
 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you see there is a ternary operator and substitutes an if / else construct.
Have a look at http://en.wikipedia.org/wiki/Ternary_operation
That will also explain why you can't add "Glob" at the end of this expression.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you have a Java™ problem, always have a look at the Java™ Language Specification, but it may be difficult to understand.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
surya.raaj prakash wrote:

can you explain to me above preceding code,How it will compare objects


It won't. It'll throw a NullPointerException.

If they are instance fields, yes. Otherwise type is an uninitialized local variable and you'll get a compiler error.
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Thanks For Your Reply..

one more doubt is How can i test 3 rd value(i.e Glob) with this condition(type.equals("pro"))
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not really clear to me what you want to do.

If you want to test whether String type equals either "Pro" or "Glob" you could use:

What this will do is it will compare whether your String, type, equals either "Pro" or "Glob", and if it equals one of these, it will return "Prof" and assign it to the String outcome, otherwise it will return "Tech" and assign that to the String outcome.

If that's not what you want, you should clarify your query.

If you want to add a third value to the output, you have to think about what you're asking. You're telling Java: "Test this expression for truth, if it returns true, do this, if it returns false, do this.". So after the "?", you specify what you want it to do if it's true, and if it's false. There is no other value to specify. Your expression can't be "tralse" or "fue" or something, it's either true or false, and you've already specified those two values.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can nest conditional statements.
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:it's not really clear to me what you want to do.

If you want to test whether String type equals either "Pro" or "Glob" you could use:

What this will do is it will compare whether your String, type, equals either "Pro" or "Glob", and if it equals one of these, it will return "Prof" and assign it to the String outcome, otherwise it will return "Tech" and assign that to the String outcome.

If that's not what you want, you should clarify your query.



I want like this But I'm getting compiler error when i add Glob value to the preceding code
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what outcome should result in Glob?

Your expression will return a boolean, it will test whether something is true or false. It can be either true or false, there is no third value. When should it choose a third value and return a third outcome? If you want to make your expression return an integer and choose a value based on that integer, you should try a switch statement, not a ternary operator.

@Ritchie: let's not make this even more confusing
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surya.raaj prakash wrote: . . . I'm getting compiler error . . .
Hardly surprising. The ?: operator is ternary, that is, it takes three operands. You are trying to provide it with four operands. Look at the link I provided earlier.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ternary operator is shorthand for "if X then Y else Z". As Dieter already mentioned, there is nothing to indicate when to return "Glob". The nested statement Campbell mentioned would solve that: if "pro" then "Prof" else (if "glo" then "Glob" else "Tech)" (parentheses added for clarity.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic