Posts

Showing posts with the label Java

Java Characters

Characters are represented by codes. ASCII is a standard for alphabets and a few symbols. For example, the code 65 is used for the letter 'A' char c1 = 65; System.out.println(c1); Will print the character A. Modern Java compilers and Operating Systems are aware of Unicode characters. Unicode is a standard system of encoding characters in all known scripts. Here's code to print the recent 'Rupee' symbol: char c1 = 0x20b9; System.out.println(c1); Will print the symbol  ₹ If you don't have a local Java compiler, you can use this online compiler . Explore Unicode. Share some characters and the corresponding codes of your mother-tongue. Click here to power back

Java Strings

If you are unfamiliar with Java or interested in a refresher, use this link . In this blog we look at some quirky dealings with Java Strings. We'll be using concepts of Java variables , data-types and operators and especially Strings . The following code constructs two strings  s1  and  s2 . When they are compared, their contents are identical. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1 == s2) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does NOT equal " + s2); } Strangely, this code reports that the strings are not equal: 12345 does NOT equal 12345   Why is that? How do we fix this? Notice the change in bold below. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1.equals(s2)) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does...