wood burning stoves 2.0*
The moose likes Java in General and the fly likes Regarding equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding equals method" Watch "Regarding equals method" New topic
Author

Regarding equals method

surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
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

Joined: Aug 05, 2005
Posts: 3432
    
  12
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 ?


Joanne
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
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

Joined: Aug 06, 2010
Posts: 512
    
    4

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.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
You can nest conditional statements.
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
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

Joined: Aug 06, 2010
Posts: 512
    
    4

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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding equals method
 
Similar Threads
Factory Vs Abstract Factory Pattern
Head First Design Patterns - Factory Method Question (?)
parameterized factory method
What to do next?
problem comparing strings...