Comparing strings with “==” operator vs “.equals” method [Java]
During lunch, I was reflecting on that day when my programming teacher asked me to come forward and teach “bitwise” operations to the whole class, and I remembered how, in my attempt to explain to them the basic difference between a regular “OR” and a bitwise “OR”, I had talked about value comparison being different from a bitwise comparison. Then I began to wonder. A bitwise operation on machine level is fairly simple to understand, but what about value comparison? What is it that happens at machine level when you check if two integer values are equal?
As I began my search for the answer, I pinged Vladislav Zorov, my mentor and friend, and asked him. He said that compilers mostly just subtract the memory addresses of the two objects being compared and returns true if the difference is zero i.e. if they are literally the same object. I couldn’t agree with this so I argued against it. I said that it is possible for two variables to point to identical objects without pointing to the same object and so I set out to prove it.
I wrote a very simple program:
import java.util.Scanner;
public class HelloWorld {
public static void main(String []args){
// Created two strings using the same literal and an array with the second index set using that literal.
String x = “abcd”;
String y = “abcd”;
String[] z = {“asafaf”,”abcd”};
// Printing addresses of all 3.
System.out.println(Integer.toHexString(x.hashCode()));
System.out.println(Integer.toHexString(y.hashCode()));
System.out.println(Integer.toHexString(z[1].hashCode()));
// Checking to see if the equality symbol works on these
if(x == z[1] && x==y && y==z[1])
System.out.println(“== works on the 3”);
// Comparing the 3 using the equals method which will, obviously, work.
if(x.equals(z[1]) && x.equals(y) && y.equals(z[1]))
System.out.println(“equals method works on the 3”);
// Creating Scanner object to read from stdin.
Scanner input = new Scanner(System.in);
// Taking two strings as input. Will test with
// 1. Two different strings
// 2. Identical strings
// 3. “abcd” for both
String j = input.next();
String k = input.next();
//printing their addresses
System.out.println(Integer.toHexString(k.hashCode()));
System.out.println(Integer.toHexString(j.hashCode()));
// Testing both comparisons
if(j==k)
System.out.println(“== working on input strings”);
if(k.equals(j))
System.out.println(“equals working on input strings”);
// Initializing a string identical to x,y,z[1] but with the new keyword
String a = new String(“abcd”);
// Printing its address
System.out.println(Integer.toHexString(a.hashCode()));
if(a==x || a==y || a== z[1])
System.out.println(“== working with new keyword”);
if(a.equals(x))
System.out.println(“equals method working with new keyword”);
}
}
If you run it, you’ll see that its output shows that all strings that have the same value point to the same address regardless of how they’re initialized. But the equality symbol only works when two strings have been created with the same literal. The fact that we’ve now proven that identical strings in Java do have the same memory address does perhaps imply that comparison is done simply by comparing addresses but then what about the equality symbol? Well, I’ll update this post when I find out.
David Carson on JS
The dirty little secret is that most “modern JavaScript development” is nothing to do with actually building websites – it’s building packages that can be used by people who build libraries that can be used by people who build frameworks that people who write tutorials and teach courses can teach.
I’m not sure anyone is actually building anything for actual users to interact with. – David Carson
Die Verwandlung
I just finished reading Franz Kafka’s “Die Verwandlung,” translated into “The Metamorphosis” by Stanley Corngold. Before I even started reading it, I’d told my classmate about how it seemed like a weird one — something I’d deduced from its synopsis — and he’d responded with “Franz Kafka’s works tend to be.”
Now that I’m done with it, I’m not quite sure how I feel about it. For one, it was incredibly short. Like I knew it was a short story but I’d still been expecting it to be longer. Then there comes the fact that (SPOILER ALERT)
Read Full PostWhat if logic isn’t actually all that logical?
Logic, science, reason, all of it is what? Rules that always hold true for everything that they are defined for. But who really defines them? We do. Who comes up with them? We do. And what are they based on? Observation.
I remember, as a kid, reading an article in my native language, titled “What if there was no Sun?” and it, as you can guess, was full of stupid stuff like “pitch black, ice cold, nobody would survive for any more than … e.t.c.” While I was reading it, I couldn’t help but wonder why a world without our Sun would ever have any life-forms that need the sun to survive? Or why the humans _on such a world would not _be used to not having a sun and thus be optimized for survival in such harsh conditions.
My point? We believe logic is absolute. Maybe it is, but only for our world. We know that if we pull at a free movable object, it would move towards us. But for all we know, there could be a world out there in which doing so would result in that very object moving away from us. That sounds illogical, stupid, and inconceivable but if it happened all the time in our world, it wouldn’t be.
We believe that we can make deductions using reasoning, but how reasonable would that be when that very art of reasoning itself is a set of deductions based on observation. What about observation? Do we even really see our world as it is? We don’t even know if we all see the world the same way. A really narrow range of the electromagnetic spectrum is sensed by our eyes and is used by the brain to form an image. This is literally akin to reading a detailed book and imagining the events and the characters. Using this analogy, we can tell that while the image can be extremely detailed, it will still be limited by the amount of information it is based on. Imagine if our ears were so trained that we could, in the absence of light, form images and see using sound. Is _that _inconceivable? But what really is the difference between the ear and the eyes? Both sense and interpret waves with the help of the brain. If we could see the rest of the electromagnetic spectrum, we would have a much smaller list of opaque materials than we currently do.
This does of course suggest that it doesn’t matter if the science and logic we so rely on is limited by our observation as long as it holds true for our observable world, but the idea was the very fact that it would be just limited to our observable world. This is kind of like how we wouldn’t be able to tell if there were any more-than-3-dimensional-beings out there observing us right now because we can’t perceive or observe more than 3 dimensions.
Linus on C++
C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it... In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C. In general, I'd say that anybody who designs his kernel modules for C++ is either (a) looking for problems (b) a C++ bigot that can't see what he is writing is really just C anyway (c) was given an assignment in CS class to do so. Feel free to make up (d).
How GoDaddy robbed me.
Humble request to all readers: Share this post.
I really want to keep this as short as I can and address only the primary issue. For those who don’t know, GoDaddy is one of the most popular domain registrar’s in the market and I started using it a couple of years back as a means for getting rid of my local registrar who had been screwing up routinely in a most inefficient way.
Abstract: Lately, they have not only made a few irrational and immoral decisions, but they also robbed me of a rather large sum of money and then messed up my order completely. In short, I paid thrice for the same order and I didn’t even get what I ordered. Furthermore, somehow the same domain was purchased twice using my account and I have absolutely no idea how it’s even possible.
Read Full PostWhat the hell?
So I was minding my own business in my humble abode when I receive an email. A quick look at my phone’s notification bar tells me that it was from the admissions counselor (, let’s call her Daisy,) of a college that had accepted me. I wasn’t at all surprised because the decision day for that college had just passed and I hadn’t shared mine at all.
The email said “Daisy has shared a document with you.” And had a button that said:Open in Docs.
Like a dumbass, I clicked on it. I did wonder why she’d share a document with me and I did, kinda, wonder why it didn’t look like the average Google Doc invitation, but I was on my phone and I chose to overlook it.
Then I land on a page that asks me if I want Google Drive to have access to my email account. It asked for permissions like “Edit, delete and create emails.” I was certain, by this point, that something was not right. And I just allowed it. You know why? Cause the goddamned address bar clearly showed that I was still on Google and still on https. I was redirected and then it appeared to be completing a request and loading something and after waiting for a few minutes, I realized what I had done.
Now I noticed another weird email, and it said “Anas Khan has shared a document with you.” That was it. Now I realized what those permissions were for. I checked the sent mail and realized that this shit had forwarded itself to “hhhhhhhhhhhhhhhh@mailinator.com” and that email was BCC’d to virtually every email address that I had ever communicated with.
Well, kinda screwed up. But I have to say this: It was a clever scam.
Let there be light
The tendency of modern physics is to resolve the whole material universe into waves, and nothing but waves. These waves are of two kinds: bottled-up waves, which we call matter, and unbottled waves, which we call radiation or light. If annihilation of matter occurs, the process is merely that of unbottling imprisoned wave-energy and setting it free to travel through space. These concepts reduce the whole universe to a world of light, potential or existent, so that the whole story of its creation can be told with perfect accuracy and completeness in the six words: ‘God said, Let there be light’. – James Jeans
Did Nokia really not do anything wrong? They did.
“We didn’t do anything wrong but somehow we still lost.”
Those were the words of the CEO. Do I agree with them? No.
Let’s go back to, say, 2006. Every average person had a Nokia. The Motorollas? Those were what people bought between two successive Nokias. Sony Errison? Well, that one had its own cult. There was a wide variety of different lines of phones in the market, each aimed at a different class of users. The basic featured for those looking for a cheap calling device, the multimedia enabled for those that wanted more, the communicators for those that afforded them.
That wasn’t all. That was the time when Nokia did some strange experiments resulting in the production of some really weird and unique phones. And guess what? A large percentage of those took off as well. Examples of such models could be the NGage and the Ngage QD – Gamepad shaped devices aimed at gaming. I happen to have owned both models. Nokia was also infamous for coming up with some really weird designs, which, surprisingly, sold just as well.
Why? Cause Nokia owned the market. They were among the pioneers and they had almost monopolized the mobile market. What they produced was good and was pretty regardless of how shitty it might actually be.
All the awesome devices that Nokia ever made were
Read Full Post