Saturday, October 10, 2009
An interesting java program that caught my attention is the one that follows!import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class ScrollSample {
public static void main(String args[]) {
String title = "JScrollPane Sample";
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon icon = new ImageIcon("dhoni.jpg");
JLabel label1 = new JLabel(icon);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(label1
);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Labels: Java, Programming
Saturday, October 3, 2009
Hope you meditated enough.
It's been quite some time since my last post.
I had been really busy organizing the Intra Virtuoso events for my college juniors. Anyway getting back to the post, we shall continue from where we left...
No preprocessor directives in Java
Which means that, Java uses something else to makeup!
It uses a technique which we would be calling as...
Importing of Classes
Consider the following code in C
# include <stdio.h>
void main()
{
printf("Hi");
}
It actually has only four lines of code.
But at compile time it shows that there are 312 lines of code!
The remaining 308 lines of code is from the stdio.h header file. Its added into the program.
In C, its just the standard library functions from the header files.
Java has a different hierarchy for programming!
Java Library------->Packages------->Classes & Interfaces------->Methods
So, if a programmer wants to use a class then only that class imported.
Difference between #include and import
#include directive makes the compiler to go to the c/c++ standard library and then copy from the header files into the program.
import statement makes the JVM go to the java standard library, execute the code there and then substitute the result in the program.
Here no code is copied and hence there is no waste of memory or the process time.
Eg:
import java.lang.system;
import java.lang.string;
Note:
java.lang.*;
is automatically imported to all java programs!
So the code import java.lang.*;
is never needed!
Now the comparison between C and Java is over!
We shall try to get along with Java basics from now on.
Try to recollect the Hello world program in Java.
I would try to get back to you with my next post.
Labels: Java
Sunday, September 6, 2009
What Java does not have?
No functions in Java (Only Methods)
Function vs. Method
1. C++ functions could be present inside and outside the class.
2. But java methods could be written inside classes only.
No pointers in Java
1. Pointers lead to confusion.
2. Pointers crash program easily. (Addition of 2 pointers, Runtime memory leakage-dangling pointers)
3. Pointers break security.
No operator overloading in Java
1. Operators are immutable in java.
2. Operator’s meaning cannot be changed in java.
No preprocessor directives in Java
No multiple inheritance in Java
No static memory in Java
No destructors in Java
No goto statement in Java
Well, there seems to be more features available in C and C++ than in java. Why learn Java then? Doesn’t your brain ask this question? My brain did ask.
And I began searching for the answer.
First we have to compare C and Java to understand how the languages work.
Here is a small comparison between C and Java
C vs. Java
So Java is powerful mainly because, of the byte code. It could be interpreted by the Java Virtual Machine. So based on the system, different JVMs are used. But the class file remains the same unlike C's exe file. It’s quite an interesting fact that JVM was written using C.
There is a big table with all the differences between C and Java mentioned in it. I would suggest you to skip it if you feel it to be too big.
Click here to view that table.
C uses a compiler or an interpreter (only one at a time).
Java uses both compiler and interpreter together.
In addition to this it uses a pretty interesting JIT. Now what does this JIT do?
Wikipedia gives the following definition:
In computing, just-in-time compilation (JIT), also known as dynamic translation, is a technique for improving the runtime performance of a computer program. JIT builds upon two earlier ideas in run-time environments: byte code compilation and dynamic compilation. It converts code at runtime prior to executing it natively, for example byte code into native machine code. The performance improvement over interpreters originates from caching the results of translating blocks of code, and not simply reevaluating each line or operand each time it is met. It also has advantages over statically compiling the code at development time, as it can recompile the code if this is found to be advantageous, and may be able to enforce security guarantees. Thus JIT can combine some of the advantages of interpretation and static (ahead-of-time) compilation.
The paragraph does not make complete sense to you unless you already know about JIT ;-).
Now let me try to put it simple.
Consider the following code:
for(i=1;i<=10;i++)
print i
It is a program to print from one to ten.
Normally, a compiler will convert the print i code ten times (say it takes ten
nanoseconds).
JIT Compiler will load print i at a place in the memory with an increment in it
and call it ten times (it takes just 2 nanoseconds).
Convert print I --------> 1 nanosecond.
Load it in memory -----> 1 nanosecond.
I have told you what Java does not have?
Why Java does not have those features is the question you have to think upon. Keep meditating till the next post.
Labels: Java
I had been spending my last fortnight with Java. As I was studying, I understood its power and the flexibility it gives to a programmer. Though I have learnt Java in bits and pieces, I have not learnt it from the basic (ie., from the origin, history, architecture,..). In my college, we students have not been attending the classes for the past one week due to the fear of getting infected from swine flu in the campus! This has been like a blessing in disguise, I was able to peacefully explore Java. As I was exploring Java, I got an idea of making a small tutorial for people like me who have learnt Java in bits and pieces.
I will be posting my tutorial here in my blog once in a while! It is a tedious process so there is no fixed timeline on my tutorial posts. Continue your support.
Labels: Java