SimpleSi Posted June 17, 2010 Posted June 17, 2010 JFI if (string1 == string2) { ... } doesn't work in java You have to use other techniques such as if (string1.equals(string2)) { ... } taken me days to work why it wasn't working and eventually googledfro java string comparision to find out that it doesn't work like PHP or C or C++ you live and learn regards Simon
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 Java is a bit backwards ( or at least I thought so ) I only did a little at uni in comparison to C++ or the likes, same goes for data casting, done differently between c++ and java from what I remember. maybe that's just me ??
webman Posted June 17, 2010 Posted June 17, 2010 It's because a string in Java is essentially an object. The comparison operator would compare the unique objects and just wouldn't be the desired output. That's why they have the equals() method which will actually compare the value of the string to something else and return the proper result 1
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 It's because a string in Java is essentially an object. So it is in other sane languages, but that's what we have operator overloads for. Java apparently doesn't.
PiqueABoo Posted June 17, 2010 Posted June 17, 2010 it doesn't work like PHP or C or C++ Interesting but if it's strict ANSI C you use strcmp() and so on, but "or JavaScript/JScript" works.
webman Posted June 17, 2010 Posted June 17, 2010 Yeah. I'm not particularly fond of Java, but that's just the way it works. *shrugs*
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 Of course then you have fun like the Bourne shell families, where we have test operators like "-eq", "-ne", "-lt", "-le", "-z", "-n" and so on, since it is not object-based nor has overloads...
tom_newton Posted June 17, 2010 Posted June 17, 2010 Java feels backwards becuase you're using a method from one object, but you're comparing two. Eg. Perl if ($foo eq $bar) C if (strcmp(foo,bar) == 0) Java if (foo.equals(bar)) So this one's the only one which "looks" uneven - counterintuitive, even. Not done java for ages, but it might be possible to do something like: if (string::equals(foo,bar))
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 (edited) It's because a string in Java is essentially an object. The comparison operator would compare the unique objects and just wouldn't be the desired output. That's why they have the equals() method which will actually compare the value of the string to something else and return the proper result I need to save up about £1000 and get a bunch of books / cbt nuggets etc - tons and tons to learn !! Trying to learn from tutorials is tough as I dont know what code snippets are correct or not etc and what do you mean by object exactly when referring to programming ? In java then how come they dont do if (foo.equals(bar.value())) seeing as they are using the .equals Edited June 17, 2010 by mac_shinobi
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 what do you mean by object exactly when referring to programming ? Object-oriented programming - Wikipedia, the free encyclopedia 1
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 Object-oriented programming - Wikipedia, the free encyclopedia so quoting from wikipedia An object is a discrete bundle of functions and procedures, often relating to a particular real-world concept such as a bank account holder or hockey player. Other pieces of software can access the object only by calling its functions and procedures that have been allowed to be called by outsiders. Would that mean if I had a script ( i know you might use byref or byval in a function, I think in a fully blown language such as c++, java or w/e ) Just as a simple example without over complicating it Sub add(x,y) add = x + y End Sub Then the object would be add sub??
webman Posted June 17, 2010 Posted June 17, 2010 Not exactly Take a bank account as an example. An example of a class would be a customer. The properties of this class would be their account number, name, address, and balance. Methods would be withdraw() and deposit(). An object is just an instantiation of a class. Pseudocode example: Customer webman = new Customer(12345678, "Craig", "101 Front Street", 4000); webman.withdraw(200); Then you get on to inheritance - the generalisation-specialisation part of object-oriented programming. A regular customer contains the stuff as mentioned above; and then you could have a specialised "Gold" customer who may have an overdraft attribute added on - but it also inherits all of the customer stuff too. The concept is a bit different from procedural programming, but once you get your head around it it becomes a lot easier.
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 (edited) An object is just an instantiation of a class. Pseudocode example: That's where I get lost - english please so the class contains all the properties that define the class but its not an object until its created Edited June 17, 2010 by mac_shinobi
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 Well not really, since subs can't return values... but I see what you mean. No. In a full OO language, add() would be a function of some object (like object.Add()) and it would take only one parameter, and add it to the value already possessed by the object: Class myobject Dim mynumber As Integer Sub Add(othernum As Integer) mynumber = mynumber + othernum End Sub Function GetNumber() As Integer GetNumber = mynumber End Function End Class (my VB is rusty, but this is reasonable pseudo-code if nothing else...) This is the difference between procedural code, like your sub, and fully object-orientated methods like above.
webman Posted June 17, 2010 Posted June 17, 2010 A class is like a blueprint, and creating an instance of an object is bringing it to life so you can interact with it. I have some information that might be useful to you, will PM tomorrow
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 Customer webman = new Customer(12345678, "Craig", "101 Front Street", 4000); webman.withdraw(200); You're not going to assign that withdrawal to a variable somewhere, just let it fall into a null pointer? A class is like a blueprint, and creating an instance of an object is bringing it to life so you can interact with it. Unless you're working with abstract classes, which can't be instantiated -- wait, I'm taking this too seriously...
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 Well not really, since subs can't return values... but I see what you mean. No. In a full OO language, add() would be a function of some object (like object.Add()) and it would take only one parameter, and add it to the value already possessed by the object: Class myobject Dim mynumber As Integer Sub Add(othernum As Integer) mynumber = mynumber + othernum End Sub Function GetNumber() As Integer GetNumber = mynumber End Function End Class (my VB is rusty, but this is reasonable pseudo-code if nothing else...) This is the difference between procedural code, like your sub, and fully object-orientated methods like above. that just confused me more and went over my head - am trying to understand objects and you've created a class with functions inside of the class and then you have called the objects method ie myobject.add() not sure if thats helping or not ??
webman Posted June 17, 2010 Posted June 17, 2010 @powdarrmonkey Nah, I know it will just return false anyway 1
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 A class is a definition of what an object looks like to the programmer. Creating an object based on that class means it possesses all the functions, subs an variables you specified in it, like a blueprint.
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 abstract class - wooo slow down, too fast sensai !!
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 Ignore the bit about abstract classes, you don't need them until you start to use inheritance.
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 A class is a definition of what an object looks like to the programmer. Creating an object based on that class means it possesses all the functions, subs an variables you specified in it, like a blueprint. working with simple examples ( or as simple as possible please ) in this case we could say window or door the object would be the window or door to create the object you would require specific properties in order to describe or make the door or window ie height width depth material in the windows case you might have opacity ( as a shower door would have specific glass that would be difficult to see through in comparison to a normal window that you could see straight through , translucent, transparent etc ) but then how would this apply in the programming sense with regards to if i wanted to create a 50% translucent window with height, width that were equal ie 500 * 500 with a depth / thickness of 10mm ?? inheritance and abstract classes can take a back seat as that doesnt mean squat to me right now
powdarrmonkey Posted June 17, 2010 Posted June 17, 2010 Maybe the traditional pets, dogs, and cats examples will help: Visual Basic/Object Oriented Programming - Wikibooks, collection of open-content textbooks
webman Posted June 17, 2010 Posted June 17, 2010 Windows myWindow = new Window(50, 500, 500, 10); The myWindow variable is now a 50% translucent window measuring 500 x 500 and has a depth of 10mm. When you create a new object, it's constructor method is automatically called. The constructor method is named the same as the class. This is what the Window class might look like: public class Window{ private int mTranslucency; private int mWidth; private int mHeight; private int mThickness; public Window(pTranslucency, pWidth, pHeight, pThickness){ // Take parameters from constructor and set attributes mTranslucency = pTranslucency; mWidth = pWidth; mHeight = pHeight; mThickness = pThickness; } public int getArea(){ return mWidth * mHeight; } } I can now call myWindow.getArea() and it will give me the area.
mac_shinobi Posted June 17, 2010 Posted June 17, 2010 (edited) Maybe the traditional pets, dogs, and cats examples will help: Visual Basic/Object Oriented Programming - Wikibooks, collection of open-content textbooks ok gone through that and there are a few things that dont quite make sense There are two types of inheritance: implementation and interface. In the pet examples above an array of cPet objects is used to hold the pets. Implements cPet <-- is this the same as an include when using asp or c++ like including the header file ?? If so then how come the cPet class has nothing in it ? thanks webman and pmonkey !! Side note - are there any cbt nuggets or anything that goes through all this sort of stuff in plain english as all these extra terms chucked in etc is not sinking in or making sense with ref to webmans window example where has the class created and he has one method to calculate the area, I presume you could do the same for transparency and use a switch case / select case to determine what type of glass anyone would want depending on the transparency value ie 0% would be plain glass, 50% would be a shower pane and 100% would be one way glass ?? Then get it to return the result to the method in the same way I did above with my script sub which may have to be a main method or function in whatever language that is OOP c++ or java or w/e ?? Edited June 17, 2010 by mac_shinobi
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now