Technical Tips, Tricks and Articles

Posts Tagged ‘code

Comparing Wrapper objects values in range –128 to 127

leave a comment »

While debugging through a bug that I encountered in one of my ongoing project. I came to know a strange behavior of Java. Consider the following code segment

1 Integer a = 10;
2 Integer b = 10;
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

If you look closely at line no 4, then you would realize that the code is actually comparing “Java objects”. Since “a” and “b” are not wrapper classes not primitive types.  But even then the condition is being true. Ok, some how this makes sense (at least till now unless you read this code segment)

1 Integer a = 150;
2 Integer b = 150;
4 if (a == b){
5 	System.out.println("Two values are equal");
6 	}

Astonishingly, for the above code, the if statement would not execute true. Why?

Well, ever since Java has introduced “auto-boxing” feature, you’ll face this issue. Why is it so? Actually, compiler try to optimize things as much it can. For this, Java has a pool of values from –128 to 127. Whenever, there is some auto-boxing between these values, the reference object for the wrapper class will get the same memory location. In both cases, we are comparing “object references” but in first case, since the value was in the range for both objects hence, Java assigned the same reference location to both objects, making the condition “true” on comparing memory location for two objects.

Now, the question is why between –128 to 127?. Well, the smallest range of values for smallest wrapper class is between –128 to 127 i.e. Byte.


Written by ..alee

July 5, 2009 at 1:06 pm

Posted in code, interview, java, programming

Tagged with , ,

Syntax highlighting on blogs

leave a comment »

Syntax highlighting is always been a desired things on blogs, specially for those who post their code so often on blogs. Not only it adds to the look of the blog but also, it makes the code more readable.  From quite a few days, i was trying to search for something by which i can paste my codes in formatted highlighted form on the blog, but whatever plug-in i found was not integrateable into wordpress free service.

The earlier solutions that came to my mind was taking a snapshot of the code window and post here. But it includes few extra steps for me (taking a snapshopt, croping it and the uploading it) and for reader, they just can’t COPY-PASTE the code if they want to.

highlightSo if you are one of those who are looking for some syntax highlighting thing for your blog, here is some great tool, named Highlight. The tools come with some default styles, including eclipse, emacs and other well know editors. You can configure the style if you wish so and the you can export it in your desired format i.e (html – with inline CSS, RTF, XHTML, LaTeX, SVG, XML etc).  See the below snippet that I formatted/highlighted using the it. it’s pretty clean and easy to incorporate 🙂

1 package com.e2e.test;
3 import javax.microedition.lcdui.Alert;
4 import javax.microedition.lcdui.Command;
5 import javax.microedition.lcdui.CommandListener;
6 import javax.microedition.lcdui.Display;
7 import javax.microedition.lcdui.Displayable;
8 import javax.microedition.lcdui.Form;
9 import javax.microedition.lcdui.StringItem;
10 import javax.microedition.lcdui.TextBox;
11 import javax.microedition.midlet.MIDlet;
12 import javax.microedition.midlet.MIDletStateChangeException;
14 public class Test extends MIDlet implements CommandListener, Runnable{
16     private Display mDisplay;
17     private Command mExitCommand, mFindCommand, mCancelCommand;
19     private TextBox mSubmitBox;
20     private Form mProgressForm;
21     private StringItem mProgressString;
23     public Test() {
24         mExitCommand = new Command("Exit", Command.EXIT, 0);
25         mFindCommand = new Command ("Find", Command.SCREEN, 0);
26         mCancelCommand = new Command ("Cancel", Command.CANCEL, 0);
28         mSubmitBox = new TextBox("Test", "", 32, 0);
29         mSubmitBox.addCommand(mExitCommand);
30         mSubmitBox.addCommand(mFindCommand);
31         mSubmitBox.setCommandListener(this);
33         mProgressForm = new Form ("Lookup Progress");
34         mProgressString = new StringItem(null, null);
35         mProgressForm.append(mProgressString);
36     }
38     protected void destroyApp(boolean arg0) throws
39                               MIDletStateChangeException {
40         // TODO Auto-generated method stub
42     }
44     protected void pauseApp() {
45         // TODO Auto-generated method stub
47     }
49     protected void startApp() throws MIDletStateChangeException {
50         mDisplay = Display.getDisplay(this);
51         mDisplay.setCurrent(mSubmitBox);
52     }
54     public void commandAction(Command c, Displayable d) {
55         if (c == mExitCommand){
56             try {
57                 destroyApp(false);
58             } catch (MIDletStateChangeException e) {
59                 System.out.println("exception @ destroyApp(false);");
60             }
61             notifyDestroyed();
62         }
63         else if (c == mFindCommand){
64             mDisplay.setCurrent(mProgressForm);
65             Thread t = new Thread(this);
66             t.start();
67         }
68     }
70     public void run() {
71         String word = mSubmitBox.getString();
72         String definition;
73         try{
74             definition = lookup(word);
75         }
76         catch (Exception ie){
77             Alert report = new Alert("Sorry", "Something went wrong", null,
78                            null);
79             report.setTimeout(Alert.FOREVER);
80             mDisplay.setCurrent(report, mSubmitBox);
81             return;
82         }
83         Alert report = new Alert ("Definition", definition, null, null);
84         report.setTimeout(Alert.FOREVER);
85         mDisplay.setCurrent(report, mSubmitBox);
87     }
89     private String lookup(String word) {
90         if (word.equalsIgnoreCase("1")){
91             throw new RuntimeException("exception thrown");
92         }
93         return "hardcoded definition";
94     }
96 }

Written by ..alee

March 13, 2009 at 9:15 am

Posted in Miscellaneous

Tagged with , , , ,

A C Code, getting a string using gets();

leave a comment »

last night one of my friend showed me a C code and told me that he was unable to get the string input using gets(); I am posting the code here.

char temp[50];
printf("Please enter your name: ");
printf("%s", temp);

Now, he was very confused that why this simple code is not working. I asked him to put fflush() before calling to gets(). He did it and the code worked fine. Do you get the reason?

The printf does not flush the memory and when you call the gets() it use the buffer characters already in memory buffer thus returning empty string.

Written by ..alee

January 11, 2007 at 4:01 pm

Posted in C, code, programming

Tagged with , ,